【bzoj2683】 【bzoj1176】 分治

将询问排序后按x轴分治。
这个题解的程序似乎是错的?
http://blog.csdn.net/wzq_QwQ/article/details/46998215

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define maxn 2000005

inline int rd() {
    char c = getchar();
    while (!isdigit(c)) c = getchar() ; int x = c - '0';
    while (isdigit(c = getchar())) x = x * 10 + c - '0';
    return x;
}

typedef int ll;

struct node {
    int id , x , y , v , t;
    node() {}
    node(int id , int x , int y , int v , int t):id(id) , x(x) , y(y) , v(v) , t(t) { }
    bool operator<(const node a) const {
        if (x == a.x && y == a.y) return t < a.t;
        if (x == a.x) return y < a.y;
        return x < a.x;
    }
}q[maxn] , tmp[maxn];

namespace BITS {
    ll val[maxn];
    int n;

    #define lowbit(x) (x & (-x))

    void init(int _n) { n = _n ; }

    void add(int x , int v) {
        while (x <= n)
            val[x] += v , x += lowbit(x);
    }

    ll get(int x) {
        ll ret = 0;
        while (x)
            ret += val[x] , x -= lowbit(x);
        return ret;
    }
}

int tot , n , ques;
ll ans[maxn];

inline void ins(int x , int y , int v , int t) {
    ++ tot;
    q[tot] = node(tot , x , y , v , t);
}

void input() {
    n = rd();
    int t;
    while (scanf("%d" , &t) && t != 3) {
        int x1 = rd() , y1 = rd() , x2 = rd() , y2;
        if (t == 1) {
            ins(x1 , y1 , x2 , 0);
        } else {
            ++ ques , y2 = rd();
            ins(x1 - 1 , y1 - 1 , 1 , ques);
            ins(x1 - 1 , y2 , -1 , ques);
            ins(x2 , y1 - 1 , -1 , ques);
            ins(x2 , y2 , 1 , ques);
        }
    }
}

void merge(int l , int r) {
    if (l == r) return ;
    int m = (l + r) >> 1;
    rep (i , l , r) {
        if (q[i].id <= m && !q[i].t) BITS::add(q[i].y , q[i].v);
        if (q[i].id >  m &&  q[i].t) ans[q[i].t] += BITS::get(q[i].y) * q[i].v;
    }
    rep (i , l , r) if (q[i].id <= m && !q[i].t) BITS::add(q[i].y , -q[i].v);
    int l1 = l , l2 = m + 1;
    rep (i , l , r) if (q[i].id <= m) tmp[l1 ++] = q[i] ; else tmp[l2 ++] = q[i];
    rep (i , l , r) q[i] = tmp[i];
    merge(l , m) , merge(m + 1 , r);
}

void solve() {
    BITS::init(n);
    sort(q + 1 , q + tot + 1);
    merge(1 , tot);
    rep (i , 1 , ques) printf("%d\n" , ans[i]);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.txt" , "r" , stdin);
        freopen("data.out" , "w" , stdout);
    #endif
    input();
    solve();
    return 0;
}

你可能感兴趣的:(【bzoj2683】 【bzoj1176】 分治)