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. Android----SharedPreferences(存储数据)

    SharedPreferences详解 我们在开发软件的时候,常需要向用户提供软件参数设置功能,例如我们常用的微信,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是在windo ...

  2. 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)

    ...... 承接上一系列动画三. 在主界面后台代码设置嵌套第二个用户控件. using System; using System.Collections.Generic; using System. ...

  3. mongodb的启动参数--quiet

    ”mongo群友在群里问了个问题,问的是--quiet启动参数如何用的? 如何理解安静的输出?“ 看到这个问题,之前看过--quiet这个参数,没有认真研究过,也没在生产中使用过. 在mongodb启 ...

  4. Linux NAT网络连接权威指南

    [1]准备工作,写在前面 1.1)检查服务(cmd>>services.msc,我用的是VM) 1.2)确保Vmnet8 连接处于启动状态 + 获取ipv4(ipv6)地址 (在网络连接不 ...

  5. 多媒体开发之---live555的多线程支持,原本只是单线程,单通道

    1)我对Live555进行了一次封装,但是Live555 是单线程的,里面定义的全局变量太多,我封装好dll库后,在客户端调用,因为多个对话框中要使用码流,我就定义了多个对象从设备端接收码流,建立多个 ...

  6. C++复习:位运算

    与          a&b :    1010&1100=1000  或          a|b  :  1010|1100=1110 异或       a^b :     101 ...

  7. HashMap与 HashTable, Treemap的区别

    (一)HashMap 1.HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null; 2.HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数 ...

  8. TP框架---thinkphp模型

    1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...

  9. Java 学习 day02

    1. 标识符 2. 常量 3. 进制的特点 4. 进制转换(十进制和二进制) 5. 进制转换(八进制和十六进制) 6. 进制转换(负数二进制) 7. 变量 8. 类型转换 9. 算术运算符 10. 转 ...

  10. CentOS6.5安装MySQL5.6 过程记录

    刚开始,还不太懂,直接上了MySQL5.7版本的二进制安装,结果遇到了各种问题,从5.6到5.7还是做了很大改变的,比如mysql_install_db的文件位置变更到了/bin文件下等等,觉得现在用 ...