【BZOJ2683】简单题【CDQ分治】
2016-04-06 11:34
393 查看
【题目链接】
写完才发现和BZOJ1176一样...
/* Pigonometry */
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500005, maxm = 1000005;
int n, tr[maxn], ans[maxm];
struct _data {
int opt, id, qid, x, y, c;
} c[maxm], tmp[maxm];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void add(int x, int c) {
for(; x <= n; x += x & -x) tr[x] += c;
}
inline int sum(int x) {
int res = 0;
for(; x; x -= x & -x) res += tr[x];
return res;
}
inline int cmp(_data a, _data b) {
return a.x != b.x ? a.x < b.x : a.y != b.y ? a.y < b.y : a.opt < b.opt;
}
inline void cdq(int l, int r) {
if(l == r) return;
int mid = l + r >> 1;
for(int i = l; i <= r; i++)
if(c[i].opt == 1 && c[i].id <= mid) add(c[i].y, c[i].c);
else if(c[i].opt == 2 && c[i].id > mid) ans[c[i].qid] += c[i].c * sum(c[i].y);
for(int i = l; i <= r; i++)
if(c[i].opt == 1 && c[i].id <= mid) add(c[i].y, -c[i].c);
int h1 = l, h2 = mid;
for(int i = l; i <= r; i++)
if(c[i].id <= mid) tmp[h1++] = c[i];
else tmp[++h2] = c[i];
for(int i = l; i <= r; i++) c[i] = tmp[i];
cdq(l, mid); cdq(mid + 1, r);
}
int main() {
n = iread(); int tot = 0, id = 0;
while(1) {
int opt = iread();
if(opt == 3) break;
if(opt == 1) {
int x = iread(), y = iread(), w = iread();
c[++tot] = (_data){1, tot, 0, x, y, w};
} else {
int x1 = iread(), y1 = iread(), x2 = iread(), y2 = iread(); id++;
c[++tot] = (_data){2, tot, id, x2, y2, 1};
c[++tot] = (_data){2, tot, id, x1 - 1, y2, -1};
c[++tot] = (_data){2, tot, id, x2, y1 - 1, -1};
c[++tot] = (_data){2, tot, id, x1 - 1, y1 - 1, 1};
}
}
sort(c + 1, c + 1 + tot, cmp);
cdq(1, tot);
for(int i = 1; i <= id; i++) printf("%d\n", ans[i]);
return 0;
}
写完才发现和BZOJ1176一样...
/* Pigonometry */
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500005, maxm = 1000005;
int n, tr[maxn], ans[maxm];
struct _data {
int opt, id, qid, x, y, c;
} c[maxm], tmp[maxm];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void add(int x, int c) {
for(; x <= n; x += x & -x) tr[x] += c;
}
inline int sum(int x) {
int res = 0;
for(; x; x -= x & -x) res += tr[x];
return res;
}
inline int cmp(_data a, _data b) {
return a.x != b.x ? a.x < b.x : a.y != b.y ? a.y < b.y : a.opt < b.opt;
}
inline void cdq(int l, int r) {
if(l == r) return;
int mid = l + r >> 1;
for(int i = l; i <= r; i++)
if(c[i].opt == 1 && c[i].id <= mid) add(c[i].y, c[i].c);
else if(c[i].opt == 2 && c[i].id > mid) ans[c[i].qid] += c[i].c * sum(c[i].y);
for(int i = l; i <= r; i++)
if(c[i].opt == 1 && c[i].id <= mid) add(c[i].y, -c[i].c);
int h1 = l, h2 = mid;
for(int i = l; i <= r; i++)
if(c[i].id <= mid) tmp[h1++] = c[i];
else tmp[++h2] = c[i];
for(int i = l; i <= r; i++) c[i] = tmp[i];
cdq(l, mid); cdq(mid + 1, r);
}
int main() {
n = iread(); int tot = 0, id = 0;
while(1) {
int opt = iread();
if(opt == 3) break;
if(opt == 1) {
int x = iread(), y = iread(), w = iread();
c[++tot] = (_data){1, tot, 0, x, y, w};
} else {
int x1 = iread(), y1 = iread(), x2 = iread(), y2 = iread(); id++;
c[++tot] = (_data){2, tot, id, x2, y2, 1};
c[++tot] = (_data){2, tot, id, x1 - 1, y2, -1};
c[++tot] = (_data){2, tot, id, x2, y1 - 1, -1};
c[++tot] = (_data){2, tot, id, x1 - 1, y1 - 1, 1};
}
}
sort(c + 1, c + 1 + tot, cmp);
cdq(1, tot);
for(int i = 1; i <= id; i++) printf("%d\n", ans[i]);
return 0;
}
相关文章推荐
- hdu 5324 树套树、cdq分治
- 【Violet3】【BZOJ2716】天使玩偶
- 【BZOJ4311】向量
- 【BeiJing2011】【BZOJ2458】最小三角形
- HDU 5412 CRB and Queries 求区间第k小 CDQ分治+整体二分
- HDU 5324 Boring Class(分治+树状数组)
- 1752. [BOI2007]摩基亚Mokia (cdq分治模板题)
- hdu 5126 stars(三维空间cdq分治)
- cdq分治模板
- BZOJ 3262 陌上花开、HDU 5618 Jam's problem again(三维偏序、cdq分治 + BIT)
- 51nod 1376 最长递增子序列的数量(dp、CDQ分治 | BIT)
- 分治、CDQ分治小结(need to be updated)
- CDQ分治学习
- BZOJ 1176: [Balkan2007]Mokia&&2683: 简单题|cdq分治
- 【分治】 BZOJ 1176 [Balkan2007]Mokia
- [BZOJ2244][SDOI2011]拦截导弹(DP+CDQ分治)
- HDU - 5730 CDQ分治 + FFT
- HDU - 1166 CDQ分治
- cogs 1752. [BOI2007]摩基亚Mokia
- hdu 1166 敌兵布阵(cdq分治)