您的位置:首页 > 其它

bzoj1176: [Balkan2007]Mokia【cdq分治】

2015-12-23 12:44 513 查看
  把询问搞成4个,cdq分治。

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a;i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> 1;
const i64 INF = ~0ULL >> 1;
//**********************************

const int maxn = 200005;

int c[2000005], w;
struct Complex {
int flag;
int x, y, c;
int id, ans;
int pos, l;
inline bool operator < (const Complex &a) const {
return x < a.x ||
x == a.x && y < a.y ||
x == a.x && y == a.y && c < a.c;
}
} src[maxn], t[maxn];

inline void add(int x, int v) {
while (x <= w) {
c[x] += v;
x += x & -x;
}
}
inline int get(int x) {
int ret(0);
while (x > 0) {
ret += c[x];
x -= x & -x;
}
return ret;
}
int ans[10005];

void cdq(int l, int r) {
if (l == r) return;
int mid = l + r >> 1, l1 = l, l2 = mid + 1;
rep(i, l, r) {
if (src[i].id <= mid && !src[i].l) add(src[i].y, src[i].c);
if (src[i].id > mid && src[i].l) ans[src[i].pos] += src[i].l * get(src[i].y);
}
rep(i, l, r) if (src[i].id <= mid && !src[i].l) add(src[i].y, -src[i].c);
rep(i, l, r) if (src[i].id <= mid) t[l1++] = src[i]; else t[l2++] = src[i];
memcpy(src + l, t + l, (r - l + 1) * sizeof(Complex));
cdq(l, mid); cdq(mid + 1, r);
}

int main() {
int cnt(0), n(0), s;
scanf("%d%d", &s, &w);
int flag;
while (scanf("%d", &flag), flag ^ 3) {
if (flag == 1) {
++n;
src
.id = n; src
.l = 0; src
.pos = 0;
scanf("%d%d%d", &src
.x, &src
.y, &src
.c);
}
else {
int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b);

ans[++cnt] = s * (a - x) * (b - y);

++n;
src
.id = n; src
.l = 1; src
.pos = cnt;
src
.x = a, src
.y = b, src
.c = inf;

++n;
src
.id = n; src
.l = -1; src
.pos = cnt;
src
.x = a, src
.y = y - 1, src
.c = inf;

++n;
src
.id = n; src
.l = -1; src
.pos = cnt;
src
.x = x - 1, src
.y = b, src
.c = inf;

++n;
src
.id = n; src
.l = 1; src
.pos = cnt;
src
.x = x - 1, src
.y = y - 1, src
.c = inf;
}
}
sort(src + 1, src + n + 1);
cdq(1, n);
rep(i, 1, cnt) printf("%d\n", ans[i]);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: