【线段树】hdu6183 Color it
题意:
维护一个数据结构,支持三种操作:
①在平面上(x,y)处添加一个颜色为c的点。
②询问平面上(1,y1)-(x,y2)范围内,有多少种不同颜色的点。
③清除平面上所有点。
颜色数量很少,对于每种颜色分别建立线段树,然后用线段树维护y坐标,对每个y坐标只存下来x坐标最小的点的x坐标,然后每次询问相当于问你[y1,y2]区间的最小值是否小于等于x。
卡常数,必须用动态开点线段树,并且在询问的时候进行剪枝(在区间内如果遇到了满足条件的子区间,直接返回true,不继续检查其他子区间了)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=2147483647;
int root[55],lc[4000005],rc[4000005],v[4000005];
int tot;
void newnode(int &rt){
rt=++tot;
lc[rt]=rc[rt]=0;
v[rt]=INF;
}
void update(int p,int x,int &rt,int l,int r){
if(!rt){
newnode(rt);
}
if(l==r){
v[rt]=min(v[rt],x);
return;
}
int m=(l+r>>1);
if(p<=m){
update(p,x,lc[rt],l,m);
}
else{
update(p,x,rc[rt],m+1,r);
}
v[rt]=min(v[lc[rt]],v[rc[rt]]);
}
bool query(int x,int ql,int qr,int rt,int l,int r)
{
if(!rt){
return 0;
}
if(ql<=l && r<=qr){
if(v[rt]<=x){
return 1;
}
return 0;
}
int m=(l+r>>1);
if(ql<=m){
if(query(x,ql,qr,lc[rt],l,m)){
return 1;
}
}
if(m<qr){
if(query(x,ql,qr,rc[rt],m+1,r)){
return 1;
}
}
return 0;
}
int main()
{
// freopen("b.in","r",stdin);
int op,x,y,z;
v[0]=INF;
while(1){
scanf("%d",&op);
if(op==3){
break;
}
if(op==0){
memset(root,0,sizeof(root));
tot=0;
}
else if(op==1){
scanf("%d%d%d",&x,&y,&z);
update(y,x,root[z],1,1000000);
}
else if(op==2){
scanf("%d%d%d",&x,&y,&z);
int ans=0;
for(int i=0;i<=50;++i){
ans+=query(x,y,z,root[i],1,1000000);
}
printf("%d\n",ans);
}
}
return 0;
}
【线段树】hdu6183 Color it的更多相关文章
- Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树) Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 2018.07.08 hdu6183 Color it(线段树)
Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- hdu6183 Color it 线段树动态开点+查询减枝
题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...
- HDU6183 Color it (线段树动态开点)
题意: 一个1e6*1e6的棋盘,有两个操作:给(x,y)加上颜色c,或查找(1,y1)到(x,y2)内的颜色种类数量,最多有50种颜色 思路: 建立50颗线段树,对每个颜色的线段树,维护每个y坐标上 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
随机推荐
- 【洛谷 P2303】 [SDOi2012]Longge的问题 (欧拉函数)
题目链接 题意:求\(\sum_{i=1}^{n}\gcd(i,n)\) 首先可以肯定,\(\gcd(i,n)|n\). 所以设\(t(x)\)表示\(gcd(i,n)=x\)的\(i\)的个数. 那 ...
- 2009 Round2 A Crazy Rows (模拟)
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- oracle 的number数据类型
NUMBER类型细讲:Oracle number datatype 语法:NUMBER[(precision [, scale])]简称:precision --> p scale ...
- 8.0docker的客户端和守护进程
C/S 链接的方式 unix:///var/run/docker.sock 默认的 tcp://host:port fd://socketfd 在linux上进行socket 模拟 nc -U /va ...
- Django rest framework 限制访问频率(源码分析)
基于 http://www.cnblogs.com/ctztake/p/8419059.html 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处 ...
- 在Perl中采用open进行管道操作
在Perl中采用open进行管道操作 http://blog.sina.com.cn/s/blog_4840fe2a0100b8na.html perl exec管道和子进程 http://blog. ...
- sicily 1046. Plane Spotting
1046. Plane Spotting Time Limit: 1sec Memory Limit:32MB Description Craig is fond of planes. Mak ...
- ==和equals()方法的区别
==和equals()方法的区别 这是一道经典的面试题,但是很多人对其一直很困惑,最近刚好复习了他们两者的区别,现总结如下: 一.==:两端可以存放不同的数据 1.放基本数据类型:根据基本数据 ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- 《深入浅出MyBatis技术原理与实战》——6. MyBatis的解析和运行原理
MyBatis的运行分为两大部分,第一部分是读取配置文件缓存到Configuration对象,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程. 6.1 涉及的技术 ...