Submission #154842

#TimeUsernameProblemLanguageResultExecution timeMemory
154842SenseiDivide and conquer (IZhO14_divide)C++14
100 / 100
203 ms26356 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5; class SegmentTree { private: long long tree[4 * 2 * N + 7]; int mid (int ss, int se) { return (ss + se) >> 1; } void upd (int ss, int se, int si, int i, long long k) { if (ss == se) { tree[si] = max(tree[si], k); } else { if (i <= mid(ss, se)) { upd (ss, mid(ss, se), si * 2, i, k); } else { upd (mid(ss, se) + 1, se, si * 2 + 1, i, k); } tree[si] = max(tree[si * 2], tree[si * 2 + 1]); } } long long qry (int ss, int se, int si, int qs, int qe) { if (ss > qe || qs > se) { return 0; } if (qs <= ss && se <= qe) { return tree[si]; } return max(qry(ss, mid(ss, se), si * 2, qs, qe), qry(mid(ss, se) + 1, se, si * 2 + 1, qs, qe)); } public: void update (int i, long long k) { upd(1, 2 * N, 1, i, k); } long long query (int qs, int qe) { return qry(1, 2 * N, 1, qs, qe); } void clear () { memset(tree, 0, sizeof tree); } }; int x[N + 7]; int g[N + 7]; int e[N + 7]; long long pree[N + 7]; long long preg[N + 7]; map<long long, int> newv; int main () { int n; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d %d %d", &x[i], &g[i], &e[i]); } for (int i = 1; i <= n; i++) { pree[i] = pree[i - 1] + e[i]; preg[i] = preg[i - 1] + g[i]; } vector<long long> values; for (int i = 1; i <= n; i++) { values.push_back(pree[i] - x[i]); values.push_back(pree[i - 1] - x[i] + 1); } sort(values.begin(), values.end()); int currv = 1; newv[values[0]] = currv; for (int i = 1; i < values.size(); i++) { if (values[i] != values[i - 1]) { currv++; } newv[values[i]] = currv; } long long ans = 0; SegmentTree segtree; segtree.clear(); for (int i = n; i >= 1; i--) { // cerr << "i: " << i << " " << pree[i] << " " << x[i] << " " << preg[i] << endl; ans = max(ans, 1LL * g[i]); segtree.update(newv[pree[i] - x[i]], preg[i]); // cerr << "x: " << newv[pree[i] - x[i]] << endl; ans = max(ans, segtree.query(newv[pree[i - 1] - x[i] + 1], 2 * N) - preg[i - 1]); // cerr << "y: " << newv[pree[i - 1] - x[i] + 1] << endl; // cerr << "qry: " << segtree.query(newv[pree[i - 1] - x[i] + 1], 2 * N) << endl; } cout << ans << endl; return 0; } /* 4 0 5 1 1 7 2 4 4 1 7 15 1 */ /* 2 0 4 1 3 5 1 */

Compilation message (stderr)

divide.cpp: In function 'int main()':
divide.cpp:90:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < values.size(); i++) {
                     ~~^~~~~~~~~~~~~~~
divide.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &x[i], &g[i], &e[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...