标签:
就是整体二分啦。。。
然后我们把一个矩形的询问拆成四个,按x排序按y加入bit中就可以O(n * logn^2)做出来啦~
1 /************************************************************** 2 Problem: 1176 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:4620 ms 7 Memory:25808 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 13 using namespace std; 14 const int N = 2e6 + 5; 15 const int Q = 2e5 + 5; 16 17 struct query { 18 int x, y, v, w, id, op; 19 20 inline bool operator < (const query q) const { 21 return x == q.x ? y == q.y ? op < q.op : y < q.y : x < q.x; 22 } 23 } q[Q], tmp[Q]; 24 25 int s, n, cnt, cnt_query; 26 int BIT[N], ans[N]; 27 28 inline int read() { 29 int x = 0, sgn = 1; 30 char ch = getchar(); 31 while (ch < ‘0‘ || ‘9‘ < ch) { 32 if (ch == ‘-‘) sgn = -1; 33 ch = getchar(); 34 } 35 while (‘0‘ <= ch && ch <= ‘9‘) { 36 x = x * 10 + ch - ‘0‘; 37 ch = getchar(); 38 } 39 return sgn * x; 40 } 41 42 inline void add_add() { 43 q[++cnt].x = read(), q[cnt].y = read(), q[cnt].v = read(), q[cnt].id = cnt; 44 } 45 46 inline void add_query() { 47 int x1 = read(), y1 = read(), x2 = read(), y2 = read(), w = ++cnt_query; 48 q[++cnt].w = w, q[cnt].id = cnt, q[cnt].x = x1 - 1, q[cnt].y = y1 - 1, q[cnt].v = 1, q[cnt].op = 1; 49 q[++cnt].w = w, q[cnt].id = cnt, q[cnt].x = x2, q[cnt].y = y2, q[cnt].v = 1, q[cnt].op = 1; 50 q[++cnt].w = w, q[cnt].id = cnt, q[cnt].x = x1 - 1, q[cnt].y = y2, q[cnt].v = -1, q[cnt].op = 1; 51 q[++cnt].w = w, q[cnt].id = cnt, q[cnt].x = x2, q[cnt].y = y1 - 1, q[cnt].v = -1, q[cnt].op = 1; 52 } 53 54 #define lowbit(x) x & -x 55 inline void add(int x, int d) { 56 while (x <= n) 57 BIT[x] += d, x += lowbit(x); 58 } 59 60 inline int query(int x) { 61 int res = 0; 62 while (x) 63 res += BIT[x], x -= lowbit(x); 64 return res; 65 } 66 #undef lowbit 67 68 void work(int l, int r) { 69 if (l == r) return; 70 int mid = l + r >> 1, i, l1, l2; 71 for (i = l; i <= r; ++i) { 72 if (q[i].id <= mid && q[i].op == 0) add(q[i].y, q[i].v); 73 if (q[i].id > mid && q[i].op) ans[q[i].w] += q[i].v * query(q[i].y); 74 } 75 for (i = l; i <= r; ++i) 76 if (q[i].id <= mid && q[i].op == 0) add(q[i].y, -q[i].v); 77 78 l1 = l, l2 = mid + 1; 79 for (i = l; i <= r; ++i) 80 if (q[i].id <= mid) tmp[l1++] = q[i]; 81 else tmp[l2++] = q[i]; 82 for (i = l; i <= r; ++i) q[i] = tmp[i]; 83 work(l, mid), work(mid + 1, r); 84 } 85 86 int main() { 87 int oper, i; 88 s = read(), n = read(); 89 while (1) { 90 oper = read(); 91 if (oper == 1) add_add(); 92 else if (oper == 2) add_query(); 93 else break; 94 } 95 sort(q + 1, q + cnt + 1); 96 work(1, cnt); 97 for (i = 1; i <= cnt_query; ++i) 98 printf("%d\n", ans[i]); 99 return 0; 100 }
标签:
原文地址:http://www.cnblogs.com/rausen/p/4309851.html