제출 #1126157

#제출 시각아이디문제언어결과실행 시간메모리
1126157RequiemSjeckanje (COCI21_sjeckanje)C++17
110 / 110
796 ms44052 KiB
#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[300005]; 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(){ 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][0] + 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; } /**Nhan xet cua ta la 1 doan toi uu khi no tang hoac giam. * Mau chot cua bai nay la ta dua ve different array de giai * dua vao different array ta co the tan dua cap nhat doan thanh cap nhat diem. * Khi giai bai toan cho different array: * dp[i][0/1]: xet den vi tri i va 0/1 tuong trung cho viec ta dang tang hay giam (dif[i] > 0 ==> tang, dif[i] < 0 giam) * dp[i][j] --> dp[i + 1][0/1] * dp[i][j] --> dp[i + 1][j] (+/-) dif[i] (tuy vao j va dau cua dif[i] ma ta tru hay cong) * Ta se dua bai toan len cay segment tree Voi 1 nut segment tree, ta se luu 2 dau. **/ const int MAXN = 3e5 + 9; int differenceArray[MAXN], a[MAXN]; struct Node{ int dp[2][2]; Node(){ memset(dp, 0, sizeof(dp)); } } st[MAXN << 2]; Node Merge(Node a, Node b){ Node res = Node(); for(int startLeft = 0; startLeft < 2; startLeft++){ for(int endingLeft = 0; endingLeft < 2; endingLeft++){ for(int startRight = 0; startRight < 2; startRight++){ for(int endingRight = 0; endingRight < 2; endingRight++){ maximize(res.dp[startLeft][endingRight], a.dp[startLeft][endingLeft]); maximize(res.dp[startLeft][endingRight], b.dp[startRight][endingRight]); if (endingLeft == startRight) maximize(res.dp[startLeft][endingRight], a.dp[startLeft][endingLeft] + b.dp[startRight][endingRight]); } } } } return res; } void update(int nn, int l, int r, int pos, int val){ if (l == r){ st[nn] = Node(); if (differenceArray[l] >= 0) st[nn].dp[0][0] = differenceArray[l]; if (differenceArray[l] < 0) st[nn].dp[1][1] = -differenceArray[l]; } else{ int mid = (l + r) >> 1; if (pos <= mid) update(nn << 1, l, mid, pos, val); else update(nn << 1 | 1, mid + 1, r, pos, val); st[nn] = Merge(st[nn << 1], st[nn << 1 | 1]); } } void solve(){ for(int i = 1; i <= n; i++){ cin >> a[i]; differenceArray[i] = a[i] - a[i - 1]; if (i > 1) update(1, 2, n, i, differenceArray[i]); } for(int i = 1; i <= q; i++){ int l, r, x; cin >> l >> r >> x; differenceArray[l] += x; differenceArray[r + 1] -= x; if (l >= 2) update(1, 2, n, l, differenceArray[l]); if (r + 1 <= n) update(1, 2, n, r + 1, differenceArray[r + 1]); int mx = max({st[1].dp[0][0], st[1].dp[0][1], st[1].dp[1][0], st[1].dp[1][1]}); cout << mx << endl; } } } 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; if (subtask3::check()) return subtask3::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 **/

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp:164:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  164 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...