Submission #915423

#TimeUsernameProblemLanguageResultExecution timeMemory
915423n3rm1nDivide and conquer (IZhO14_divide)C++17
100 / 100
67 ms12372 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const long long MAXN = 1e5+10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long n; long long x[MAXN], gold[MAXN], energy[MAXN]; long long a[MAXN], pref[MAXN]; void read() { cin >> n; for (long long i = 1; i <= n; ++ i) { cin >> x[i] >> gold[i] >> energy[i]; a[i] = a[i-1]; pref[i] = pref[i-1] + gold[i]; if(i > 1)a[i] -= x[i] - x[i-1]; a[i] += energy[i]; } } long long t[MAXN * 4]; long long lazy[MAXN * 4]; long long inf = -1; void make_tree(long long i, long long l, long long r) { if(l == r) { t[i] = a[l]; // cout << l << " " << r << " " << a[l] << endl; return; } long long mid = (l + r)/2; make_tree(2*i, l, mid); make_tree(2*i+1, mid+1, r); t[i] = max(t[2*i], t[2*i+1]); } void push_lazy(long long i, long long l, long long r) { t[i] += lazy[i]; if(l != r) { lazy[2*i] += lazy[i]; lazy[2*i+1] += lazy[i]; } lazy[i] = 0; } long long ql, qr, val; void update(long long i, long long l, long long r) { push_lazy(i, l, r); if(qr < l || ql > r)return; if(ql <= l && r <= qr) { lazy[i] = val; push_lazy(i, l, r); return; } long long mid = (l + r)/2; update(2*i, l, mid); update(2*i+1, mid+1, r); t[i] = max(t[2*i], t[2*i+1]); } long long xx; void clear_update(long long i, long long l, long long r) { if(l == r) { t[i] = -1; return; } long long mid = (l + r)/2; if(xx <= mid)clear_update(2*i, l, mid); else clear_update(2*i+1, mid+1, r); t[i] = max(t[2*i], t[2*i+1]); } long long query(long long i, long long l, long long r) { push_lazy(i, l, r); if(l == r) { if(t[i] >= 0)return l; else return -1; } long long mid = (l + r)/2; push_lazy(2*i, l, mid); push_lazy(2*i+1, mid + 1, r); long long tleft = t[2*i], tright = t[2*i+1]; if(tright >= 0)return query(2*i+1, mid+1, r); else return query(2*i, l, mid); } int main() { speed(); read(); make_tree(1, 1, n); long long ans = 0; for (long long starter = 1; starter <= n; ++ starter) { long long index = query(1, 1, n); // cout << index << endl; if(index == -1)continue; long long sol = pref[index] - pref[starter-1]; ans = max(ans, sol); long long change = -energy[starter]; if(starter < n)change += x[starter+1] - x[starter]; ql = starter + 1; qr = n; val = change; if(ql <= qr) update(1, 1, n); xx = starter; clear_update(1, 1, n); } cout << ans << endl; return 0; }

Compilation message (stderr)

divide.cpp: In function 'long long int query(long long int, long long int, long long int)':
divide.cpp:93:15: warning: unused variable 'tleft' [-Wunused-variable]
   93 |     long long tleft = t[2*i], tright = t[2*i+1];
      |               ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...