Submission #796968

#TimeUsernameProblemLanguageResultExecution timeMemory
796968ThMinh_Cake 3 (JOI19_cake3)C++14
0 / 100
1 ms340 KiB
#include<bits/stdc++.h> #define forin(i, a, b) for(int i = a; i <= b; ++i) #define ll long long using namespace std; const int N = 2e5 + 10; int n, m; ll ans; ll s[N]; struct it { int cnt; ll sum; it* lf; it* rt; it() : cnt(), sum(), lf(), rt() { } void up(int i, int val, int f = 1, int l = n) { if(f == l) { cnt = 1; sum = val; return; } int mid = f + l >> 1; if(i <= mid) { lf = lf ? new it(*lf) : new it(); lf -> up(i, val, f, mid); } else { rt = rt ? new it(*rt) : new it(); rt -> up(i, val, mid + 1, l); } sum = cnt = 0; if(lf) sum += lf -> sum, cnt += lf -> cnt; if(rt) sum += rt -> sum, cnt += rt -> cnt; } }; ll sum(it* id) { return id ? id -> sum : 0; } int cnt(it* id) { return id ? id -> cnt : 0; } ll get(it* id1, it* id2, int f = 1, int l = n, int k = m) { if(f == l) return sum(id2) - sum(id1); int mid = f + l >> 1; int res = cnt(id2 -> rt) - cnt(id1 -> rt); ll val = sum(id2 -> rt) - sum(id1 -> rt); if(res < k) { if(!id1 -> lf) id1 -> lf = new it(); if(!id2 -> lf) id2 -> lf = new it(); return val + get(id1 -> lf, id2 -> lf, f, mid, k - res); } else { if(!id1 -> rt) id1 -> rt = new it(); if(!id2 -> rt) id2 -> rt = new it(); return get(id1 -> rt, id2 -> rt, mid + 1, l, k); } } struct piece { int v, c, i; }; piece p[N]; vector<it*> tr; void solve(int from = 1, int to = n, int f = 1, int l = n) { if(f > l) return; int mid = f + l >> 1, nxt = from; ll &a = s[mid]; forin(i, from, min(to, mid - m + 1)) { int val = get(tr[i - 1], tr[mid]) - 2 * (p[mid].c - p[i].c); if(a < val) { a = val; nxt = i; } } solve(from, nxt, f, mid - 1); solve(nxt, to, mid + 1, l); } int main () { cin.tie(0)->sync_with_stdio(0); if(fopen("Task.inp","r")) { freopen("Task.inp","r",stdin); freopen("WA.out","w",stdout); } cin>>n>>m; forin(i, 1, n) cin>>p[i].v>>p[i].c; sort(p + 1, p + n + 1, [] (piece a, piece b) { return a.v < b.v; }); forin(i, 1, n) p[i].i = i; sort(p + 1, p + n + 1, [] (piece a, piece b) { return a.c < b.c; }); tr.resize(n + 1); tr[0] = new it(); forin(i, 1, n) { tr[i] = new it(*tr[i - 1]); tr[i] -> up(p[i].i, p[i].v); } solve(); forin(i, 1, n) ans = max(ans, s[i]); cout<<ans; }

Compilation message (stderr)

cake3.cpp: In member function 'void it::up(int, int, int, int)':
cake3.cpp:21:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |         int mid = f + l >> 1;
      |                   ~~^~~
cake3.cpp: In function 'long long int get(it*, it*, int, int, int)':
cake3.cpp:43:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |     int mid = f + l >> 1;
      |               ~~^~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:64:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |     int mid = f + l >> 1, nxt = from;
      |               ~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen("Task.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen("WA.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...