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<bits/stdc++.h>
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();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值