Submission #885519

#TimeUsernameProblemLanguageResultExecution timeMemory
885519Marco_EscandonFoehn Phenomena (JOI17_foehn_phenomena)C++11
100 / 100
887 ms24628 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
struct node
{
    int value, lazy;
 
    node(int v = 0LL)
    {
        value = v;
        lazy = 0LL;
    }
};
 
node tree[(int)1e6]; int l;
 
void update(int v, int s, int e, int k)
{
    tree[v].lazy += k;
    tree[v].value += (e - s + 1) * k;
}
 
void push(int v, int s, int e)
{
    if (tree[v].lazy)
    {
        int middle = (s + e) / 2;
 
        update(2 * v, s, middle, tree[v].lazy);
        update(2 * v + 1, middle + 1, e, tree[v].lazy);
 
        tree[v].lazy = 0LL;
    }
}
 
void increase(int node, int v, int x, int y, int s, int e)
{
    if (x > e || y < s) return;
 
    if (s >= x && e <= y)
    {
        update(node, s, e, v);
        return;
    }
 
    push(node, s, e);
 
    int middle = (s + e) / 2;
 
    increase(node * 2, v, x, y, s, middle);
    increase(node * 2 + 1, v, x, y, middle + 1, e);
 
    tree[node].value = tree[node * 2].value
        + tree[node * 2 + 1].value;
}
 
void increase(int x, int y, int v) { increase(1, v, x, y, 1, l); }
 
int sum(int node, int x, int y, int s, int e)
{
    if (x > e || y < s) return 0LL;
 
    if (s >= x && e <= y)
        return tree[node].value;
 
    push(node, s, e);
    int middle = (s + e) / 2;
 
    return sum(node * 2, x, y, s, middle) +
        sum(node * 2 + 1, x, y, middle + 1, e);
}
 
int query(int x) { return sum(1, x, x, 1, l); }
 
void Segment_tree(int n)
{
    for (l = 1; l < n; l = (l << 1));
}
 
 
int32_t main()
{
    ll n,q,s,t;
    cin>>n>>q>>s>>t;
    ll cad[n+1];
    Segment_tree (n+2);
    for(int i=0LL; i<n+1; i++)
    {
        cin>>cad[i];
    }
    ll sol=0LL;
    for(int i=1; i<=n; i++)
    {
        sol-=s*max(cad[i]-cad[i-1],0LL);
        sol+=t*max(cad[i-1]-cad[i],0LL);
        increase(i+1,i+1,cad[i]);
    }
    while(q--)
    {
        ll a,b,c;
        cin>>a>>b>>c;
        a++;
        b++;
        ll t1=query(a),t2=query(a-1);
        sol+=s*max(t1-t2,0LL);
        sol-=t*max(t2-t1,0LL);
        if(b!=n+1){
        ll t3=query(b+1), t4=query(b);
        sol+=s*max(t3-t4,0LL);
        sol-=t*max(t4-t3,0LL);}
 
        increase(a,b,c);
        t1=query(a),t2=query(a-1);

        sol-=s*max(t1-t2,0LL);
        sol+=t*max(t2-t1,0LL);
        if(b!=n+1){
        ll t3=query(b+1), t4=query(b);
        sol-=s*max(t3-t4,0LL);
        sol+=t*max(t4-t3,0LL);}
        cout<<sol<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...