#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME ""
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
int n, q;
int a[3003];
namespace subtask2{
bool check(){
return n <= 3000 and q <= 3000;
}
/**
Nhan xet cua ta la ta se dung cai difference array roi dp tren do
Khi chuyen sang difference array thi ta co cac luu y:
- Cac doan duoc chon phai co cung dau.
- Cac doan duoc chon phai cach nhau 1 phan tu.
**/
int differenceArray[3003], dp[3030][2];
//dp[i][j]: cho biet day con co tong lon nhat khi xet den vi tri i va j la dang duong hay am
void update(int l, int r, int x){
differenceArray[l] += x;
differenceArray[r + 1] -= x;
}
int get(){
int consNegative = 0, consPositive = 0;
memset(dp, -0x3f, sizeof(dp));
dp[1][0] = dp[1][1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < 2; j++){
if (dp[i][j] > -inf){
if (j == 0) {
if (differenceArray[i + 1] > 0) maximize(dp[i + 1][0], dp[i][j] + differenceArray[i + 1]);
else {
maximize(dp[i + 1][0], dp[i][j]);
maximize(dp[i + 1][1], dp[i][j]);
}
}
else {
if (differenceArray[i + 1] < 0) maximize(dp[i + 1][1], dp[i][j] - differenceArray[i + 1]);
else {
maximize(dp[i + 1][0], dp[i][j]);
maximize(dp[i + 1][1], dp[i][j]);
}
}
}
}
}
return max(dp[n][0], dp[n][1]);
}
void solve(){
for(int i = 1; i <= n; i++){
cin >> a[i];
differenceArray[i] = a[i] - a[i - 1];
}
for(int i = 1; i <= q; i++){
int l, r, x;
cin >> l >> r >> x;
update(l, r, x);
cout << get() << endl;
}
}
}
namespace subtask3{
bool check(){
return true;
}
void solve(){
}
}
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin >> n >> q;
if (subtask2::check()) return subtask2::solve(), 0;
}
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
- Nho sinh test de tranh RTE / TLE
--> Coi lai truoc khi nop
**/
Compilation message
Main.cpp: In function 'long long int subtask2::get()':
Main.cpp:46:14: warning: unused variable 'consNegative' [-Wunused-variable]
46 | int consNegative = 0, consPositive = 0;
| ^~~~~~~~~~~~
Main.cpp:46:32: warning: unused variable 'consPositive' [-Wunused-variable]
46 | int consNegative = 0, consPositive = 0;
| ^~~~~~~~~~~~
Main.cpp: At global scope:
Main.cpp:97:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
97 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |