【分治】 BZOJ 1176 [Balkan2007]Mokia

cdq分治套树状数组。。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 2000005
#define maxm 2000005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct node
{
	int x, y, val, id, pos;
}p[maxn];
int tree[maxn];
int res[maxn];
int n, m, last, t;

void add(int x, int val)
{
	for(int i = x; i <= n; i += lowbit(i)) tree[i] += val;
}

int sum(int x)
{
	int ans = 0;
	for(int i = x; i > 0; i -= lowbit(i)) ans += tree[i];
	return ans;
}

void read(void)
{
	int x1, x2, y1, y2, k;
	m = t = 0;
	while(scanf("%d", &k), k != 3) {
		if(k == 1) {
			m++, p[m].id = 0, p[m].pos = m;
			scanf("%d%d%d", &p[m].x, &p[m].y, &p[m].val);
			p[m].x++, p[m].y++;
		}
		else {
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			x1++, y1++, x2++, y2++;
			p[++m].id = ++t, p[m].val = 1;
			p[m].x = x1 - 1, p[m].y = y1 - 1;
			p[m].pos = m;
			p[++m].id = t, p[m].val = 1;
			p[m].x = x2, p[m].y = y2;
			p[m].pos = m;
			p[++m].id = t, p[m].val = -1;
			p[m].x = x1 - 1, p[m].y = y2;
			p[m].pos = m;
			p[++m].id = t, p[m].val = -1;
			p[m].x = x2, p[m].y = y1 - 1;
			p[m].pos = m;
		}

	}
}

int cmp(node a, node b)
{
	if(a.y != b.y) return a.y < b.y;
	if(a.x != b.x) return a.x < b.x;
	return a.id < b.id;
}

void solve(int L, int R)
{
	if(L == R) return;
	int mid = (L + R) >> 1;
	solve(L, mid);
	solve(mid+1, R);
	sort(p + L, p + R + 1, cmp);
	for(int i = L; i <= R; i++) {
		if(!p[i].id && p[i].pos <= mid) add(p[i].x, p[i].val);
		else if(p[i].id && p[i].pos > mid) res[p[i].id] += p[i].val * sum(p[i].x);
	}
	for(int i = L; i <= R; i++) if(!p[i].id && p[i].pos <= mid) add(p[i].x, -p[i].val);
}

void work(void)
{
	for(int i = 1; i <= t; i++) res[i] = 0;
	solve(1, m);
	for(int i = 1; i <= t; i++) printf("%d\n", res[i] + last);
}

int main(void)
{
	while(scanf("%d%d", &last, &n)!=EOF) {
		n++;
		read();
		work();
	}


	return 0;
}


你可能感兴趣的:(cdq分治)