Codeforce 52C---Circular RMQ 线段树

来源:互联网 发布:单片机外部存储器 编辑:程序博客网 时间:2024/05/22 05:32
C. Circular RMQ
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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 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.

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

Examples
Input
41 2 3 443 03 0 -10 12 1
Output
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;}

原创粉丝点击