[HDU6315]Naive Operations(线段树+树状数组)
构造一个序列B[i]=-b[i],建一颗线段树,维护区间max,
每次区间加后再询问该区间最大值,如果为0就在树状数组中对应的值+1(该操作可能进行多次)
答案在树状数组中找
其实只用一颗线段树也是可以的
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#define mst(a) memset(a,0,sizeof(a))
#define N 100010
using namespace std; int n,m,b[N]; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} namespace BT{
int T[N];
#define lowbit(x) ((x)&(-x))
void add(int x,int v){for(;x<=n;x+=lowbit(x))T[x]+=v;}
int Q(int x){int r=0;for(;x;x-=lowbit(x))r+=T[x];return r;}
void ntt(){mst(T);}
} namespace Seg{
#define MID int mid=(l+r)>>1,ls=id<<1,rs=id<<1|1
int tag[N*4];
struct info{
int x,id;
info(){x=id=0;}
info(int a,int b):x(a),id(b){}
friend info operator+(info a,info b){
return a.x>b.x?a:b;
}
}T[N*4];
void build(int l,int r,int id){
if(l==r){T[id]=info(-b[l],l);return;}
MID;
build(l,mid,ls),build(mid+1,r,rs);
T[id]=T[ls]+T[rs];
}
void pushdown(int l,int r,int id){
int &tmp=tag[id];
if(!tmp)return;
MID;
tag[ls]+=tmp,tag[rs]+=tmp;
T[ls].x+=tmp,T[rs].x+=tmp;
tmp=0;
}
void upd(int l,int r,int id,int L,int R,int v){
if(L<=l&&r<=R){tag[id]+=v;T[id].x+=v;return;}
pushdown(l,r,id);
MID;
if(L<=mid)upd(l,mid,ls,L,R,v);
if(R>=mid+1)upd(mid+1,r,rs,L,R,v);
T[id]=T[ls]+T[rs];
}
info Q(int l,int r,int id,int L,int R){
if(L<=l&&r<=R){return T[id];}
pushdown(l,r,id);
MID;
info res(-1e9,0);
if(L<=mid)res=res+Q(l,mid,ls,L,R);
if(R>=mid+1)res=res+Q(mid+1,r,rs,L,R);
return res;
}
void fft(){mst(tag),mst(T);}
}
void fwt(){Seg::fft(),BT::ntt();}
int main(){
for(;~scanf("%d%d",&n,&m);){
fwt();
for(int i=1;i<=n;++i)b[i]=read();
Seg::build(1,n,1);
for(int i=1;i<=m;++i){
char s[10];scanf("%s",s);
int l=read(),r=read();
if(s[0]=='a'){
Seg::upd(1,n,1,l,r,1);
for(;;){
Seg::info tmp=Seg::Q(1,n,1,l,r);
if(tmp.x==0){
Seg::upd(1,n,1,tmp.id,tmp.id,-b[tmp.id]);
BT::add(tmp.id,1);
}else break;
} }else printf("%d\n",BT::Q(r)-BT::Q(l-1));
}
}
return 0;
}
[HDU6315]Naive Operations(线段树+树状数组)的更多相关文章
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- HDU6315 Naive Operations 线段树
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)
原题地址 Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/ ...
- HDU6315 Naive Operations(多校第二场1007)(线段树)
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)
http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...
- HDU 6315 Naive Operations(线段树区间整除区间)
Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...
- HDU - 6315 Naive Operations (线段树+思维) 2018 Multi-University Training Contest 2
题意:数量为N的序列a和b,a初始全为0,b为给定的1-N的排列.有两种操作:1.将a序列区间[L,R]中的数全部+1:2.查询区间[L,R]中的 ∑⌊ai/bi⌋(向下取整) 分析:对于一个位置i, ...
随机推荐
- 设计模式:仲裁者(Mediator)模式
设计模式:仲裁者(Mediator)模式 一.前言 Mediator模式又称为仲裁者模式或者中介者模式,所起的作用是仲裁和中介,帮助其它类之间进行交流.在仲裁者模式之中,我们要明确两个概念,那 ...
- ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...
- “三八节”如何做好EDM邮件营销
阳春三月,乍暖还寒,万物复苏,一年一度的三八节也马上来临了,各路商家都开足马力,掀起了一股美丽的旋风.如今酒香也怕巷子深,要想取得良好的营销效果,就得早早动手,赚足眼球,才会换来节日当天的丰厚回馈.U ...
- servlet的使用
Servlet是比较基础的的客户端与服务器数据交互程序,通过HttpServletRequest请求和HttpServletResponse响应,可以基本实现web程序开发. 1.Servlet基础代 ...
- 线程锁的本质:线程控制、线程状态控制 while if:根据线程的关系(模式)协调线程的执行
线程锁的本质:线程控制.线程状态控制 while if https://www.cnblogs.com/feng9exe/p/8319000.html https://www.cnblogs.com/ ...
- Centos7 安装eclipse IDE for C++
1.安装前eclipse需要java, yum -y install java 查看版本java -version 2.下载eclipse IDE http://www.eclipse.org/dow ...
- PHP基础系列(二) PHP数组相关的函数分类整理
之前写过一篇介绍 PHP字符串函数 的博文,这里写第二篇,本文主要介绍PHP 数组相关的函数: 一.检查数组中是否存在 array_key_exists — 检查给定的键名或索引是否存在于数组中 ar ...
- 9、SpringBoot-CRUD国际化
1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国际化配置文件, ...
- ROBOCOPY——Windows 的可靠文件复制
复制指定类型文件 (-s :含子目录 不包括空目录) 复制所有 (-e :含子目录 包括空目录) 复制指定成层级内的 (-lev:n 仅复制源目录树的前 n 层) 复制排除给定类型后的 (-xf) ...
- SSM框架之多数据源配置
多数据源的应用场景:主要是数据库拆分后,怎样让多个数据库结合起来来达到业务需求. SSM框架(Spring+SpringMVC+MyBatis(MyBatis-Plus))是目前最常用的,此次仍然是m ...