HDU - 6183 Color it (动态开点权值线段树)

Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The specific format of these operations is as follows. 

00 : clear all the points. 

11 xx yy cc : add a point which color is cc at point (x,y)(x,y). 

22 xx y1y1 y2y2 : count how many different colors in the square (1,y1)(1,y1) and (x,y2)(x,y2). That is to say, if there is a point (a,b)(a,b) colored cc, that 1≤a≤x1≤a≤x and y1≤b≤y2y1≤b≤y2, then the color cc should be counted. 

33 : exit. 

Input

The input contains many lines. 

Each line contains a operation. It may be '0', '1 x y c' ( 1≤x,y≤106,0≤c≤501≤x,y≤106,0≤c≤50 ), '2 x y1 y2' (1≤x,y1,y2≤1061≤x,y1,y2≤106 ) or '3'. 

x,y,c,y1,y2x,y,c,y1,y2 are all integers. 

Assume the last operation is 3 and it appears only once. 

There are at most 150000150000 continuous operations of operation 1 and operation 2. 

There are at most 1010 operation 0. 
 

Output

For each operation 2, output an integer means the answer . 

Sample Input

0
1 1000000 1000000 50
1 1000000 999999 0
1 1000000 999999 0
1 1000000 1000000 49
2 1000000 1000000 1000000
2 1000000 1 1000000
0
1 1 1 1
2 1 1 2
1 1 2 2
2 1 1 2
1 2 2 2
2 1 1 2
1 2 1 3
2 2 1 2
2 10 1 2
2 10 2 2
0
1 1 1 1
2 1 1 1
1 1 2 1
2 1 1 2
1 2 2 1
2 1 1 2
1 2 1 1
2 2 1 2
2 10 1 2
2 10 2 2
3

Sample Output

2
3
1
2
2
3
3
1
1
1
1
1
1
1

Sponsor

以颜色为根 开权值线段树 由于数据过大用到动态开点 也可以离线离散化

#include
using namespace std;
#define ll long long
const int maxn = 2e6 + 7;
const int inf = 1e6 + 5;

using namespace std;

struct node{
    int l;
    int r;
    int v;
}tree[maxn << 1];
int rt[55];
int tot;
int f;
void update(int &rot, int l, int r, int pos, int val){
    if(!rot){
        rot = ++tot;
        tree[rot].v = val;
    }
    tree[rot].v = min(tree[rot].v, val);
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    if(pos <= mid){
        update(tree[rot].l, l, mid, pos, val);
    }
    else{
        update(tree[rot].r, mid + 1, r, pos, val);
    }
}

void query(int rot, int val, int x, int y, int l, int r){
    if(f || !rot){
        return;
    }
    if(l >= x && r <= y){
        if(tree[rot].v <= val){
            f = 1;
        }
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid){
        query(tree[rot].l, val, x, y, l, mid);
    }
    if(y > mid){
        query(tree[rot].r, val, x, y, mid + 1, r);
    }
}
void solve(){
    int opt;
    while(cin >> opt){
        if(opt == 3){
            return ;
        }
        if(opt == 0){
            for(int i = 1; i <= tot; i++){
                tree[i].l = tree[i].r = tree[i].v = 0;
            }
            for(int i = 0; i <= 50; i++){
                rt[i] = 0;
            }
            tot = 0;
        }
        if(opt == 1){
            int x, y, c;
            cin >> x >> y >> c;
            update(rt[c], 1, inf, y, x);
        }
        if(opt == 2){
            int ans = 0;
            int x, y1, y2;
            cin >> x >> y1 >> y2;
            for(int i = 0; i <= 50; i++){
                f = 0;
                query(rt[i], x, y1, y2, 1, inf);
                if(f)
                    ans++;
            }
            cout << ans << endl;
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    t = 1;
    while(t--){
        solve();
    }
}

 

你可能感兴趣的:(线段树,主席树)