Codeforce 52C---Circular RMQ 线段树
来源:互联网 发布:单片机外部存储器 编辑:程序博客网 时间:2024/05/22 05:32
You are given circular array a0, a1, ..., an - 1. There are two types of operations with it:
- inc(lf, rg, v) — this operation increases each element on the segment[lf, rg] (inclusively) by v;
- rmq(lf, rg) — this operation returns minimal value on the segment[lf, rg] (inclusively).
Assume segments to be circular, so if n = 5 andlf = 3, rg = 1, it means the index sequence:3, 4, 0, 1.
Write program to process given sequence of operations.
The first line contains integer n (1 ≤ n ≤ 200000). The next line contains initial state of the array:a0, a1, ..., an - 1 ( - 106 ≤ ai ≤ 106),ai are integer. The third line contains integerm (0 ≤ m ≤ 200000),m — the number of operartons. Next m lines contain one operation each. If line contains two integer lf, rg (0 ≤ lf, rg ≤ n - 1) it meansrmq operation, it contains three integers lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106) —inc operation.
For each rmq operation write result for it. Please, do not use%lld specificator to read or write 64-bit integers in C++. It is preffered to usecout (also you may use %I64d).
41 2 3 443 03 0 -10 12 1
100
这题是个裸的线段树题,用到区间更新和查询。
比赛时拿到题一看就知道方法,然后开始写,因为平时没怎么写线段树的题,结果写完后全是bug,调了好久,所以还是得多练,尤其像数据结构这类题,尽量写的时候不要写错,不然找bug真心难。。。附上ac代码(如果线段树查询更新部分看不懂可以去看我的“线段树入门”,自认为写的很详细啦,认真一点就能看懂)~
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define inf 0x3fffffffusing namespace std;int n,m;long long int a[200005];//这题需要用long long,注意一下struct node{ int l,r; long long int val; int tag;//区间要更改的值}segtree[200005*4];void pushup(int root){ segtree[root].val=min(segtree[root<<1].val,segtree[root<<1|1].val);}void build(int root,int l,int r){ segtree[root].l=l; segtree[root].r=r; segtree[root].tag=0; if(l==r) { segtree[root].val=a[l]; return; } int mid=(segtree[root].l+segtree[root].r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); pushup(root);}void pushdown(int root){ if(segtree[root].tag!=0) { segtree[root<<1].val+=segtree[root].tag; segtree[root<<1|1].val+=segtree[root].tag; segtree[root<<1].tag+=segtree[root].tag; segtree[root<<1|1].tag+=segtree[root].tag; segtree[root].tag=0; return; }}long long int query(int root,int l,int r){ int ll=segtree[root].l; int rr=segtree[root].r; if(l<=ll&&rr<=r) { return segtree[root].val; } pushdown(root); int mid=(ll+rr)>>1; long long int ans1=inf,ans2=inf; if(l<=mid) ans1=min(ans1,query(root<<1,l,r)); if(r>mid) ans1=min(ans1,query(root<<1|1,l,r)); return min(ans1,ans2);}void update(int root,int l,int r,int loop){ int ll=segtree[root].l; int rr=segtree[root].r; if(l<=ll&&rr<=r) { segtree[root].val+=loop; segtree[root].tag+=loop; return; } pushdown(root); int mid=(ll+rr)>>1; if(l<=mid) update(root<<1,l,r,loop); if(r>mid) update(root<<1|1,l,r,loop); pushup(root);}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); } build(1,1,n); scanf("%d",&m); for(int i=1;i<=m;i++) { long long int a,b,c; char ch; scanf("%lld%lld",&a,&b);//这题输入很关键,需要注意一下 ch=getchar(); if(ch!=' ') { long long int ans=inf; if(b<a)//如果b比a小的话,就分两段去查询,因为我习惯端点从1开始,所以都加上了1 { ans=query(1,1,b+1); ans=min(ans,query(1,a+1,n)); } else ans=query(1,a+1,b+1); printf("%I64d\n",ans); } else { scanf("%lld",&c); if(b<a)//和查询一样,这里分两段去更新 { update(1,1,b+1,c); update(1,a+1,n,c); } else update(1,a+1,b+1,c); } } } return 0;}
- Codeforce 52C---Circular RMQ 线段树
- Codeforces 52C - Circular RMQ - 线段树
- 【CodeForces】52C Circular RMQ 线段树
- 线段树 C. Circular RMQ
- CodeForces 52C Circular RMQ (区间更新线段树)
- CodeForces 52C Circular RMQ (区间更新线段树)
- CF#52 C Circular RMQ (线段树区间更新)
- codeforces #52 C Circular RMQ(线段树)
- 线段树 Circular RMQ
- CodeForces 52C Circular RMQ(区间循环线段树,区间更新,区间求和)
- Codeforces 52C Circular RMQ 线段树+从字符串读数字 sscanf
- CodeForces 52C Circular RMQ (线段树的区间更新+lazy tag)
- codeforces 52C Circular RMQ(线段树区间更新)【模板】
- CF 52C Circular RMQ(区间更新,区间询问)
- Circular RMQ
- codeforce 777 C. Alyona and Spreadsheet (线段树)
- Circular RMQ CodeForces
- poj3368(线段树,RMQ)
- 局部变量和全局变量以及成员变量的区别
- 用Servlet实现简单的网站登录
- 浏览器调试匿名函数
- 使用Kotlin实现Android简单Demo,对比JAVA实现分析具体语法不同之处(一)
- c++实现洗牌
- Codeforce 52C---Circular RMQ 线段树
- Selenium + phantomJS 爬取动态网站
- 理解mysql 数据库覆盖索引
- Ubuntu下QNX SDP 6.6安装使用指南
- 设计模式(6)-适配器模式
- WUST 1927 信使(最短路之弗洛伊德算法)
- 关于Tomcat部署项目时部署两次问题的解决方案
- POJ 2135 Farm Tour(最小费用最大流模板题)
- Golang教程:(十八)接口