Submission #136350

#TimeUsernameProblemLanguageResultExecution timeMemory
136350dndhkCake 3 (JOI19_cake3)C++14
100 / 100
1120 ms200224 KiB
#include <bits/stdc++.h> #define pb push_back #define all(v) ((v).begin(), (v).end()) #define sortv(v) sort(all(v)) #define sz(v) ((int)(v).size()) #define uniqv(v) (v).erase(unique(all(v)), (v).end()) #define umax(a, b) (a)=max((a), (b)) #define umin(a, b) (a)=min((a), (b)) #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define rep(i,n) FOR(i,1,n) #define rep0(i,n) FOR(i,0,(int)(n)-1) #define FI first #define SE second #define INF 2000000000 #define INFLL 1000000000000000000LL using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAX_N = 200000; int N, M; vector<pll> vt; ll ans = -INFLL; priority_queue<ll> pq1, pq2; int sz = 0; struct PST{ struct NODE{ int l=-1, r=-1; int cnt=0; ll sum=0; }; int SZ; vector<NODE> seg; int num = 0; vector<int> Tree; void Init(int x){ seg.resize(MAX_N*40); SZ = x; Tree.pb(num++); init(Tree[0], 0, SZ-1); } void init(int idx, int s, int e){ if(s==e) return; seg[idx].l = num++; seg[idx].r = num++; init(seg[idx].l, s, (s+e)/2); init(seg[idx].r, (s+e)/2+1, e); } void Update(int x, ll y){ Tree.pb(update(Tree.back(), 0, SZ-1, x, y)); } int update(int pidx, int s, int e, int x, ll y){ if(x<s || x>e) return pidx; int idx = num++; if(s==e){ seg[idx].sum = y; seg[idx].cnt = 1; return idx; } int k = update(seg[pidx].l, s, (s+e)/2, x, y); seg[idx].l = k; k = update(seg[pidx].r, (s+e)/2+1, e, x, y); seg[idx].r = k; seg[idx].sum = seg[seg[idx].l].sum + seg[seg[idx].r].sum; seg[idx].cnt = seg[seg[idx].l].cnt + seg[seg[idx].r].cnt; return idx; } ll Calc(int x, int y, int k){ return calc(Tree[x], Tree[y], 0, SZ-1, k); } ll calc(int idx1, int idx2, int s, int e, int k){ //cout<<idx1<<" "<<idx2<<" "<<s<<" "<<e<<" "<<k<<endl; //cout<<seg[idx2].cnt<<" "<<seg[idx1].cnt<<endl; if(seg[idx2].cnt - seg[idx1].cnt == k){ return seg[idx2].sum - seg[idx1].sum; } if(seg[seg[idx2].r].cnt - seg[seg[idx1].r].cnt < k){ return (seg[seg[idx2].r].sum - seg[seg[idx1].r].sum) + calc(seg[idx1].l, seg[idx2].l, s, (s+e)/2 , k - (seg[seg[idx2].r].cnt - seg[seg[idx1].r].cnt)); }else{ return calc(seg[idx1].r, seg[idx2].r, (s+e)/2+1, e, k); } } }; PST Pst; ll csum(int x, int y){ //cout<<"*"<<x<<" "<<y<<endl; return Pst.Calc(x, y+1, M); } void calc(int x, int y, int s, int e){ if(x>y) return; int m = (x+y)/2; ll mx = -INFLL, idx = 0; for(int i=e; i>=s; i--){ if(m-i+1<M) continue; ll c = csum(i, m); if(mx < c - 2 * (vt[m].first - vt[i].first)){ mx = c - 2 * (vt[m].first - vt[i].first); idx = i; } } ans = max(ans, mx); calc(x, m-1, s, idx); calc(m+1, y, idx, e); } vector<pll> upd; int main(){ scanf("%d%d", &N, &M); for(int i=0; i<N; i++){ ll a, b; scanf("%lld%lld", &a, &b); vt.pb({b, a}); } sort(vt.begin(), vt.end()); for(int i=0; i<N; i++){ upd.pb({vt[i].second, i}); } sort(upd.begin(), upd.end()); Pst.Init(N); for(int i=0; i<N; i++){ upd[i].first = upd[i].second; upd[i].second = i; } sort(upd.begin(), upd.end()); for(int i=0; i<N; i++){ Pst.Update(upd[i].second, vt[i].second); } calc(0, N-1, 0, N-1); cout<<ans; return 0; }

Compilation message (stderr)

cake3.cpp: In function 'int main()':
cake3.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...