Color it

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)

Problem Description

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.

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≤50 ), ‘2 x y1 y2’ (1≤x,y1,y2≤106 ) or ‘3’.

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

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

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

There are at most 10 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

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

Recommend

liuyiding | We have carefully selected several similar problems for you: 6297 6296 6295 6294 6293

这是一道经典的线段树简单题,对于操作0,直接手动清零(你可以直接当做这道题有多组数据来看),然后就是相对来说思维难度略高一点的另外两个操作(操作3略)。

先思考一个问题,这道题如何维护答案?

观察数据范围可以发现,c" role="presentation" style="position: relative;">cc的取值范围很小,只有[0,50]" role="presentation" style="position: relative;">[0,50][0,50],这不禁让我们想到对于每一种颜色建一颗线段树,然后每次询问时我们跑一个O(50logn)" role="presentation" style="position: relative;">O(50logn)O(50logn)来查询,看看时限和数据范围应该没啥问题。所以怎么维护这种信息呢?

我们知道,所有点的x" role="presentation" style="position: relative;">xx坐标都满足1≤xi" role="presentation" style="position: relative;">1≤xi1≤xi,这样的话,我们可以维护每一个y" role="presentation" style="position: relative;">yy坐标上所对应的x" role="presentation" style="position: relative;">xx坐标的最小值。然后查询时就看在当前y" role="presentation" style="position: relative;">yy坐标范围内的最小值是否小于x" role="presentation" style="position: relative;">xx就行了。

还剩了一个问题:空间的处理。这个可以使用动态开点的方法,这样你实际使用的空间只与修改次数有关。(不过我每次写动态开点的问题感觉线段树格式写的都很丑)

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define Node 2500010
#define n 1000005
#define inf 0x3f3f3f3f
using namespace std;
int rt[60],son[Node<<2][2],minn[Node<<2],cnt=0;
inline void init(){
    memset(rt,0,sizeof(rt));
    cnt=0;
    son[0][1]=son[0][0]=0;
    minn[0]=inf;
}
inline void pushup(int p){minn[p]=min(minn[son[p][0]],minn[son[p][1]]);}
inline void update(int &p,int l,int r,int y,int x){
    if(!p){
        p=++cnt;
        son[p][0]=son[p][1]=0;
        minn[p]=x;
    }
    if(l==r){
        minn[p]=min(minn[p],x);
        return;
    }
    int mid=l+r>>1;
    if(y<=mid)update(son[p][0],l,mid,y,x);
    else update(son[p][1],mid+1,r,y,x);
    pushup(p);
}
inline int query(int p,int l,int r,int ql,int qr){
    if(!p)return inf;
    if(ql<=l&&r<=qr)return minn[p];
    int mid=l+r>>1;
    if(qr<=mid)return query(son[p][0],l,mid,ql,qr);
    if(ql>mid)return query(son[p][1],mid+1,r,ql,qr);
    return min(query(son[p][0],l,mid,ql,mid),query(son[p][1],mid+1,r,mid+1,qr));
}
int main(){
    init();
    int op;
    while(~scanf("%d",&op)){
        if(op==3)break;
        switch(op){
            case 0:{
                init();
                break;
            }
            case 1:{
                int qx,qy,c;
                scanf("%d%d%d",&qx,&qy,&c);
                update(rt[c],1,n,qy,qx);
                break;
            }
            default:{
                int x,ql,qr,ans=0;
                scanf("%d%d%d",&x,&ql,&qr);
                for(int i=0;i<=50;++i)ans+=(query(rt[i],1,n,ql,qr)<=x);
                printf("%d\n",ans);
                break;
            }
        }
    }
}

2018.07.08 hdu6183 Color it(线段树)的更多相关文章

  1. 2018.07.08 hdu5316 Magician(线段树)

    Magician Problem Description Fantasy magicians usually gain their ability through one of three usual ...

  2. hdu6183 Color it 线段树动态开点+查询减枝

    题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...

  3. 2018.07.31cogs2964. 数列操作η(线段树)

    传送门 线段树基本操作. 给出一个排列b,有一个初始值都为0的数组a,维护区间加1,区间统计区间∑(ai/bi)" role="presentation" style=& ...

  4. 2018.07.25 hdu5306Gorgeous Sequence(线段树)

    传送门 线段树基本操作. 要求维护区间取min" role="presentation" style="position: relative;"> ...

  5. 2018.07.23[PA2015]Siano(线段树)

    [PA2015]Siano 描述 Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘 ...

  6. HDU6183 Color it (线段树动态开点)

    题意: 一个1e6*1e6的棋盘,有两个操作:给(x,y)加上颜色c,或查找(1,y1)到(x,y2)内的颜色种类数量,最多有50种颜色 思路: 建立50颗线段树,对每个颜色的线段树,维护每个y坐标上 ...

  7. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  8. Count Color poj2777 线段树

    Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...

  9. 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...

随机推荐

  1. DEMO: springboot 与 freemarker 集成

    直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...

  2. Centos LVM 创建 删除 扩大 缩小

    新建LVM的过程1.使用fdisk 新建分区 修改ID为8e3.使用 pvcreate 创建 PV 4.使用 vgcreate 创建 VG 5.使用 lvcreate 创建 LV 6.格式化LV7.挂 ...

  3. xe fmx 怎么改变button颜色

    xe fmx 怎么改变button颜色 改变照相机的默认像素CameraComponent1

  4. spring cloud 消费者

    本消费者  加了 Hystrix, 为了后续监控用. 1. 依赖: <parent> <groupId>org.springframework.boot</groupId ...

  5. Javascript系列:总体理解

    js是一个脚本客户端(浏览器)语言.和sql html类似.多练习. 没有排错的经验,弱类型语言,浏览器解释执行,出错也不会报错 预备 <!DOCTYPE html PUBLIC "- ...

  6. Winform 无纸化办公-屏幕联动

    最近做无纸化办公,对接硬件,用了挺多东西总结一下 技术上主要是:asp.net .winform.activeX控件.chrome插件.socket编程,websocket. 其实看着需求挺简单的,在 ...

  7. shell 一次移动很多个命名相似的文件

    文件夹下面有很多类似下面命名的文件 aaaaaa01bbb aaaaaa01cc aaaaaa01dd aaaaaa02bbb aaaaaa02cc 要把 aaaaaa01 的文件移走 用 mv  / ...

  8. Promise/Deferred

    [fydisk] 1.$.get('/api').success(onSuccess).error(onError).comlete(onComplete); 2.对同一事件加入多个Handler. ...

  9. java基础五 [数字与静态](阅读Head First Java记录)

    本章主要讲了静态变量.静态方法,final关键词.以及介绍了怎么对数字和日期进行格式化输出.这里对这些内容进行了整理.本章还介绍了java.util.Date和java.util.Calendar来操 ...

  10. chrome input去除黄色背景色

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; border: 1px solid #CCC!impo ...