/*
<< in the name of Allah >>
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef pair<int , int> pii ;
typedef pair<ll , ll> pll ;
#define ps push_back
#define pb pop_back
#define mp make_pair
#define all(x) x.begin() , x.end()
#define sz(x) (int)x.size()
#define debug(a) cout<<"debug: "<<(#a)<<" = "<<a<<'\n'
#define debugAr(a) cout << "Array " << (#a) << " :\n" ;for (int TMP : a) cout << TMP << " " ; cout << '\n' ;
const int maxn = 2e5 + 3 ;
ll dp[maxn] ;
ll a[maxn] ;
ll ghol[maxn] ;
ll n , q ;
ll Abs(ll x){
if (x < 0) x = 0-x ;
return x ;
}
void solve(){
fill(ghol , ghol+n+2 , 0 ) ;
for (int i = n ;i > 1 ; i--){
if (a[i-1] <= a[i]){
int j = i-1 ;
while (a[j] <= a[j+1] && j > 0){
ghol[j] = i ;
j-- ;
}
i = j+2 ;
}
else{
int j = i-1 ;
while (a[j] > a[j+1] && j > 0){
ghol[j] = i ;
j-- ;
}
i = j+2 ;
}
}
for (int i = n-1 ; i > 0 ;i--){
ll x = Abs(a[ghol[i]] - a[i]) ;
dp[i] = max(dp[i+1] , (x+dp[ghol[i]+1])) ;
if (ghol[i] > i){
x = Abs(a[ghol[i]-1] -a[i]) ;
dp[i] = max(dp[i] , x+dp[ghol[i]]) ;
}
}
cout << dp[1] << '\n' ;
}
int main()
{
ios::sync_with_stdio(false) ;
cin.tie(NULL) ;
cout.tie(NULL) ;
cin>> n >> q ;
for (int i = 1;i <= n ; i++) cin >> a[i] ;
while (q--){
ll l , r , tt ;
cin >> l >> r >> tt ;
for (int i = l ; i <= r ; i++) a[i] += tt ;
solve() ;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
63 ms |
416 KB |
Output is correct |
8 |
Correct |
64 ms |
540 KB |
Output is correct |
9 |
Correct |
65 ms |
420 KB |
Output is correct |
10 |
Correct |
64 ms |
432 KB |
Output is correct |
11 |
Correct |
66 ms |
416 KB |
Output is correct |
12 |
Correct |
61 ms |
412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
63 ms |
416 KB |
Output is correct |
8 |
Correct |
64 ms |
540 KB |
Output is correct |
9 |
Correct |
65 ms |
420 KB |
Output is correct |
10 |
Correct |
64 ms |
432 KB |
Output is correct |
11 |
Correct |
66 ms |
416 KB |
Output is correct |
12 |
Correct |
61 ms |
412 KB |
Output is correct |
13 |
Execution timed out |
2053 ms |
5136 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |