差分模版题(需理解才明白)AT2442 フェーン現象 (Foehn Phenomena)

https://www.luogu.org/problemnew/show/AT2442

 1 #include <bits/stdc++.h>
 2 #define read read()
 3 #define up(i,l,r) for(register int i = (l);i <= (r);i++)
 4 #define down(i,l,r) for(register int i = (l);i >= (r);i--)
 5 #define traversal_vedge(i) for(register int i = head[u]; i ;i = e[i].nxt)
 6 #define ll long long
 7 using namespace std;
 8 int read
 9 {
10     int x = 0, f = 1; char ch = getchar();
11     while(ch < 48 || ch > 57) {if(ch == '-')f = -1; ch = getchar();}
12     while(ch >=48 && ch <=57) {x = 10 * x + ch - 48;ch = getchar();}
13     return x * f; 
14 }
15 const int N =  2e5+7;
16 ll n,q,S,T,b[N],ans;
17 
18 inline ll get(ll x)
19 {
20     return x > 0 ? (-x*S) : (-x*T);
21 }
22 
23 int main()
24 {
25     freopen("input.txt","r",stdin);
26     n = read; q = read; S = read; T = read;
27     ll last = read;
28     up(i,1,n)
29     {
30         ll x = read;
31         b[i] = x - last;
32         last = x;
33         ans += get(b[i]);
34     }
35     //ans代表b[i](1~n)遍历后的答案; 
36     while(q--)
37     {
38         int l = read, r = read, x = read;
39         ans -= get(b[l]);// b[l]改变,先减后加;
40         b[l] += x;
41         ans += get(b[l]);
42         if(r < n)
43         {
44             ans -= get(b[r+1]);//debug -= -> +=
45             b[r+1] -= x;
46             ans += get(b[r+1]);//degug += -> -=
47         }
48         printf("%lld
",ans);
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/mzg1805/p/10413166.html