Submission #1012109

# Submission time Handle Problem Language Result Execution time Memory
1012109 2024-07-01T16:17:55 Z vantam Sjeckanje (COCI21_sjeckanje) C++17
55 / 110
2000 ms 9140 KB
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#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];
long long st[4*N],s[4*N],mit[4*N], mis[4*N],mat[4*N],mas[4*N] ;
void build(int id, int l, int r){
    if(l==r){
        st[id]=abs(f[l]);s[id]=mit[id]=mis[id]=mat[id]=mas[id]=f[l] ;
        return ;
    }
    int mid=(l+r)/2 ;
    build(2*id,l,mid) ;
    build(2*id+1,mid+1,r) ;
    st[id]=max({st[2*id], st[2*id+1],abs(mis[2*id]+mit[2*id+1]),abs(mas[2*id]+mat[2*id+1])}) ;
    s[id]=s[2*id]+s[2*id+1] ;
    mit[id]=min(mit[2*id],s[2*id]+mit[2*id+1]) ;
    mis[id]=min(mis[2*id+1],s[2*id+1]+mis[2*id]) ;
    mat[id]=max(mat[2*id],s[2*id]+mat[2*id+1]) ;
    mas[id]=max(mas[2*id+1],s[2*id+1]+mas[2*id]) ;
    //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){
        st[id]=abs(f[u]);s[id]=mit[id]=mis[id]=mat[id]=mas[id]=f[u] ;
        return ;
    }
    int mid=(l+r)/2 ;
    up(2*id,l,mid,u) ;
    up(2*id+1,mid+1,r,u) ;
    st[id]=max({st[2*id], st[2*id+1],abs(mis[2*id]+mit[2*id+1]),abs(mas[2*id]+mat[2*id+1])}) ;
    s[id]=s[2*id]+s[2*id+1] ;
    mit[id]=min(mit[2*id],s[2*id]+mit[2*id+1]) ;
    mis[id]=min(mis[2*id+1],s[2*id+1]+mis[2*id]) ;
    mat[id]=max(mat[2*id],s[2*id]+mat[2*id+1]) ;
    mas[id]=max(mas[2*id+1],s[2*id+1]+mas[2*id]) ;
    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] ;
        f[i]=a[i]-a[i+1] ;
    }
    for(i=1;i<=n;i++){
        f[i]=a[i]-a[i+1] ;
    }
    //build(1,1,n-1) ;
    //cout << f[3] << '\n' ;
    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 << st[1] << '\n' ;
        //cout << f[1] << '\n' ;
        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{
                //if(l==2 && i==2) cout << dp[i]
                dp[i][1]=dp[i-1][0]+abs(f[i]) ;
                dp[i][0]=max(dp[i-1][0], dp[i-1][1]) ;
            }
            //cout << f[3] << '\n' ;
            //cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
        }
        cout << max(dp[n-1][0], dp[n-1][1]) << '\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:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 25 ms 696 KB Output is correct
8 Correct 24 ms 2648 KB Output is correct
9 Correct 23 ms 604 KB Output is correct
10 Correct 24 ms 4700 KB Output is correct
11 Correct 26 ms 676 KB Output is correct
12 Correct 19 ms 4736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 25 ms 696 KB Output is correct
8 Correct 24 ms 2648 KB Output is correct
9 Correct 23 ms 604 KB Output is correct
10 Correct 24 ms 4700 KB Output is correct
11 Correct 26 ms 676 KB Output is correct
12 Correct 19 ms 4736 KB Output is correct
13 Execution timed out 2055 ms 9140 KB Time limit exceeded
14 Halted 0 ms 0 KB -