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>
using namespace std;
#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;
void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}
const int maxn = 200050;
int n, q, s, t, a[maxn];
struct node{ll sum, tag; int l, r;}tr[maxn<<2];
void pd(int k)
{
if(tr[k].tag)
{
tr[k<<1].tag += tr[k].tag;
tr[k<<1|1].tag += tr[k].tag;
tr[k<<1].sum += tr[k].tag * (tr[k<<1].r - tr[k<<1].l + 1);
tr[k<<1|1].sum += tr[k].tag * (tr[k<<1|1].r - tr[k<<1|1].l + 1);
tr[k].tag = 0;
}
}
void pu(int k)
{
tr[k].sum = tr[k<<1].sum + tr[k<<1|1].sum;
}
void build(int l, int r, int k = 1)
{
tr[k].l = l; tr[k].r = r; tr[k].tag = 0;
if(l == r) {tr[k].sum = a[l]; return;}
int mid = l + r >> 1;
build(l, mid, k<<1);
build(mid+1, r, k<<1|1);
pu(k);
}
void upd(int l, int r, int x, int k = 1)
{
if(l <= tr[k].l && tr[k].r <= r)
{
tr[k].sum += x * (tr[k].r - tr[k].l + 1);
tr[k].tag += x;
return;
}
pd(k);
int mid = tr[k].l + tr[k].r >> 1;
if(r <= mid) upd(l, r, x, k<<1);
else if(l > mid) upd(l, r, x, k<<1|1);
else upd(l, mid, x, k<<1), upd(mid+1, r, x, k<<1|1);
pu(k);
}
ll ask(int p, int k = 1)
{
if(tr[k].l == tr[k].r) return tr[k].sum;
pd(k);
int mid = tr[k].l + tr[k].r >> 1;
if(p <= mid) return ask(p, k<<1);
else return ask(p, k<<1|1);
}
int main()
{
#ifdef MPS
fp("1.in","r",stdin);
fp("1.out","w",stdout);
#endif
sf("%d%d%d%d",&n,&q,&s,&t);
s = -s;
fo(i,0,n) sf("%d",&a[i]);
ll ans = 0;
fo(i,0,n-1)
if(a[i] < a[i+1]) ans += (a[i+1]-a[i])*1ll*s;
else ans += (a[i]-a[i+1])*1ll*t;
build(0, n);
fo(i,1,q)
{
int l, r, x;
sf("%d%d%d",&l,&r,&x);
ll oa = ask(l), ob = ask(r), w;
w = ask(l-1); ans -= abs(oa - w) * 1ll * (w < oa ? s : t);
if(r != n)
{
w = ask(r+1);
ans -= abs(ob - w) * 1ll * (ob < w ? s : t);
}
upd(l, r, x);
oa = ask(l); ob = ask(r);
w = ask(l-1); ans += abs(oa - w) * 1ll * (w < oa ? s : t);
if(r != n)
{
w = ask(r+1);
ans += abs(ob - w) * 1ll * (ob < w ? s : t);
}
pf("%lld\n",ans);
}
return 0;
}
Compilation message (stderr)
foehn_phenomena.cpp: In function 'void build(int, int, int)':
foehn_phenomena.cpp:57:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
foehn_phenomena.cpp: In function 'void upd(int, int, int, int)':
foehn_phenomena.cpp:71:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tr[k].l + tr[k].r >> 1;
~~~~~~~~^~~~~~~~~
foehn_phenomena.cpp: In function 'll ask(int, int)':
foehn_phenomena.cpp:81:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tr[k].l + tr[k].r >> 1;
~~~~~~~~^~~~~~~~~
foehn_phenomena.cpp: In function 'int main()':
foehn_phenomena.cpp:91:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d%d%d%d",&n,&q,&s,&t);
^
foehn_phenomena.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,0,n) sf("%d",&a[i]);
^
foehn_phenomena.cpp:102:5: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d%d%d",&l,&r,&x);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |