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?
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.
After each cut print on a single line the area of the maximum available glass fragment in mm2.
4 3 4
H 2
V 2
V 3
V 1
8
4
4
2
7 6 5
H 4
V 3
V 5
H 2
V 1
28
16
12
6
4
Picture for the first sample test:
10344839 | 2015-03-19 07:09:30 | njczy2010 | C - Glass Carving | GNU C++ | Accepted | 389 ms | 12556 KB |
题解:用两个set记录割点位置,用两个multiset记录割出来的块的大小
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #include <vector> 5 #include <algorithm> 6 #include <queue> 7 #include <map> 8 #include <string> 9 #include <set> 10 11 #define ll long long 12 int const N = 200005; 13 int const M = 205; 14 int const inf = 1000000000; 15 ll const mod = 1000000007; 16 17 using namespace std; 18 19 ll w,h,n; 20 set<ll> x; 21 set<ll> y; 22 multiset<ll> xp; 23 multiset<ll> yp; 24 ll xmax,ymax; 25 26 void out(); 27 28 void ini() 29 { 30 x.clear();y.clear();xp.clear();yp.clear(); 31 x.insert(0);x.insert(w); 32 y.insert(0);y.insert(h); 33 xp.insert(w);yp.insert(h); 34 xmax=w;ymax=h; 35 // out(); 36 } 37 38 void solve() 39 { 40 ll i; 41 char s[2]; 42 ll v; 43 ll r,l; 44 ll le; 45 set<ll>::iterator it; 46 set<ll>::iterator it1; 47 set<ll>::iterator it2; 48 multiset<ll>::iterator it3; 49 for(i=1;i<=n;i++){ 50 scanf("%s%I64d",s,&v); 51 //printf(" i=%I64d s=%s v=%I64d ",i,s,v); 52 if(s[0]=='V'){ 53 x.insert(v); 54 it=x.find(v); 55 it1=it2=it; 56 it1--;it2++; 57 l=*it1; 58 r=*it2; 59 le=r-l; 60 xp.erase(xp.find(le)); 61 xp.insert(v-l); 62 xp.insert(r-v); 63 it3=xp.end(); 64 it3--; 65 xmax=*it3; 66 //printf(" l=%I64d v=%I64d r=%I64d xmax=%I64d ",l,v,r,xmax); 67 } 68 else{ 69 y.insert(v); 70 it=y.find(v); 71 // printf(" *it=%I64d ",*it); 72 it1=it2=it; 73 it1--;it2++; 74 l=*it1; 75 r=*it2; 76 le=r-l; 77 // printf(" *it1=%I64d *it2=%I64d le=%I64d ",*it1,*it2,le); 78 yp.erase(yp.find(le)); 79 yp.insert(v-l); 80 yp.insert(r-v); 81 it3=yp.end(); 82 it3--; 83 ymax=*it3; 84 // printf(" *it3=%I64d ",*it3); 85 // printf(" l=%I64d v=%I64d r=%I64d ymax=%I64d ",l,v,r,ymax); 86 } 87 out(); 88 } 89 } 90 91 void out() 92 { 93 //printf(" xmax=%I64d ymax=%I64d ",xmax,ymax); 94 printf("%I64d ",xmax*ymax ); 95 } 96 97 int main() 98 { 99 //freopen("data.in","r",stdin); 100 //scanf("%d",&T); 101 //for(cnt=1;cnt<=T;cnt++) 102 while(scanf("%I64d%I64d%I64d",&w,&h,&n)!=EOF) 103 { 104 ini(); 105 solve(); 106 // out(); 107 } 108 }