Submission #153336

#TimeUsernameProblemLanguageResultExecution timeMemory
153336arnold518Holiday (IOI14_holiday)C++14
100 / 100
556 ms49864 KiB
#include "holiday.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e5; const int MAXS = 20e5; int N, S, D, A[MAXN+10]; vector<int> comp; int getcomp(int x) { return lower_bound(comp.begin(), comp.end(), x)-comp.begin(); } ll ans; struct Node { int cnt; ll sum; int lc, rc; Node() : cnt(0), sum(0), lc(-1), rc(-1) {} }; Node nodes[MAXS]; int cnt=0; int tree[MAXN+10]; int newNode() { return cnt++; } void makeTree(int node, int tl, int tr) { if(tl==tr) return; int mid=tl+tr>>1; nodes[node].lc=newNode(); nodes[node].rc=newNode(); makeTree(nodes[node].lc, tl, mid); makeTree(nodes[node].rc, mid+1, tr); } int addTree(int node, int tl, int tr, int pos) { if(pos<tl || tr<pos) return node; int ret=newNode(); if(tl==tr) { nodes[ret].cnt=(nodes[node].cnt)+1; nodes[ret].sum=(nodes[node].sum)+comp[pos]; return ret; } int mid=tl+tr>>1; nodes[ret].lc=addTree(nodes[node].lc, tl, mid, pos); nodes[ret].rc=addTree(nodes[node].rc, mid+1, tr, pos); nodes[ret].cnt=(nodes[nodes[ret].lc].cnt)+(nodes[nodes[ret].rc].cnt); nodes[ret].sum=(nodes[nodes[ret].lc].sum)+(nodes[nodes[ret].rc].sum); return ret; } ll query(int nodel, int noder, int tl, int tr, int k) { if(tl==tr) return comp[tl]*min((ll)k, (ll)(nodes[noder].cnt)-(nodes[nodel].cnt)); int mid=tl+tr>>1; ll t=(nodes[nodes[noder].rc].cnt)-(nodes[nodes[nodel].rc].cnt); if(t>=k) return query(nodes[nodel].rc, nodes[noder].rc, mid+1, tr, k); else return query(nodes[nodel].lc, nodes[noder].lc, tl, mid, k-t)+(nodes[nodes[noder].rc].sum)-(nodes[nodes[nodel].rc].sum); } int dist(int l, int r) { return D-(r-l+min(abs(S-l), abs(r-S))); } void solve(int sl, int sr, int el, int er) { if(sl>sr) return; int i, j; int smid=sl+sr>>1, emid=er; ll val=-1; for(i=max(smid, el); i<=er; i++) { ll t=query(tree[smid-1], tree[i], 1, comp.size()-1, dist(smid, i)); if(t>val) val=t, emid=i; } ans=max(ans, val); solve(sl, smid-1, el, emid); solve(smid+1, sr, emid, er); } ll findMaxAttraction(int _N, int _S, int _D, int *_A) { int i, j; N=_N; S=_S; D=_D; S++; for(i=1; i<=N; i++) A[i]=_A[i-1], comp.push_back(A[i]); comp.push_back(-1); sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); tree[0]=newNode(); makeTree(tree[0], 1, comp.size()-1); for(i=1; i<=N; i++) tree[i]=addTree(tree[i-1], 1, comp.size()-1, getcomp(A[i])); solve(1, N, 1, N); return ans; }

Compilation message (stderr)

holiday.cpp: In function 'void makeTree(int, int, int)':
holiday.cpp:31:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=tl+tr>>1;
             ~~^~~
holiday.cpp: In function 'int addTree(int, int, int, int)':
holiday.cpp:48:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=tl+tr>>1;
             ~~^~~
holiday.cpp: In function 'll query(int, int, int, int, int)':
holiday.cpp:59:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=tl+tr>>1;
             ~~^~~
holiday.cpp: In function 'void solve(int, int, int, int)':
holiday.cpp:71:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int smid=sl+sr>>1, emid=er; ll val=-1;
              ~~^~~
holiday.cpp:70:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
holiday.cpp: In function 'll findMaxAttraction(int, int, int, int*)':
holiday.cpp:86:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for(i=1; i<=N; i++) A[i]=_A[i-1], comp.push_back(A[i]); comp.push_back(-1);
     ^~~
holiday.cpp:86:61: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     for(i=1; i<=N; i++) A[i]=_A[i-1], comp.push_back(A[i]); comp.push_back(-1);
                                                             ^~~~
holiday.cpp:84:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...