Submission #541829

#TimeUsernameProblemLanguageResultExecution timeMemory
541829keta_tsimakuridzeCake 3 (JOI19_cake3)C++14
24 / 100
4019 ms10116 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<ll,int> #define f first #define s second #define endl "\n" const int N = 2e5 + 5, mod = 1e9 + 7; //! int t, n, val[N], a[N],m, cur = 0; pair<int,int> p[N]; ll ans; vector<int> rem; multiset<int> s; ll sum = 0; void add(int id, int t) {// cout << val[id] << " " << t << endl; id = val[id]; if(t == 1) { // cout << "+" << id << endl; s.insert(id); sum += id; if(s.size() > m) { rem.push_back(*s.begin()); sum -= *s.begin(); s.erase(s.find(*s.begin())); }// cout << sum << endl; return; } if(!s.count(id)) rem.pop_back(); else { s.erase(s.find(id)); sum -= id; if(rem.size()) { sum += rem.back(); s.insert(rem.back()); rem.pop_back(); } } } void solve(int l,int r,int ql, int qr) { if(r < l || ql > qr) return; int mid = (l + r) >> 1; pii opt; opt = {-2e17, ql}; for(int i = mid; i >= max(l, qr + 1); i--) add(a[i], 1); if(mid < max(l, qr + 1))add(a[mid], 1); for(int i = min(qr, mid - 1); i >= ql; i--) { add(a[i], 1); if(mid - i + 1 >= m) opt = max(opt, make_pair(sum - 2 * (p[mid].f - p[i].f), i)); } ans = max(ans, opt.f); for(int i = ql; i <= min(qr, mid - 1); i++) add(a[i], -1); if(mid < max(l, qr + 1))add(a[mid], -1); for(int i = max(l, qr + 1); i <= mid; i++) add(a[i], -1); if(l == r) return; for(int i = opt.s + 1; i <= min(qr, l - 1); i++) add(a[i], 1); solve(l, mid - 1, ql, opt.s); for(int i = min(qr, l - 1); i >= opt.s + 1; i--) add(a[i], -1); for(int i = max(l, qr + 1); i <= mid; i++) add(a[i], 1); solve(mid + 1,r, opt.s, qr); for(int i = mid; i >= max(l, qr + 1); i--) add(a[i], -1); } main() { // ios_base::sync_with_stdio(false), // cin.tie(0),cout.tie(0); cin >> n >> m; ans = -1e18; vector<pair<int,int> > x; for(int i = 1; i <= n; i++) { cin >> p[i].s >> p[i].f; } sort(p + 1, p + n + 1); for(int i = 1; i <= n; i++) { x.push_back({p[i].s, i}); } sort(x.begin(), x.end()); for(int i = 0; i < x.size(); i++) { if(!i || x[i].f != x[i - 1].f) cur++; val[cur] = x[i].f; a[x[i].s] = cur; } solve(1, n, 1, n); cout << ans; }

Compilation message (stderr)

cake3.cpp: In function 'void add(int, int)':
cake3.cpp:22:15: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   22 |   if(s.size() > m) {
      |               ^
cake3.cpp: At global scope:
cake3.cpp:68:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   68 | main() {
      | ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:82:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |  for(int i = 0; i < x.size(); i++) {
      |                 ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...