hdu6183 Color it 线段树动态开点+查询减枝
题目大意:
有多次操作。操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序。
思路:
由于查询的矩形是贴着y轴的,所以以y轴为线段树节点,建立52颗线段树,然后每个节点都保存这个纵坐标下x的最小值,然后查询。
这样的线段树显然是开不下的,所以我们考虑线段树动态开点,但是发现有50颗,如果按照50*n*logn的查询,还是会TLE,这里需要一个减枝,就是如果一部分区间内已经有一个颜色了,就直接退出所有查询即可,否则会TLE(卡常数?)
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=;
int rt[],tot;
int R[maxn*],L[maxn*],v[maxn*],flag;
int op,x,y,y1,y2,co;
void init(){
tot=,clr(rt,);
}
void update(int &o,int l,int r,int y,int x){
if(!o){
L[o=++tot]=,R[o]=;
v[o]=x;
}
v[o]=min(v[o],x);
if(l==r)return;
int mid=(l+r)>>;
if(y<=mid)update(L[o],l,mid,y,x);
else update(R[o],mid+,r,y,x);
}
void query(int o,int l,int r,int ql,int qr){
if(flag||!o){
return ;
}
if(ql<=l&&qr>=r)
{
if(v[o]<=x)flag=;
return;
}
int mid=(l+r)>>; if(ql<=mid)query(L[o],l,mid,ql,qr);
if(qr>mid)query(R[o],mid+,r,ql,qr);
return ;
}
int main(){
// freopen("simple.in","r",stdin);
int n=1e6;
while(scanf("%d",&op)!=EOF){
if(op==)break;
else if(op==){
init();
}else if(op==){
scanf("%d%d%d",&x,&y,&co);
update(rt[co],,n,y,x);
}else{
scanf("%d%d%d",&x,&y1,&y2);
int ans=;
for(int i=;i<=;i++)
{
flag=;
query(rt[i],,n,y1,y2);
ans+=flag;
}
printf("%d\n",ans);
}
}
}
Color it
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 2327 Accepted Submission(s): 703
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.
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.
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
3
1
2
2
3
3
1
1
1
1
1
1
1
hdu6183 Color it 线段树动态开点+查询减枝的更多相关文章
- HDU6183 Color it (线段树动态开点)
题意: 一个1e6*1e6的棋盘,有两个操作:给(x,y)加上颜色c,或查找(1,y1)到(x,y2)内的颜色种类数量,最多有50种颜色 思路: 建立50颗线段树,对每个颜色的线段树,维护每个y坐标上 ...
- HDU - 6183 暴力,线段树动态开点,cdq分治
B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...
- BZOJ_4636_蒟蒻的数列_线段树+动态开点
BZOJ_4636_蒟蒻的数列_线段树+动态开点 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将 ...
- P3939 数颜色 线段树动态开点
P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...
- 洛谷P3313 [SDOI2014]旅行 题解 树链剖分+线段树动态开点
题目链接:https://www.luogu.org/problem/P3313 这道题目就是树链剖分+线段树动态开点. 然后做这道题目之前我们先来看一道不考虑树链剖分之后完全相同的线段树动态开点的题 ...
- codedecision P1113 同颜色询问 题解 线段树动态开点
题目描述:https://www.cnblogs.com/problems/p/11789930.html 题目链接:http://codedecision.com/problem/1113 这道题目 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- hdu 6183 Color it (线段树 动态开点)
Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B ...
- HDU - 6183:Color it (线段树&动态开点||CDQ分治)
Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B ...
随机推荐
- angular.js简单入门。
小弟刚接触angular js 就写了一个简单的入门.后续慢慢补... 首先看 html 页面. <html> <meta charset="UTF-8"> ...
- 项目代码:js
1 //获取发文时间 function selectWriteTime(){ $("#writing_time_index").on("click"," ...
- 20-取石子动态规则(hdu2516 斐波那契博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=2516 取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memor ...
- js颜色拾取器
几年前,很难找到一个合适的颜色选择器.正好看到很多不错的JavaScript颜色选择器插件,故而把这些编译汇总.在本文,Web设计师和开发人员 Kevin Liew 选取了11个相应插件,有些会比较复 ...
- algorithm notes
1.算法可视化 https://visualgo.net/en
- Gnu C API使用指南
1)posix_fadvise http://blog.yufeng.info/archives/1917 2)fts系列 http://www.cnblogs.com/patientAndPersi ...
- NUMA微架构
NUMA微架构 written by qingran September 8th, 2011 no comment 现在开始补日志,逐步的扫清以前写了一半的和"欠账未还的".半年之 ...
- python3--列表生成式
# Auther: Aaron Fan # 原始的写法:a = []for i in range(10): a.append(i*2)print(a) # 用列表生成式完成上面的写法:a = [i*2 ...
- iPhone的home键进果汁了,按起来粘粘的感觉
解决办法是按住home键转动一下,再用棉签蘸点水或者酒精都行(注意:水不要太多,不能让水渗进去),用棉签按压home 键多转几圈就好了.
- DFS小题
原创 题目为:()()()+()()()=()()() 将1~9这9个数字填入括号,每个数字只能用一次. 枚举: public class Test { public static void main ...