【hdu 6183】Color it(线段树动态开点)

考虑到颜色只有50种

所以我们开50棵线段树 然后对于每次询问 循环50次对于每一个颜色的线段树询问

那如何查询啊

你考虑到每次询问的最左边都是1

也就是说只需要对于每种颜色的纵坐标 维护往右出现过的点的横坐标的最小值

然后就是区间(y1,y2)查询最小值 判断是否超过了x

但是会MLE啊

所以动态开点 复杂度为nlogn

#include<bits/stdc++.h>
#define N 55 
#define DIS 1000000
#define maxn 1000010
using namespace std;
struct Node
{
    int l,r;
    int lson,rson;
    int sum,minc;
}tree[4*maxn];
int tot,root[N];
bool flag;
void update(int &now,int l,int r,int x,int y)
{
    if(!now)    now=++tot;
    tree[now].l=l;  tree[now].r=r;  tree[now].minc=min(tree[now].minc,x);   tree[now].sum++;
    if(l==r)    return;
    int m=(l+r)>>1;
    if(y<=m)    update(tree[now].lson,l,m,x,y);
    else update(tree[now].rson,m+1,r,x,y);
}
void query(int now,int l,int r,int up)
{
    if(!now||flag)  return;
    if(l<=tree[now].l&&tree[now].r<=r)
    {
        if(tree[now].minc<=up&&tree[now].sum>0) flag=1;
        return;
    }
    int m=(tree[now].l+tree[now].r)>>1;
    if(l<=m) query(tree[now].lson,l,r,up);
    if(r>m) query(tree[now].rson,l,r,up);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int opt;
    for(int i=0;i<maxn*4;i++)   tree[i].minc=DIS+5;
    while(cin>>opt)
    {
        if(opt==3)  return 0;   //exit
        if(opt==0)  //clear
        {
            memset(root,0,sizeof(root));
            for(int i=0;i<=tot;i++)
            {
                tree[i].lson=tree[i].rson=tree[i].l=tree[i].r=tree[i].sum=0;
                tree[i].minc=DIS+5;
            }
            tot=0;
        }
        else if(opt==1)
        {
            int x,y,c;
            cin>>x>>y>>c;
            update(root[c],1,DIS,x,y);
        }
        else if(opt==2)
        {
            int x,y1,y2;
            cin>>x>>y1>>y2;
            int ans=0;
            for(int i=0;i<=50;i++)
            {
                flag=false;
                query(root[i],y1,y2,x);
                if(flag)    ans++;
            }
            cout<<ans<<'\n';
        }
    }
    return 0;
} 

转载于:https://www.cnblogs.com/Patrickpwq/articles/9833666.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值