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 int
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int n,q,s,t;
int ans=0;
const int MAX=1<<18;
int tree[2*MAX];
int tab[MAX];
int dp[MAX];
void update(int u,int v,int c)
{
for (u+=MAX,v+=MAX;u<v;u>>=1,v>>=1)
{
if (u&1)tree[u++]+=c;
if (v&1)tree[--v]+=c;
}
}
int query(int u)
{
int sum=0;
for (u+=MAX;u>0;u>>=1)sum+=tree[u];
return sum;
}
int32_t main()
{
BOOST;
cin>>n>>q>>s>>t;
for (int i=0;i<=n;i++)cin>>tab[i],update(i,i+1,tab[i]);
dp[0]=0;
for (int i=1;i<=n;i++)
{
if (tab[i]>tab[i-1])dp[i]=dp[i-1]-s*(tab[i]-tab[i-1]);
else dp[i]=dp[i-1]+t*(tab[i-1]-tab[i]);
}
ans=dp[n];
for (int z=0;z<q;z++)
{
int l,r,c;
cin>>l>>r>>c;
//cout<<"ZAWARTOSC "<<z<<"\n";
//for (int i=0;i<=n;i++)cout<<query(i)<<" ";
//cout<<"\n";
int prevl=query(l);
int prevr=query(r);
update(l,r+1,c);
int nowl=query(l);
int nowr=query(r);
int ilel=query(l-1);
int iler=query(r+1);
int balans=0;
// pierwsza czesc dla lewego;
//if (z==2)cout<<"TERAZ "<<ilel<<" "<<prevl<<" "<<nowl<<"\n";
if (ilel>=prevl)balans-=(ilel-prevl)*t;
else balans+=(prevl-ilel)*s;
if (ilel>=nowl)balans+=(ilel-nowl)*t;
else balans-=(nowl-ilel)*s;
// druga czesc dla prawego
if (r!=n)
{
if (iler<=prevr)balans-=(prevr-iler)*t;
else balans+=(iler-prevr)*s;
if (iler<=nowr)balans+=(nowr-iler)*t;
else balans-=(iler-nowr)*s;
}
ans+=balans;
cout<<ans<<"\n";
}
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... |