牛客小白月賽5 I - 區間
阿新 • • 發佈:2018-07-23
poj amp using data cli oar += 一行 cst
看到一份不錯的操作。。。。。
鏈接:https://www.nowcoder.com/acm/contest/135/I
來源:牛客網
Apojacsleam喜歡數組。
他現在有一個n個元素的數組a,而他要對a[L]-a[R]進行M次操作:
操作一:將a[L]-a[R]內的元素都加上P
操作二:將a[L]-a[R]內的元素都減去P
最後詢問a[l]-a[r]內的元素之和? 請認真看題幹及輸入描述。輸入描述:
輸入共M+3行:
第一行兩個數,n,M,意義如“題目描述”
第二行n個數,描述數組。
第3-M+2行,共M行,每行四個數,q,L,R,P,若q為1則表示執行操作2,否則為執行操作1
第4行,兩個正整數l,r
輸出描述:
一個正整數,為a[l]-a[r]內的元素之和
示例1
輸入
復制10 5 1 2 3 4 5 6 7 8 9 10 1 1 5 5 1 2 3 6 0 2 5 5 0 2 5 8 1 4 9 6 2 7
輸出
復制23
說明
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <cmath> 6 #include <sstream> 7#include <algorithm> 8 #include <set> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <iomanip> 13 #include <stack> 14 15 using namespace std; 16 17 typedef long long LL; 18 const int INF = 0x3f3f3f3f; 19 const int MAXN = 1000005; 20 constint MOD = 1e9 + 7; 21 22 #define MemI(x) memset(x, -1, sizeof(x)) 23 #define Mem0(x) memset(x, 0, sizeof(x)) 24 #define MemM(x) memset(x, 0x3f, sizeof(x)) 25 int a[MAXN]; 26 int main() 27 { 28 int n, m; 29 scanf("%d%d", &n, &m); 30 for(int i = 1;i <= n;++i) 31 scanf("%d", &a[i]); 32 //這裏為下面的累加進行修正 33 for(int i = n;i > 1;--i) 34 a[i] = a[i] - a[i - 1]; 35 int flag, l, r, num; 36 //求出修改區間的前綴和 37 for(int i = 1;i <= m;++i) 38 { 39 scanf("%d%d%d%d", &flag, &l, &r, &num); 40 //這裏註意題意 41 if(flag == 1) 42 a[l] -= num, a[r + 1] += num; 43 else 44 a[l] += num, a[r + 1] -= num; 45 } 46 for(int i = 2;i <= n;++i) 47 a[i] = a[i] + a[i - 1]; 48 int x, y; 49 LL ans = 0; 50 scanf("%d%d", &x, &y); 51 for(int i = x;i <= y;++i) 52 ans += a[i]; 53 printf("%lld\n", ans); 54 return 0; 55 }
牛客小白月賽5 I - 區間