1435: Find the minimum

来源:互联网 发布:淘宝卖家登录界面 编辑:程序博客网 时间:2024/06/15 21:01

Description

Given an integer array of size N, we define two kind of operators:
1. Add(L,R,W) : adding an integer W to every element in subarray[L,R];
2. Min(L,R) : returning the minimum number in subarray[L,R].
Note. L and R are the index of array starting from 0. L > R is possible. If L > R, the subarray is composed of array[L], array[L+1].....array[N-1], array[0], array[1],.....array[R].

Input

There are several test cases, processed to the end of file.
For each test, the first line contains two positive integers N and M. N is the size of array, and M is the number of the operation. 
The second line contains N array elements, a1, a2, a3, ...., and an.
Then in the following M lines, each line contains an operation. If the line contains three integers L,R and W, it means the add(L,R,W) operator should be involved. If the line contains two integers L,R , it means the Min(L,R) operator should be involved.
(0<N, M<100,000; 0<= ai <= 10^6; 0 <= L, R <= N – 1, -10^6 <= W <= 10^6。)

Output

 For each Min(L,R) operator in test case, output the return value.

Sample Input


3 31 2 40 20 0 10 2

Sample Output

12

HINT

the output value may be very large ,long long type is recommended!


 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;  #define lson rt << 1 #define rson rt << 1 | 1 typedef long long ll; const int MAXN = (100000 + 100); template < typename T > inline T getMIN(const T &a, const T &b) {     return a < b ? a : b; }  int N, M; ll MIN[MAXN << 2], add[MAXN << 2];  void PushDown(int rt) {     if (add[rt]) {         add[lson] += add[rt];         add[rson] += add[rt];         MIN[lson] += add[rt];         MIN[rson] += add[rt];         add[rt] = 0;     } }  void PushUp(int rt) {     MIN[rt] = getMIN(MIN[lson], MIN[rson]); }  void Build(int L, int R, int rt) {     add[rt] = 0;     if (L == R) {         scanf("%lld", &MIN[rt]);         return;     }     int M = (L + R) >> 1;     Build(L, M, lson);     Build(M + 1, R, rson);     PushUp(rt); }  void Update(int L, int R, int rt, int l, int r, int lnc) {     if (l <= L && R <= r) {         add[rt] += lnc;         MIN[rt] += lnc;         return;     }     PushDown(rt);     int M = (L + R) >> 1;     if (l <= M) Update(L, M, lson, l, r, lnc);     if (r > M) Update(M + 1, R, rson, l, r, lnc);     PushUp(rt); }  ll Query(int L, int R, int rt, int l, int r) {     if (l <= L && R <= r) {         return MIN[rt];     }     PushDown(rt);     int M = (L + R) >> 1;     ll ans = 1LL << 60;     if (l <= M) ans = getMIN(ans, Query(L, M, lson, l, r));     if (r > M) ans = getMIN(ans, Query(M + 1, R, rson, l, r));     return ans; }  int Judge(char *str) {     int len = strlen(str), cnt = 0;     for (int i = 0; i < len; ) {         if (str[i] == ' ') {             cnt++;             while (i < len && str[i] == ' ') i++;         } else             i++;     }     return cnt; }  int main() {     while (~scanf("%d %d", &N, &M)) {         Build(0, N - 1, 1);         getchar();         while (M--) {             char str[22];             int a, b, c;             gets(str);             if (Judge(str) == 1) {                 sscanf(str, "%d %d", &a, &b);                 if (a <= b) {                     printf("%lld\n", Query(0, N - 1, 1, a, b));                 } else                     printf("%lld\n", getMIN(Query(0, N - 1, 1, a, N - 1), Query(0, N - 1, 1, 0, b)));             } else if (Judge(str) == 2) {                 sscanf(str, "%d %d %d", &a, &b, &c);                 if (a <= b) {                     Update(0, N - 1, 1, a, b, c);                 } else {                     Update(0, N - 1, 1, 0, b, c);                     Update(0, N - 1, 1, a, N - 1, c);                 }             }         }     }     return 0; }

0 0
原创粉丝点击