Submission #796988

#TimeUsernameProblemLanguageResultExecution timeMemory
796988ThMinh_Cake 3 (JOI19_cake3)C++14
100 / 100
825 ms206280 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, inf = 2e18; ll s[N]; struct it { int cnt; ll sum; it* lf; it* rt; it() : cnt(), sum(), lf(), rt() { } void build(int f = 1, int l = n) { if(f == l) return; int mid = f + l >> 1; lf = new it(); rt = new it(); lf -> build(f, mid); rt -> build(mid + 1, l); } void up(int i, ll val, int f = 1, int l = n) { if(f == l) { cnt = 1; sum = val; return; } int mid = f + l >> 1; if(i <= mid) { lf = new it(*lf); lf -> up(i, val, f, mid); } else { rt = new it(*rt); 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 -> sum; } int cnt(it* id) { return id -> cnt; } ll get(it* id1, it* id2, int f = 1, int l = n, int k = m - 2) { 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) return val + get(id1 -> lf, id2 -> lf, f, mid, k - res); return get(id1 -> rt, id2 -> rt, mid + 1, l, k); } struct piece { ll v, c; int pos; }; 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 = 0; ll &a = s[mid]; forin(i, from, min(to, mid)) { ll val = -1e18; if(mid - i + 1 >= m) { val = get(tr[i], tr[mid - 1]) + p[mid].v + p[i].v - 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].pos = i; sort(p + 1, p + n + 1, [] (piece a, piece b) { return a.c < b.c; }); tr.resize(n + 1); tr[0] = new it(); tr[0] -> build(); forin(i, 1, n) { tr[i] = new it(*tr[i - 1]); tr[i] -> up(p[i].pos, p[i].v); s[i] = -inf; } solve(); ans = -inf; forin(i, 1, n) ans = max(ans, s[i]); cout<<ans; }

Compilation message (stderr)

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