CF #52 C Circular RMQ (segment tree Interval Update)

Source: Internet
Author: User

CF #52 C Circular RMQ (segment tree Interval Update)

Description

You are given circular arrayA0 ,?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) V; Rmq( Lf,? Rg)-This operation returns minimal value on the segment [ Lf,? Rg] (Inclusively ).

Assume segments to be circular, so ifN? =? 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 integerN(1? ≤?N? ≤? (200000). The next line contains initial state of the array:A0 ,?A1 ,?...,?AN? -? 1 (? -? 106? ≤?AI? ≤? 106 ),AIAre integer. The third line contains integerM(0? ≤?M? ≤? 200000 ),M-The number of operartons. NextMLines contain one operation each. If line contains two integerLf,?Rg(0? ≤?Lf,?Rg? ≤?N? -? 1) it meansRmqOperation, it contains three integersLf,?Rg,?V(0? ≤?Lf,?Rg? ≤?N? -? 1 ;? -? 106? ≤?V? ≤? 106 )-IncOperation.

Output

For eachRmqOperation 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 ).

Sample Input

Input
41 2 3 443 03 0 -10 12 1
Output
100

The question is easy to understand.

If a> B, query 0 ~ B and ~ N-1. The rest is the line segment tree Interval Update template, which determines whether c exists in m queries. For details, refer to the Code for better understanding.


#include 
 
  #include 
  
   #include #include 
   
    #include 
    
     #include 
     
      #define lson o << 1, l, m#define rson o << 1|1, m+1, rusing namespace std;typedef __int64 LL;const __int64 MAX =  9223372036854775807;const int maxn = 200010;int n, a, q, c, b;char str[1200];LL mi[maxn<<2], add[maxn<<2];void up(int o) {    mi[o] = min(mi[o<<1], mi[o<<1|1]);}void down(int o) {    if(add[o]) {        add[o<<1] += add[o];        add[o<<1|1] += add[o];        mi[o<<1] += add[o];        mi[o<<1|1] += add[o];        add[o] = 0;    }}void build(int o, int l, int r) {    if(l == r) {        scanf("%I64d", &mi[o]);        return;    }    int m = (l+r) >> 1;    build(lson);    build(rson);    up(o);}void update(int o, int l, int r) {    if(a <= l && r <= b) {        add[o] += c;        mi[o] += c;        return ;    }    down(o);    int m = (l+r) >> 1;    if(a <= m) update(lson);    if(m < b ) update(rson);    up(o);}LL query(int o, int l, int r) {    if(a <= l && r <= b) return mi[o];    down(o);    int m = (l+r) >> 1;    LL res = MAX;    if(a <= m) res = query(lson);    if(m < b ) res = min(res, query(rson));    return res;}int main(){    scanf("%d", &n);    build(1, 0, n-1);    scanf("%d", &q); getchar(); while(q--) {        gets(str);        if(sscanf(str,"%d %d %d", &a, &b, &c) == 3)  {            if(a <= b) update(1, 0, n-1);            else {                int tmp = b;                b = n-1; update(1, 0, n-1);                a = 0, b = tmp; update(1, 0, n-1);            }        } else {            LL ans ;            if(a <= b) ans = query(1, 0, n-1);            else {                int tmp = b;                b = n-1; ans = query(1, 0, n-1);                a = 0, b = tmp; ans = min(ans, query(1, 0, n-1));            }            printf("%I64d\n", ans);        }    }    return 0;}
     
    
   
  
 


Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.