This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
using namespace std;
const long long N = 3e5+10;
const long long mod = 1e9+7;
const long long inf = 1e18;
struct fenwick {
vector<int>fenw;
void build(int n) {
fenw.resize(n+1);
}
void update(int index,int n,int val) {
while(index <= n) {
fenw[index] += val;
index += index & -index;
}
}
int get(int x) {
int ret = 0;
while(x >= 1) {
ret += fenw[x];
x -= x & -x;
}
return ret;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,Q,s,t,smT=0,smS=0;
cin >> n >> Q >> s >> t;
vector<int>a(n+1);
fenwick ft;
ft.build(n);
for(int i=0;i<=n;i++) {
cin >> a[i];
if(i > 0) {
ft.update(i,n,a[i]);
ft.update(i+1,n,-a[i]);
if(a[i-1] < a[i]) smS += a[i-1]-a[i];
else smT += a[i-1]-a[i];
}
}
/*while(Q--) {
int l,r,x;
cin >> l >> r >> x;
for(int i=l;i<=r;i++) a[i] += x;
int res = 0;
for(int i=1;i<=n;i++) {
if(a[i-1] < a[i]) res += (a[i-1]-a[i])*s;
else res += (a[i-1]-a[i])*t;
}
cout << res << '\n';
}*/
//cout << smS << ' ' << smT << endl;
while(Q--) {
int l,r,x;
cin >> l >> r >> x;
int raz = ft.get(l-1)-ft.get(l);
int raz2=0,raz2_posle=0;
if(r+1 <= n) raz2 = ft.get(r)-ft.get(r+1);
ft.update(l,n,x);
ft.update(r+1,n,-x);
int raz_posle = ft.get(l-1)-ft.get(l);
if(raz >= 0 && raz_posle >= 0) {
// cout << "prvi1" << endl;
smT -= raz;
smT += raz_posle;
}
if(raz < 0 && raz_posle < 0) {
// cout << "drugi1" << endl;
smS -= raz;
smS += raz_posle;
}
if(raz >= 0 && raz_posle < 0) {
// cout << "treci1" << endl;
smT -= raz;
smS += raz_posle;
}
if(raz < 0 && raz_posle >= 0) {
// cout << "cetvrti1" << endl;
smS -= raz;
smT += raz_posle;
}
if(r+1 <= n) raz2_posle = ft.get(r)-ft.get(r+1);
if(raz2 >= 0 && raz2_posle >= 0 && r+1 <= n) {
// cout << "prvi2" << endl;
smT -= raz2;
smT += raz2_posle;
}
if(raz2 < 0 && raz2_posle < 0 && r+1 <= n) {
//// cout << "drugi2" << endl;
smS -= raz2;
smS += raz2_posle;
}
if(raz2 >= 0 && raz2_posle < 0 && r+1 <= n) {
// cout << "drugi3" << endl;
smT -= raz2;
smS += raz2_posle;
}
if(raz2 < 0 && raz2_posle >= 0 && r+1 <= n) {
/// cout << "drugi4" << endl;
smS -= raz2;
smT += raz2_posle;
}
cout << smS*s+smT*t << '\n';
//cout << "dbg" << endl;
//for(int i=1;i<=n;i++) cout << ft.get(i) << ' ';
//cout << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |