Codeforces 527C Glass Carving 线段树区间合并

传送门:


http://codeforces.com/contest/527/problem/C

C. 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 w mm  ×  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的玻璃,每次水平切割或垂直切割。求每次切割后最大块的面积。

思路:很容易知道,最大块的面积就是水平的最大长度*垂直的最大长度。用两个线段树分别维护水平和垂直的最大长度。tree[id].p表示这个区间的前端被切过。详细操作参见代码:

#include
#include
#include
#define LL __int64
using namespace std;
struct node
{
    int l,r;
    int ll,rr,mlen;
    bool p;
};
void pushup(node *tree,int id)
{
    int m=tree[id].r-tree[id].l+1;

    tree[id].p=tree[id<<1].p;

    tree[id].ll=tree[id<<1].ll;
    if(tree[id].ll==m-(m>>1)&&!tree[id<<1|1].p) tree[id].ll+=tree[id<<1|1].ll;

    tree[id].rr=tree[id<<1|1].rr;
    if(tree[id].rr==(m>>1)&&!tree[id<<1|1].p) tree[id].rr+=tree[id<<1].rr;

    tree[id].mlen=max(tree[id<<1].mlen,tree[id<<1|1].mlen);
    if(!tree[id<<1|1].p) tree[id].mlen=max(tree[id].mlen,tree[id<<1].rr+tree[id<<1|1].ll);
}
void build(node *tree,int id,int l,int r)
{
    tree[id].l=l;
    tree[id].r=r;
    tree[id].p=0;
    tree[id].mlen=tree[id].ll=tree[id].rr=r-l+1;
    if(l!=r)
    {
        int mid=(l+r)>>1;
        build(tree,id<<1,l,mid);
        build(tree,id<<1|1,mid+1,r);
    }
}
void update(node *tree,int id,int pos)
{
    if(tree[id].l==tree[id].r)
    {
        tree[id].p=1;
    }
    else
    {
        int mid=(tree[id].l+tree[id].r)>>1;
        if(pos<=mid) update(tree,id<<1,pos);
        else update(tree,id<<1|1,pos);
        pushup(tree,id);
    }
}
node ht[800005],vt[800005];
int main()
{
    int w,h,n;
    scanf("%d %d %d",&w,&h,&n);
    build(vt,1,0,w-1);
    build(ht,1,0,h-1);
    char s[10];
    int x;
    while(n--)
    {
        scanf("%s %d",s,&x);
        if(s[0]=='V') update(vt,1,x);
        else update(ht,1,x);
        printf("%I64d\n",(LL)vt[1].mlen*ht[1].mlen);
    }
    return 0;
}

你可能感兴趣的:(数据结构)