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
Sample Output
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; }
- 1435: Find the minimum
- Find the kth minimum.
- CUG@WH 1435Find the minimum(线段树成段更新)
- Find the minimum线段树成段更新
- 线段树之Find the minimum
- Find the first minimum number and the second one.
- Find the Minimum Element in A sorted and Rotated Array
- Given a tree, find the node with the minimum sum of distances to other nodes
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find Minimum Array
- Find minimum cut
- In a bunch of number to find out the biggest minimum value
- [LeetCode] find the Minimum window in s which contains all elements from T
- CareerCup Write the code to find lexicographic minimum in a circular array
- Given an array say [9,20,-2,-45,23,5,1], find the minimum positive missing number from the array.
- The Minimum Length 【kmp】
- hust1010 The Minimum Length
- The Minimum Length KMP
- [BZOJ2809][Apio2012]dispatching(可并堆)
- 在 Visual Studio 中自定义开发设置
- Vijos P1317 开心的金明(动态规划,01背包,NOIP)
- python pip安装模块加速的方法
- NBUT OJ 1647 又一道简单题 直接动手就好
- 1435: Find the minimum
- mysql Show global status参数详解
- 最长回文子串
- 1022. Digital Library (30)-PAT甲级真题(map映射)
- HDU 5860 Death Sequence (递推 + 约瑟夫环 + 思维)——2016 Multi-University Training Contest 10
- Spring从入门到精通(一)----IoC(控制反转)
- UVa 1428 Ping pong (树状数组)
- PerconaXtrabackup 压缩备份集
- iptables学习