Paint The Wall

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3114    Accepted Submission(s): 846


Problem Description
As a amateur artist, Xenocide loves painting the wall. The wall can be considered as a line consisting of n nodes. Each node has its own color.

Xenocide spends all day in front of the wall. Sometimes, he paints some consecutive nodes so that these nodes have the same color. When he feels tired, he focuses on a particular color and counts the number of nodes that have this color within a given interval.

Now Xenocide is tired of counting, so he turns to you for help.

 
Input
The input consists of several test cases.
The first line of each test case contains two integer n, m(1<=n, m<=100000) indicating the length of the wall and the number of queries.
The following line contains N integers which describe the original color of every position.
Then m lines follow. Each line contains 4 non-negative integers a, l, r, z(1<=a<=2, 0<=l<=r<n ,0<=z<231).
a = 1 indicates that Xenocide paints nodes between l and r and the resulting color is z.
a = 2 indicates that Xenocide wants to know how many nodes between l and r have the color z.
 
Output
Print the corresponding answer for each queries.
 
Sample Input
5 5
1 2 3 4 0
2 1 3 3
1 1 3 1
2 1 3 3
2 0 3 1
2 3 4 1
 
Sample Output
1
0
4
1
剪枝线段树。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node{
int col,mx,mn;//col==-1 表示该区间为杂色
int l,r;
}a[MAXN*];
void pushUp(int rt)
{
if(a[rt<<].col==a[(rt<<)|].col)
a[rt].col=a[rt<<].col;
else
a[rt].col=-;
a[rt].mn=min(a[rt<<].mn,a[(rt<<)|].mn);
a[rt].mx=max(a[rt<<].mx,a[(rt<<)|].mx);
}
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
scanf("%d",&a[rt].col);
a[rt].mn=a[rt].col;
a[rt].mx=a[rt].col;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
pushUp(rt);
}
void pushDown(int rt)
{
if(a[rt].col!=-)
{
a[rt<<].col=a[(rt<<)|].col=a[rt].col;
a[rt<<].mn=a[(rt<<)|].mn=a[rt].col;
a[rt<<].mx=a[(rt<<)|].mx=a[rt].col;
}
}
void update(int rt,int l,int r,int v)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].col=v;
a[rt].mn=a[rt].mx=v;
return ;
}
pushDown(rt);
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
update(rt<<,l,r,v);
else if(mid<l)
update((rt<<)|,l,r,v);
else
{
update(rt<<,l,mid,v);
update((rt<<)|,mid+,r,v);
}
pushUp(rt);
}
int query(int rt,int l,int r,int v)
{
if(v<a[rt].mn||v>a[rt].mx) return ;
if(a[rt].l==l&&a[rt].r==r)
{
if(a[rt].col==v) return r-l+;
}
if(a[rt].l==a[rt].r)
{
if(a[rt].col==v) return ;
else return ;
}
pushDown(rt);
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
return query(rt<<,l,r,v);
else if(mid<l)
return query((rt<<)|,l,r,v);
else
return query(rt<<,l,mid,v)+query((rt<<)|,mid+,r,v);
}
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,,n);
while(m--)
{
int op,x,y,z;
scanf("%d%d%d%d",&op,&x,&y,&z);
x++,y++;
if(op==)
update(,x,y,z);
else
printf("%d\n",query(,x,y,z));
} } return ;
}

HDU4391(线段树+剪枝)的更多相关文章

  1. 对权值线段树剪枝的误解--以HDU6703为例

    引子 对hdu6703,首先将问题转化为"询问一个排列中大于等于k的值里,下标超过r的最小权值是多少" 我们采用官方题解中的做法:权值线段树+剪枝 对(a[i],i)建线段树,查询 ...

  2. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  3. HDOJ:6356-Glad You Came(线段树剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356 解题心得: 现在深深的知道了算法复杂度的重要了,这个题算复杂度的时候还要把一些常数也算出来,不然 ...

  4. LibreOJ #6190. 序列查询(线段树+剪枝)

    莫队貌似是过不了的,这题是我没见过的科技... 首先区间按右端点排序,然后一个扫描线,扫到某个区间右端点时候计算答案,线段树上节点的信息并不需要明确定义,我们只要求线段树做到当前扫到now时,查询[L ...

  5. hdu4027-Can you answer these queries? -(线段树+剪枝)

    题意:给n个数,m个操作,分两种操作,一种是将一段区间的每个数都开根号,另一种是查询区间和. 解题:显然对每个数开根号不能用lazy的区间更新.一个一个更新必然爆时间,对1开根号还是1,如果一段区间都 ...

  6. HDU 5828 Rikka with Sequence (线段树+剪枝优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...

  7. CodeForces 438D 线段树 剪枝

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  8. hdu 4027 Can you answer these queries? 线段树

    线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...

  9. BZOJ 4556(后缀数组+主席树求前驱后继+二分||后缀数组+二分+可持久化线段树)

    换markdown写了.. 题意: 给你一个1e5的字符串,1e5组询问,求\([l_1,r_1]\)的所有子串与\([l_2,r_2]\)的lcp 思路: 首先可以发现答案是具有单调性的,我们考虑二 ...

随机推荐

  1. requestWindowFeature使用详解

    requestWindowFeature可以设置的值有: // 1.DEFAULT_FEATURES:系统默认状态,一般不需要指定        // 2.FEATURE_CONTEXT_MENU:启 ...

  2. Delphi下如何使程序在Win7/Vista上用管理员权限运行(转)

    Delphi程序必须在资源里面嵌入MANIFEST信息 一 首先编辑一个文件,内容如下: <?xml version="1.0" encoding="UTF-8&q ...

  3. Dubbo--简单介绍

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,能够和Spring框架无缝集成.Dubbo致力于提供高性能和透明化的RPC远程服务调用 ...

  4. Erlang节点重启导致的incarnation问题(转)

    转自霸爷的博客: 转载自系统技术非业余研究 本文链接地址: Erlang节点重启导致的incarnation问题 遇到个问题, =ERROR REPORT==== 10-Mar-2016::09:44 ...

  5. nodejs实如今线群聊

    这不是一个项目而是一个适合刚開始学习的人学习的样例.主要实现了下面基本功能: 1:群聊.每个人都能够收到其它人的消息,以及能够发消息给其它人,每个人用ip地址标识. 2:显示当前在线用户. 3:每个用 ...

  6. C# xml读取操作

    以下xml: <Project> <ProjectMains> <ProjectMain Action="added"> <Project ...

  7. Android 关于软键盘

    一..弹出的时候显示Editext框 添加布局replay_input <?xml version="1.0" encoding="utf-8"?> ...

  8. 再看GS接包过程

    再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...

  9. 如何进行Web服务的性能测试?

    随着浏览器功能的不断完善,用户量不断的攀升,涉及到web服务的功能在不断的增加,对于我们测试来说,我们不仅要保证服务端功能的正确性,也要验证服务端程序的性能是否符合要求.那么性能测试都要做些什么呢?我 ...

  10. android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例

    有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果. 以下主要参考<第一行代码> 1.自定义一个标题栏: 系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在o ...