HDU4391(线段树+剪枝)
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
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.
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.
#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(线段树+剪枝)的更多相关文章
- 对权值线段树剪枝的误解--以HDU6703为例
引子 对hdu6703,首先将问题转化为"询问一个排列中大于等于k的值里,下标超过r的最小权值是多少" 我们采用官方题解中的做法:权值线段树+剪枝 对(a[i],i)建线段树,查询 ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- HDOJ:6356-Glad You Came(线段树剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356 解题心得: 现在深深的知道了算法复杂度的重要了,这个题算复杂度的时候还要把一些常数也算出来,不然 ...
- LibreOJ #6190. 序列查询(线段树+剪枝)
莫队貌似是过不了的,这题是我没见过的科技... 首先区间按右端点排序,然后一个扫描线,扫到某个区间右端点时候计算答案,线段树上节点的信息并不需要明确定义,我们只要求线段树做到当前扫到now时,查询[L ...
- hdu4027-Can you answer these queries? -(线段树+剪枝)
题意:给n个数,m个操作,分两种操作,一种是将一段区间的每个数都开根号,另一种是查询区间和. 解题:显然对每个数开根号不能用lazy的区间更新.一个一个更新必然爆时间,对1开根号还是1,如果一段区间都 ...
- HDU 5828 Rikka with Sequence (线段树+剪枝优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...
- CodeForces 438D 线段树 剪枝
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- BZOJ 4556(后缀数组+主席树求前驱后继+二分||后缀数组+二分+可持久化线段树)
换markdown写了.. 题意: 给你一个1e5的字符串,1e5组询问,求\([l_1,r_1]\)的所有子串与\([l_2,r_2]\)的lcp 思路: 首先可以发现答案是具有单调性的,我们考虑二 ...
随机推荐
- Net dll版本兼容问题
Net dll组件版本兼容问题 https://www.cnblogs.com/newP/p/9543528.html dll组件版本兼容问题,是生产开发中经常遇到的问题,常见组件兼容问题如:Newt ...
- 重新编译Nginx指导手册【修复静态编译Openssl的Nginx漏洞 】(转)
1. 概述 当前爆出了Openssl漏洞,会泄露隐私信息,涉及的机器较多,环境迥异,导致修复方案都有所不同.不少服务器使用的Nginx,是静态编译opensssl,直接将openssl编译到ng ...
- erlang处理mongodb日期时间格式data类型(原)
在项目中,mongo中要创建日期类型,根据这个日期类型进而对mongo设置过期时间expire,加上对应的index索引自动删除. 而mongo中的日期类型,使用ISO格式,例如:ISODate(&q ...
- Python中类方法、__new__方法和__init__方法解析
在编程语言中创建一个类,有构造方法这样的一个术语.而在Python中,通常大家都认为__init__是构造方法,其实并不完全等同.在构建类中,有一个特殊的方法__new__,这个才能等同为构造方法. ...
- 【BZOJ1499】[NOI2005]瑰丽华尔兹 单调队列+DP
[BZOJ1499][NOI2005]瑰丽华尔兹 Description 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是 ...
- input file 选择Excel文件 相关操作
1.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFo ...
- An Overview of Query Optimization in Relational Systems
An Overview of Query Optimization in Relational Systems
- php xmlrpc使用示例
xmlrpc 远程过程调用, 使用xml文本方式传输数据. soap协议比xmlrpc复杂并强大. 1.修改 php.ini,开启 xmlrpc 扩展 2.rpc_client.php <?ph ...
- typescript import 全局node_modules报错
项目里面有一个node_modules的包太大,每次放到docker里面都要下载半天,大大减少了部署效率. 所以考虑将这个node包全局安装到docker的基础镜像中,那么代码里面直接引用全局包就可以 ...
- MySQL——函数
MySQL数据库提供了很多函数包括: (1)数学函数 (2)字符串函数 (3)日期和时间函数 (4)条件判断函数 (5)系统信息函数 (6)加密函数 (7)格式化函数 一.数学函数 数学函数主要用于处 ...