Codeforces Round #296 (Div. 2) —— C(set/multiset)

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 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 0001 ≤ 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:

Codeforces Round #296 (Div. 2) —— C(set/multiset)_第1张图片Picture for the second sample test:

Codeforces Round #296 (Div. 2) —— C(set/multiset)_第2张图片题意:有一个高H宽W的方块,现有n个操作,每个操作为横切或竖切,每次操作后都要问你其中最大方块面积是多少?

分析:想到求其中最大方块的面积其实只需保证高和宽都是最长的即可。而每次操作也就是会将本来那段长度分为两段,也就是说去掉了原先那段长度而增加了两条更短的长度。可以用set容器来实现找出插入的位置以及相邻的两根线的位置,这样是为了可以求出所夹的两个长度和被分割的原先长度,这样就可以在用multiset(因为插入的长度中有可能有相同的)来删除原先长度而插入被分割出来的两个长度,以此来维持所有长度以及快速找出最长线段。

code:

#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 200010
int main()
{
    ll W, H, n, x;
    char c;

     scanf("%lld%lld%lld", &W,&H,&n);
     set<ll> s_w; set<ll>::iterator it_w;
     set<ll> s_h; set<ll>::iterator it_h;
     s_w.insert(0); s_w.insert(W);
     s_h.insert(0); s_h.insert(H);
     multiset<ll> S_W; S_W.insert(W);
     multiset<ll> S_H; S_H.insert(H);
     while(n--)
     {
         getchar();
         scanf("%c %lld", &c,&x);
         if(c == 'H')
         {
             s_h.insert(x);
             it_h = s_h.find(x);
             ll a = *--it_h;
             it_h++; it_h++;
             ll b = *it_h;
             *it_h--;
             S_H.erase(S_H.lower_bound(b-a)); //删除b-a长度的迭代器,这样最多只会删除一个元素
             //it = find(b-a); S_H.erase(it); 与上类似,删除元素的迭代器
             S_H.insert(b-(*it_h)); S_H.insert((*it_h)-a);
             printf("%lld\n", (*S_H.rbegin())*(*S_W.rbegin())); //反向第一位是最大的
         }
         else
         {
             s_w.insert(x);
             it_w = s_w.find(x);
             ll a = *--it_w; //找出插入线段的左线段位置
             it_w++; it_w++;
             ll b = *it_w; //找出插入线段的右线段位置
             *it_w--;
             S_W.erase(S_W.lower_bound(b-a));
             S_W.insert(b-(*it_w)); S_W.insert((*it_w)-a);
             printf("%lld\n", (*S_H.rbegin())*(*S_W.rbegin()));
         }
     }
  /*  multiset<int> s;
    multiset<int>::iterator it;
    s.insert(8);
    s.insert(8);
    //s.insert(2);
    s.insert(9);
    it = s.find(8);
    s.erase(s.lower_bound(8));
    s.erase(it);
    printf("%d\n", *s.begin()); */
    return 0;
}

一开始实现有点乱,后来看别人的发现multiset容器竟然可以删除相应迭代器而只删除一位元素,普通的erase()函数是删除所有相应元素。find()函数时间复杂度也是log(n)的。看来容器还是很强大啊。

你可能感兴趣的:(STL,codeforces,set容器)