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. [jjzhu学java]之solr4.9同步mysql数据

    Solr是一个高性能,採用Java5开发,基于Lucene的全文搜索server.同一时候对其进行了扩展,提供了比Lucene更为丰富的查询语言,同一时候实现了可配置.可扩展并对查询性能进行了优化,而 ...

  2. 讲真,你是因为什么才买华为P20系列手机!

    华为P20系列手机上市两个半月发货600万台!600万台?!看到这个亮瞎我钛合金狗眼的数据,且容我掰着手指脚趾算一下,算了,还是容我毫不夸张的感叹一句吧:华为做手机不用桨,不需风,全靠“浪”……. 两 ...

  3. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  4. java设计模式之迭代器模式

    一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...

  5. 我的Java开发学习之旅------>求字符串中出现次数最多的字符串以及出现的次数

    金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数 ...

  6. 我的Android进阶之旅------>温习Sqlite3的常用操作

    前言;今天要写一个应用来调节系统的Brightness值,来改变系统的背光亮度.由于刚开始些的时候没有考虑Brightness的最小值,直接托动SeekBar到最小值(为0).瞬间,屏幕变成全黑,失败 ...

  7. 在RedHat Linux系统中安装和配置snmp服务

    检查系统是否安装snmp服务 # rpm -qa|grep snmp net-snmp-5.3.2.2-17.el5 net-snmp-perl-5.3.2.2-17.el5 net-snmp-dev ...

  8. c#中多线程写DataGridView出现滚动栏导致程序卡死(无响应)的解决的方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013529927/article/details/24225567 由于写的程序涉及到多线程维护一 ...

  9. DOM的介绍

    一 . DOM 介绍 什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节 ...

  10. 获取原生的DOM方式,DIY脚手架,vue-cli的使用

    一 . 获取原生的DOM的方式 在js中,我们可以通过id, class 或者标签获取DOM元素,vue中也为我们提供了获取原生DOM的方法,就是给标签或者组件添加 ref 属性,通过 this.$r ...