hdu 6183 【线段树的巧妙】

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.

0 : clear all the points.

1 x y c : add a point which color is c at point (x,y).

2 x y1 y2 : count how many different colors in the square (1,y1) and (x,y2). That is to say, if there is a point (a,b) colored c, that 1≤a≤x and y1≤b≤y2, then the color c should be counted.

3 : exit.

/*
算法和代码技巧要精 才能过啊!
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf = 1e9;
const int maxn = 1000000;
typedef long long ll;
int n,root[55],tot;
int tree[maxn*4],lc[maxn+5],rc[maxn+5];// 我们开不下 50 * maxn那么大的数组, 但是对于这个问题来说我们不需要那么大的空间。 因为我们不需要用得到一颗完整的线段树
void Clear()
{
    memset(root,0,sizeof(root));
    for(int i=0; i<=tot; i++) tree[i] = lc[i] = rc[i] = 0;
    tot = 0;
}
void update(int& v,int l,int r,int p,int x)
{
    if(!v) v = ++tot, tree[v] = x;//对于没有第一次出现的点 直接赋值就行
    if(tree[v]>x) tree[v] = x;
    if(l==r) return;
    int mid = (l+r)>>1;
    if(mid>=p) update(lc[v],l,mid,p,x);
    if(mid<p) update(rc[v],mid+1,r,p,x);
}
bool query(int v,int l,int r,int L,int R,int x)
{
    if(!v) return false;
    if(L<=l&&r<=R)// 感受一下致命的超时写法: if(L<=l&&r<=R&&tree[v]<=x) return true; 这是对线段树的理解运用还不够深啊!
    {
        if(tree[v]<=x) return true;
        return false;
    }
    int mid = (l+r)>>1;
    if(mid>=L) if(query(lc[v],l,mid,L,R,x)) return true;
    if(mid<R) if(query(rc[v],mid+1,r,L,R,x)) return true;
    return false;
}
int solve(int y1,int y2,int x)
{
    int cot = 0;
    for(int i=0; i<=50; i++) if(query(root[i],1,maxn,y1,y2,x)) cot++;
    return cot;
}
int main()
{
    int t,x,y,c,y1,y2;
    while(scanf("%d",&t)&&t!=3)
    {
        if(t==0) Clear();
        if(t==1)
        {
            scanf("%d %d %d",&x,&y,&c);
            update(root[c],1,maxn,y,x);
        }
        if(t==2)
        {
            scanf("%d %d %d",&x,&y1,&y2);
            printf("%d\n",solve(y1,y2,x));
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值