This problem is really killing me.
A for an afternoon
#include <cstdio> #include <queue> #include <cstring> #include <algorithm>using namespace std; typedef long LONG ll;const int maxn = 200005;int h,w,n;int c1[maxn],c2[maxn];int Vis1[maxn] = {0},VIS2[MAXN] = {0};p Riorit Y_queue<int,vector<int>,less<int> >q1;priority_queue<int,vector<int>,less<int> >q2;int lowbit (int x) {return x & x;} int sum (int c[],int x) {int ret = 0; while (x > 0) {ret + = c[x]; X-= Lowbit (x); } return ret;} void Add (int x,int d,int n,int c[]) {while (x <= N) {c[x] + = D; x + = Lowbit (x); }}int Main () {memset (c1,0,sizeof (C1)); memset (c2,0,sizeof (C2)); scanf ("%d%d%d", &w,&h,&n); int max_width = W; int max_height = h; int Oh = 0, ow = 0; VIS1[H] + +; Q1.push (h); VIS2[W] + +; Q2.push (w); H + +; W + +; Add (1,1,H,C1); Add (H,1,H,C1); Add (1,1,W,C2); Add (W,1,W,C2); for (int i = 0; i < n; i++) {char str[7]; INT D; scanf ("%s%d", str,&d); D + +; if (str[0] = = ' H ') {int L = 1,r = d-1; int e1,e2; int v = SUM (c1,d); while (L < r) {int mid = (L + r) >> 1; int ans = SUM (c1,mid); if (Ans >= v) r = Mid; else L = mid + 1; } e1 = L; Add (D,1,H,C1); v = SUM (c1,d) + 1; L = d + 1; R = h; while (L < r) {int mid = (L + r) >> 1; int ans = SUM (c1,mid); if (ans < v) L = mid + 1; else R = Mid; } e2 = L; int D1 = D-E1,D2 = e2-d; Vis1[d1 + d2]--; if (!vis1[d1]) Q1.push (D1); if (!vis1[d2]) Q1.push (D2); VIS1[D1] + +; VIS1[D2] + +; while (!q1.empty ()) { int t = q1.top (); Q1.pop (); if (Vis1[t]) {max_height = t; Q1.push (t); Break }}}} else if (str[0] = = ' V ') {int L = 1,r = d-1; int e1,e2; int v = SUM (c2,d); while (L < r) {int mid = (L + r) >> 1; int ans = SUM (c2,mid); if (ans < v) L = mid + 1; else R = Mid; } e1 = L; Add (D,1,W,C2); v = SUM (c2,d) + 1; L = d + 1; R = W; while (L < r) {int mid = (L + r) >> 1; int ans = SUM (c2,mid); if (ans < v) L = mid + 1; else R = Mid; } e2 = L; int D1 = D-E1,D2 = e2-d; Vis2[d1 + d2]--; if (!vis2[d1])Q2.push (D1); if (!vis2[d2]) Q2.push (D2); VIS2[D1] + +; VIS2[D2] + +; while (!q2.empty ()) {int t = q2.top (); Q2.pop (); if (Vis2[t]) {max_width = t; Q2.push (t); Break }}} printf ("%i64d\n", (LL) max_width * max_height); } return 0;}
"CF" C. Glass carving (binary + Tree array + Priority queue + array count)