Codeforces 52c circular rmq (interval cyclic line segment tree, Interval Update, interval summation)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Link: http://codeforces.com/problemset/problem/52/C

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

Sample test (s) Input
41 2 3 443 03 0 -10 12 1
Output
100

The Code is as follows:

# Include <cstdio> # include <algorithm> using namespace STD; # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 // The difference between lson and rson indicates that the Left and Right sons of the node represent the root of the current subtree ), that is, the current node # define INF 0x3fffffff _ int64 mmin [5000047] ;__ int64 Col [5000047] ;__ int64 min (_ int64 A, _ int64 B) {if (a <B) return a; return B;} void pushup (INT RT) {// sum [RT] = sum [RT <1] + sum [RT <1 | 1]; mmin [RT] = min (mmin [RT <1], mmin [RT <1 | 1]);} void Pushdown (int rt, int m) {If (COL [RT]) {Col [RT <1] + = Col [RT]; Col [RT <1 | 1] + = Col [RT]; mmin [RT <1] + = Col [RT]; mmin [RT <1 | 1] + = Col [RT]; Col [RT] = 0 ;}} void build (int l, int R, int RT) {Col [RT] = 0; If (L = r) {scanf ("% i64d ", & mmin [RT]); return;} int M = (L + r)> 1; build (lson); Build (rson); pushup (RT );} void Update (int l, int R, int V, int L, int R, int RT) {If (L <= L & R <= r) {Col [RT] + = V; mmin [RT] + = V; return;} Pushdown (RT, R-l + 1 ); int M = (L + r)> 1; if (L <= m) Update (L, R, V, lson); If (r> m) Update (L, r, V, rson); pushup (RT) ;:int64 query (int l, int R, int L, int R, int RT) {// query interval [L, if (L <= L & R <= r) in R] // the current node is completely included in the query interval. Return mmin [RT]; Pushdown (RT, r-l + 1); int M = (L + r)> 1 ;__ int64 ret = inf; If (L <= m) // go to the left ret = min (Ret, query (L, R, lson); If (r> m) // go to the right ret = min (Ret, query (L, r, rson); return ret;} int main () {int n, m, a, B, c; char op; scanf ("% d", & N ); build (1, n, 1); scanf ("% d", & M); While (M --) {scanf ("% d % C", &, & B, & OP); A ++; // converts the interval to 1 to Nb ++; if (a <= B) {If (OP = '') // indicates that the Operation {scanf ("% d", & C); If (C = 0) continue; update (A, B, C, 1, n, 1);} elseprintf ("% i64d \ n", query (A, B, 1, n, 1);} else {If (OP = '') {scanf ("% d", & C); If (C = 0) continue; update (A, N, C, 1, n, 1 ); // The Shard range is A to N and 1 to bupdate (1, B, c, 1, n, 1);} elseprintf ("% i64d \ n ", min (query (A, N, 1, n, 1), query (1, B, 1, n, 1) ;}} return 0 ;}




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.