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 and lf = 3, rg = 1, it means the index sequence: 3, 4, 0, 1.

Write program to process given sequence of operations.

Input

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 integer m (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 means rmq operation, it contains three integers lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106) — inc operation.

Output

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 use cout (also you may use %I64d).

Sample 1

Input Output
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
1
0
0

思路:

树形结构。

数据较大时可以使用位运算。

以及注意命令的判断。

#include<iostream>
using namespace std;
typedef long long LL;
const LL INF = 0x3fffffff;
struct ss
{LL v,tag;
}a[200000 << 2];
LL Min(LL a,LL b)
{return a < b ? a:b;
}void pushup(int cur)
{a[cur].v = Min(a[cur<<1].v,a[cur<<1|1].v);
}void built(int l,int r,int cur)
{a[cur].v = 0;a[cur].tag = 0;if(l == r){scanf("%I64d", &a[cur].v);return ;}int mid = (l + r) >> 1;built(l, mid,cur << 1);built(mid + 1,r,cur << 1|1);pushup(cur);
}void pushdown(int cur)
{if(a[cur].tag != 0){a[cur<<1].tag += a[cur].tag;a[cur<<1|1].tag += a[cur].tag;a[cur<<1].v += a[cur].tag;a[cur<<1|1].v += a[cur].tag;a[cur].tag = 0;}
}void update(int l,int r,int ll,int rr,int cur,LL v)
{if(l<=ll&&r>=rr){a[cur].v += v;a[cur].tag += v;return ;}pushdown(cur);int mid = (ll +rr)>>1;if(mid < r) update(l,r,mid + 1,rr,cur<<1|1,v);if(mid >= l) update(l,r,ll,mid,cur<<1,v);pushup(cur);
}LL quiry(int l,int r,int ll,int rr,int cur)
{if(l<=ll&&r>=rr){return a[cur].v;} pushdown(cur);int mid = (ll + rr) >> 1;LL t1 =INF;LL t2 = INF;if(mid < r) t1 = quiry(l,r,mid + 1,rr,cur<<1|1);if(mid >= l) t2 =  quiry(l,r,ll,mid,cur<<1);return Min(t1,t2);
}int main()
{int n;scanf("%d", &n);built(1,n,1);int m;scanf("%d", &m);while(m--){int a,b,c;char ch;scanf("%d%d%c", &a, &b, &ch);a++,b++;if(ch != ' '){if(a>b){LL t1=quiry(a,n,1,n,1);LL t2 = quiry(1,b,1,n,1);printf("%I64d\n",Min(t1,t2));}elseprintf("%I64d\n",quiry(a,b,1,n,1));}else {scanf("%d", &c);if(a>b){update(a,n,1,n,1,c);update(1,b,1,n,1,c);}else update(a,b,1,n,1,c);}}return 0;
}

Circular RMQ相关推荐

  1. Codeforces 52C

    C. Circular RMQ 题目大意: 好激动,带懒标记的上百行的线段树带A掉这道题,线段树真好玩,可以看出来是裸的带懒标记的线段树 难点1:读入的时候怎么判断是3个数还是2个数,看快读最后有无读 ...

  2. 20190916CF训练

    A. Checkout Assistant 大意是你可以花c时间购买一个商品,然后得到任意的t的物品,最小化花费 一个类01背包,对每件物品枚举购买后时间结束的位置,用购买时的dp更新当前时间点 初始 ...

  3. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  4. [ASP.NET4之旅]Circular file references are not allowed

    将ASP.NET 2.0的项目升级到ASP.NET 4后,用VS2010编译站点,某些控件出现编译错误"Circular file references are not allowed&qu ...

  5. 【UVA/Codeforces】1584 Circular Sequence / 792B Counting-out Rhyme(就是一个圈儿...)

    https://vjudge.net/problem/UVA-1584 1584 Circular Sequence 输入一个字符串,可以以字符串中任意一个字母作为起始,输出字典序最小的那个字符串 两 ...

  6. 倍增算法入门 超详细解答+LCA+RMQ(ST表)+例题剖析

    目录 一.倍增算法 二.倍增算法的应用:求LCA(最近公共祖先)附模板题 三.倍增算法的应用:RMQ 问题(ST表)附模板题 一.倍增算法 要了解倍增之前,强烈建议大家先看一下这位大佬对倍增的解释:[ ...

  7. python使用matplotlib可视化饼图(pie plot)、可视化嵌套的环形饼图(Nested circular pie chart)

    python使用matplotlib可视化饼图(pie plot).可视化嵌套的环形饼图(Nested circular pie chart) 目录 python使用matplotlib

  8. partially initialized module ‘numpy‘ has no attribute ‘array‘ (most likely due to a circular import)

    partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import) ...

  9. Accurate circular consensus long-read sequencing improves variant detection and assembly of a human

    Accurate circular consensus long-read sequencing improves variant detection and assembly of a human ...

最新文章

  1. 阿里巴巴的持久层抛弃了hibernate,采用的却是MyBatis框架。。。
  2. shareSDK(分享第三方库)的 使用
  3. OpenVINO 部署 YOLOv5 转换IR文件
  4. 优化tableView性能(针对滑动时出现卡的现象)
  5. PCL环境配置失败和运行PCL自带例子
  6. oracle中dlink使用,Oracle,MetaLink,中文使用指南
  7. Winform中实现文件另存为后并打开文件
  8. 山西上党残疾男子“只”手脱贫 带领村民增收
  9. 1 linux中解决文件已rm删除但空间不释放的案例
  10. ENVI遥感影像镶嵌
  11. 计算机IP掩码的与运算,计算机IP地址与子网掩码如何进行AND运算
  12. 如何解决css样式表在不同浏览器中显示效果不同的问题
  13. 千方百剂创建账套服务器文件,千方百剂各工具使用.doc
  14. 网站日志分析软件--让网站日志分析工作变得更简单
  15. [C语言] 常量与变量
  16. list中的元素升序排列
  17. Flutter ExpansionTile 折叠组件的使用
  18. 计算机课听课心得体会,计算机心得体会(精选8篇).doc
  19. 计算机财务管理技术pdf,计算机财务管理技术在财务管理方面的应用研究.pdf
  20. PTA 7-153 循环结构_ 求数列和

热门文章

  1. Linux安装ArcSDE典型问题
  2. 3.20 挖掘机技术哪家强
  3. 鹰式价差matlab,什么是鹰式价差策略?-投资江湖
  4. 智联的相关信息的获取
  5. Java学习篇——JavaWeb:CSS
  6. 区块链学习1:Merkle树(默克尔树)和Merkle根
  7. VC中自定义消息ON_MESSAGE的用法
  8. C/C++面试常见问题(一)
  9. 什么是双亲委派机制和其作用
  10. python36块砖36人搬算法_Python填字游戏算法题