This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 "sjeckanje"
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){
// cout << i << ' ' << j <<' ' << dp[i][j] << endl;
if (j == 0) {
if (differenceArray[i + 1] > 0) maximize(dp[i + 1][0], dp[i][j] + differenceArray[i + 1]);
}
else {
if (differenceArray[i + 1] < 0) maximize(dp[i + 1][1], dp[i][j] - differenceArray[i + 1]);
}
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];
}
// cout << get();
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 (stderr)
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:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
95 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |