> File Name: 52c.cpp > Author: jiangyuzhu > Mail: 834138558@qq.com > Created Time: 2016/8/23 10:23:31 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 4e5 + 5; int M; ll T[maxn << 2]; void build(int n) { for(M = 1; M <= n + 1; M <<= 1); for(int i = M + 1; i <= M + n; i++) scanf("%I64d", &T[i]); for(int i = M - 1; i; i--){ T[i] = min(T[i << 1], T[i << 1 | 1]); } for(int i = M + n; i; i--){ T[i] = T[i] - T[i >> 1]; } } void update(int l, int r, int x) { l = l + M - 1, r = r + M + 1; ll tmp; for(; l^r^1; l >>= 1, r >>= 1){ if(~l & 1) T[l ^ 1] += x; if(r & 1) T[r ^ 1] += x; tmp = min(T[l], T[l ^ 1]), T[l] -= tmp, T[l ^ 1] -= tmp, T[l >> 1] += tmp; tmp = min(T[r], T[r ^ 1]), T[r] -= tmp, T[r ^ 1] -= tmp, T[r >> 1] += tmp; } while(l > 1){ tmp = min(T[l], T[l ^ 1]), T[l] -= tmp, T[l ^ 1] -= tmp, T[l >>= 1] += tmp; } } ll query(int l, int r) { l = l + M, r = r + M; ll lans = 0, rans = 0; if(l != r){ for(; l^r^1; l >>= 1, r >>= 1){ lans += T[l], rans += T[r]; if(~l & 1) lans = min(lans, T[l ^ 1]); if(r & 1) rans = min(rans, T[r ^ 1]); } } ll ans = min(lans + T[l], rans + T[r]); while(l > 1) ans += T[l >>= 1]; return ans; } int main (void) { int n;scanf("%d", &n); build(n); int m;scanf("%d", &m); int x, y, z; ll ans; for(int i = 0; i < m; i++){ scanf("%d%d", &x, &y); if(getchar() == '\n'){ if(x > y) ans = min(query(x + 1, n), query(1, y + 1)); else ans = query(x + 1, y + 1); printf("%I64d\n", ans); }else{ scanf("%d", &z); if(x > y){ update(x + 1, n, z); update(1, y + 1, z); }else update(x + 1, y + 1, z); } } return 0; }
|