Submission #989408

# Submission time Handle Problem Language Result Execution time Memory
989408 2024-05-28T06:34:47 Z HuyAT Sjeckanje (COCI21_sjeckanje) C++14
0 / 110
1 ms 2396 KB
#include<bits/stdc++.h>
#define newl '\n'

const int N = 2e5 + 10;
const int V = 1e7 + 10;
const long long INF = 1e18;
const long long M = 1e9 + 7;

int a[N + 1],n,q;
long long diff[N + 1];

void readData(){
    std::cin >> n >> q;
    for(int i = 1;i <= n;++i){
        std::cin >> a[i];
        if(i > 1){
            diff[i - 1] = a[i] - a[i - 1];
        }
    }
}
long long calc(){
    long long ans = 0;
    for(int i = 1;i < n;++i){
        if(i < n - 1 && (diff[i] < 0) != (diff[i + 1] < 0)){
            ans -= std::min(abs(diff[i]),abs(diff[i + 1]));
        }
        ans += abs(diff[i]);
    }
    return ans;
}
void update(long long &ans,int i,int x){
    if(i > 1 && i < n){
        if((diff[i - 1] < 0) != (diff[i] < 0)){
            ans += std::min(abs(diff[i - 1]),abs(diff[i]));
        }
    }
    if(i < n - 1){
        if((diff[i] < 0) != (diff[i + 1] < 0)){
            ans += std::min(abs(diff[i]),abs(diff[i + 1]));
        }
    }
    if(i < n && i > 0){
        ans -= abs(diff[i]);
    }
    diff[i] += x;
    if(i < n && i > 0){
        ans += abs(diff[i]);
    }
    if(i > 1 && i < n){
        if((diff[i - 1] < 0) != (diff[i] < 0)){
            ans -= std::min(abs(diff[i - 1]),abs(diff[i]));
        }
    }
    if(i < n - 1){
        if((diff[i] < 0) != (diff[i + 1] < 0)){
            ans -= std::min(abs(diff[i]),abs(diff[i + 1]));
        }
    }
}
void solve(){
    long long ans = 0;
    for(int i = 1;i < n;++i){
        if(i < n - 1 && (diff[i] < 0) != (diff[i + 1] < 0)){
            ans -= std::min(abs(diff[i]),abs(diff[i + 1]));
        }
        ans += abs(diff[i]);
    }
//    std::cout << ans << newl;
    for(int i = 1;i <= q;++i){
        int l,r,x;
        std::cin >> l >> r >> x;
        update(ans,l - 1,x);
        update(ans,r,-x);
        std::cout << calc() << newl;
    }
}
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);std::cout.tie(nullptr);
    readData();
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -