Submission #1012332

# Submission time Handle Problem Language Result Execution time Memory
1012332 2024-07-02T00:37:20 Z vantam Sjeckanje (COCI21_sjeckanje) C++17
110 / 110
235 ms 39508 KB
#include<bits/stdc++.h>
using namespace std;
const int N=200005 ;
long long i,j,n,q,l,r,x;
long long a[200005],f[200005],dp[200005][2];
struct dulieu{
    long long t,s,ks,kt,best,kk ;
};
dulieu ss[800005] ;
void build(int id, int l, int r){
    if(l==r){
        ss[id].best=abs(f[l]) ;
        ss[id].t=f[l] ;
        ss[id].s=f[l] ;
        ss[id].ks=ss[id].kt=ss[id].kk=0 ;
        return ;
    }
    int mid=(l+r)/2 ;
    build(2*id,l,mid) ;
    build(2*id+1,mid+1,r) ;
    if(ss[2*id].s*ss[2*id+1].t>=0){
        ss[id].best=ss[2*id].best+ss[2*id+1].best ;
        ss[id].t=ss[2*id].t ;
        ss[id].s=ss[2*id+1].s ;
        ss[id].kt=ss[2*id].kt+ss[2*id+1].best ;
        ss[id].ks=ss[2*id].best+ss[2*id+1].ks ;
        ss[id].kk=ss[2*id].kt+ss[2*id+1].ks ;
    }else{
        ss[id].best=max(ss[2*id].ks+ss[2*id+1].best, ss[2*id].best+ss[2*id+1].kt) ;
        ss[id].t=ss[2*id].t ;
        ss[id].s=ss[2*id+1].s ;
        ss[id].kt=max(ss[2*id].kk+ss[2*id+1].best, ss[2*id].kt+ss[2*id+1].kt) ;
        ss[id].ks=max(ss[2*id].ks+ss[2*id+1].ks, ss[2*id].best+ss[2*id+1].kk) ;
        ss[id].kk=max({ss[2*id].kk+ss[2*id+1].ks, ss[2*id].kt+ss[2*id+1].kk, ss[2*id].kk+ss[2*id+1].kk}) ;
    }
    //cout << id << " " << l << ' ' << r << ' ' << st[id] << '\n' ;
    return ;
}
void up(int id, int l, int r, int u){
    if(r<u || u<l) return ;
    if(l==r){
        ss[id].best=abs(f[l]) ;
        ss[id].t=f[l] ;
        ss[id].s=f[l] ;
        ss[id].ks=ss[id].kt=ss[id].kk=0 ;
        return ;
    }
    int mid=(l+r)/2 ;
    up(2*id,l,mid,u) ;
    up(2*id+1,mid+1,r,u) ;
    if(ss[2*id].s*ss[2*id+1].t>=0){
        ss[id].best=ss[2*id].best+ss[2*id+1].best ;
        ss[id].t=ss[2*id].t ;
        ss[id].s=ss[2*id+1].s ;
        ss[id].kt=ss[2*id].kt+ss[2*id+1].best ;
        ss[id].ks=ss[2*id].best+ss[2*id+1].ks ;
        ss[id].kk=ss[2*id].kt+ss[2*id+1].ks ;
    }else{
        ss[id].best=max(ss[2*id].ks+ss[2*id+1].best, ss[2*id].best+ss[2*id+1].kt) ;
        ss[id].t=ss[2*id].t ;
        ss[id].s=ss[2*id+1].s ;
        ss[id].kt=max(ss[2*id].kk+ss[2*id+1].best, ss[2*id].kt+ss[2*id+1].kt) ;
        ss[id].ks=max(ss[2*id].ks+ss[2*id+1].ks, ss[2*id].best+ss[2*id+1].kk) ;
        ss[id].kk=max({ss[2*id].kk+ss[2*id+1].ks, ss[2*id].kt+ss[2*id+1].kk, ss[2*id].kk+ss[2*id+1].kk}) ;
    }
    return ;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #define NAME "test"
    if(fopen(NAME".inp", "r")){
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }
    cin >> n >> q;
    for(i=1;i<=n;i++){
        cin >> a[i] ;
    }
    for(i=1;i<=n;i++){
        f[i]=a[i]-a[i+1] ;
    }
//    //sub12
//    while(q--){
//        cin >> l >> r >> x ;
//        f[l-1]-=x ;
//        f[r]+=x ;
//        for(i=1;i<n;i++){
//            if(f[i-1]*f[i]>=0){
//                dp[i][0]=max(dp[i-1][0], dp[i-1][1]) ;
//                dp[i][1]=max(dp[i-1][0], dp[i-1][1])+abs(f[i]) ;
//            }else{
//                dp[i][1]=dp[i-1][0]+abs(f[i]) ;
//                dp[i][0]=max(dp[i-1][0], dp[i-1][1]) ;
//            }
//        }
//        cout << max(dp[n-1][0], dp[n-1][1]) << '\n' ;
//    }
    build(1,1,n-1) ;
    while(q--){
        cin >> l >> r >> x ;
        f[l-1]-=x;
        f[r]+=x ;
        up(1,1,n-1,l-1) ;
        up(1,1,n-1,r) ;
        cout << ss[1].best << '\n' ;
    }
    return 0;
}
/*
4 7
-1 3 -6 8
1 4 4 2
2
1 4 4 3
2
1 1 2 -1
1 1 2 -4
2
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 2908 KB Output is correct
13 Correct 235 ms 37204 KB Output is correct
14 Correct 223 ms 37200 KB Output is correct
15 Correct 228 ms 38672 KB Output is correct
16 Correct 211 ms 37204 KB Output is correct
17 Correct 202 ms 38716 KB Output is correct
18 Correct 199 ms 39508 KB Output is correct