CF #296 (Div. 1) A. Glass Carving 线段树
2015-09-13 16:22
381 查看
A. Glass Carving
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.
In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.
After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.
Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?
Input
The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).
Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.
Output
After each cut print on a single line the area of the maximum available glass fragment in mm2.
Sample test(s)
input
output
input
output
Note
Picture for the first sample test:
Picture for the second sample test:
题目意思:
一个w*h的矩形,然后用水平线和竖直线切割,每次切割后输出最大的子面积。
思路:
很明显最大子面积是横着最大的长度*竖着最大的长度,那么建两棵线段树,每个区间lx、rx、maxh分别为离该区间左端点最近的已经切割过的点、离右端点最近的已经切割过的点、最长的切割长度。维护这三个值就行了。代码略丑。
代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.
In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.
After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.
Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?
Input
The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).
Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.
Output
After each cut print on a single line the area of the maximum available glass fragment in mm2.
Sample test(s)
input
4 3 4 H 2 V 2 V 3 V 1
output
8 4 4 2
input
7 6 5 H 4 V 3 V 5 H 2 V 1
output
28 16 12 6 4
Note
Picture for the first sample test:
Picture for the second sample test:
题目意思:
一个w*h的矩形,然后用水平线和竖直线切割,每次切割后输出最大的子面积。
思路:
很明显最大子面积是横着最大的长度*竖着最大的长度,那么建两棵线段树,每个区间lx、rx、maxh分别为离该区间左端点最近的已经切割过的点、离右端点最近的已经切割过的点、最长的切割长度。维护这三个值就行了。代码略丑。
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cmath> #include <set> using namespace std; #define N 200005 #define ll root<<1 #define rr root<<1|1 #define mid1 (a[root].l+a[root].r)/2 #define mid2 (b[root].l+b[root].r)/2 int w, h, n; struct node{ int l, r; int lx, rx; int maxh; }a[N*4], b[N*4]; void build1(int l,int r,int root){ a[root].l=l; a[root].r=r; if(l==r){ if(l==0) a[root].lx=a[root].rx=0; else if(l==w) a[root].lx=a[root].rx=w; else a[root].lx=a[root].rx=-1; a[root].maxh=0; return; } build1(l,mid1,ll); build1(mid1+1,r,rr); a[root].lx=a[ll].lx; a[root].rx=a[rr].rx; a[root].maxh=a[root].r-a[root].l; } void build2(int l,int r,int root){ b[root].l=l; b[root].r=r; if(l==r){ if(l==0) b[root].lx=b[root].rx=0; else if(l==h) b[root].lx=b[root].rx=h; else b[root].lx=b[root].rx=-1; b[root].maxh=0; return; } build2(l,mid2,ll); build2(mid2+1,r,rr); b[root].lx=b[ll].lx; b[root].rx=b[rr].rx; b[root].maxh=b[root].r-b[root].l; } void update1(int p,int root){ if(a[root].l==p&&a[root].r==p){ a[root].lx=a[root].rx=p;return; } if(p<=a[ll].r) update1(p,ll); else update1(p,rr); int ln, rn; if(a[ll].lx!=-1) a[root].lx=a[ll].lx; else if(a[ll].rx!=-1) a[root].lx=a[ll].rx; else if(a[rr].lx!=-1) a[root].lx=a[rr].lx; else if(a[rr].rx!=-1) a[root].lx=a[rr].rx; else a[root].lx=-1; if(a[rr].rx!=-1) a[root].rx=a[rr].rx; else if(a[rr].lx!=-1) a[root].rx=a[rr].lx; else if(a[ll].rx!=-1) a[root].rx=a[ll].rx; else if(a[ll].lx!=-1) a[root].rx=a[ll].lx; else a[root].rx=-1; if(a[ll].rx!=-1) ln=a[ll].r-a[ll].rx; else ln=a[ll].r-a[ll].l; if(a[rr].lx!=-1) rn=a[rr].lx-a[rr].l; else rn=a[rr].r-a[rr].l; a[root].maxh=max(max(a[ll].maxh,a[rr].maxh),ln+rn+1); } void update2(int p,int root){ if(b[root].l==p&&b[root].r==p){ b[root].lx=b[root].rx=p;return; } if(p<=b[ll].r) update2(p,ll); else update2(p,rr); int ln, rn; if(b[ll].lx!=-1) b[root].lx=b[ll].lx; else if(b[ll].rx!=-1) b[root].lx=b[ll].rx; else if(b[rr].lx!=-1) b[root].lx=b[rr].lx; else if(b[rr].rx!=-1) b[root].lx=b[rr].rx; else b[root].lx=-1; if(b[rr].rx!=-1) b[root].rx=b[rr].rx; else if(b[rr].lx!=-1) b[root].rx=b[rr].lx; else if(b[ll].rx!=-1) b[root].rx=b[ll].rx; else if(b[ll].lx!=-1) b[root].rx=b[ll].lx; else b[root].rx=-1; if(b[ll].rx!=-1) ln=b[ll].r-b[ll].rx; else ln=b[ll].r-b[ll].l; if(b[rr].lx!=-1) rn=b[rr].lx-b[rr].l; else rn=b[rr].r-b[rr].l; b[root].maxh=max(max(b[ll].maxh,b[rr].maxh),ln+rn+1); } main() { int i, j, k; while(scanf("%d %d %d",&w,&h,&n)==3){ char s[5]; build1(0,w,1); build2(0,h,1); // printf("%d %d\n",a[1].maxh,b[1].maxh); while(n--){ scanf("%s%d",s,&k); if(s[0]=='H'){ update2(k,1); } else{ update1(k,1); } // printf("%d %d\n",a[1].maxh,b[1].maxh); printf("%I64d\n",(__int64)a[1].maxh*(__int64)b[1].maxh); } } }
相关文章推荐
- [题解+总结]20150913
- 服务器环境配置nginx / php / php-fpm(二)
- UI课程06 UIController及其子类,已学所有类的关系
- struts2(6)
- 将文字数据写入到硬盘上的文件中。
- “#ifdef __cplusplus extern "C" { #endif”的定义
- struts2(5)
- 常用正则表达式集锦
- POJ 3437 Tree Grafting
- 随机数概率变形01概率p和1-p改变概率为二分之一
- 员工管理系统
- Wamp 简单使用方法
- 聊一聊Android的消息机制
- Java并发概念汇总
- windows和linux的文件传输方法
- view的事件的处理机制-源码分析
- javascript基于原型的面向对象的理论基础
- POJ C++程序设计 编程题#1 输出200
- 组合逻辑六三编码器实现数码管显示对应数字
- 永不放弃-高考励志演讲