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 思路: 首先可以发现答案是具有单调性的,我们考虑二 ...
随机推荐
- Getting Started with the G1 Garbage Collector(译)
原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...
- 华为云测平台服务再升级!华为M5系列平板调测能力正式上线!
6月1日,华为M5系列平板设备兼容性测试和远程真机调试功能在华为终端开放实验室正式上线!助力您的产品在大屏适配上快人一步! 华为终端开放实验室DevEco平台现已提供基于华为M5系列平板设备的兼 ...
- 有状态的EJB对象和无状态的EJB对象
一,定义有状态Bean和无状态Bean 有状态Bean: @Stateful @Remote public class StatefulEjbBean implements StatefulEjb{ ...
- 计算机网络--DNS
1.域名系统DNS(domain name system)是因特网使用的命名系统,用来把便于人们时用的机器名字转换为IP地址.因特网的域名系统DNS被设计成一个联机分布式数据库系统,并采用客户服务器方 ...
- ES通过API调整设置
1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0
- 九度OJ 1055:数组逆置 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7324 解决:3429 题目描述: 输入一个字符串,长度小于等于200,然后将数组逆置输出. 输入: 测试数据有多组,每组输入一个字符串. ...
- OSGI简介(转)
原文地址 目前,业内关于OSGI技术的学习资源或者技术文档还是很少的.我在某宝网搜索了一下“OSGI”的书籍,结果倒是有,但是种类少的可怜,而且几乎没有人购买.因为工作的原因我需要学习OSGI,所以我 ...
- python 安装coreml
2.安装pip, 下载get-pip.py, https://bootstrap.pypa.io/get-pip.py,然后Python 这个文件,如果没有权限就加sudo 3.安装coreml:这 ...
- 【栈】日志分析(BSOJ2981)
Description M海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志.该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装 ...
- hihocoder #1040 矩形判断(计算几何问题 给8个点的坐标,能否成为一个矩形 【模板思路】)
#1040 : 矩形判断 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形. 输入 输入第一行是一个整数T ...