이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 2e6;
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
{
ll cnt, sum;
Node *lc, *rc;
Node() : cnt(0), sum(0), lc(0), rc(0) {}
};
Node nodes[MAXS+10]; int cnt=0;
Node *tree[MAXN+10];
Node *newNode()
{
return &nodes[cnt++];
}
void makeTree(Node *node, int tl, int tr)
{
if(tl==tr) return;
int mid=tl+tr>>1;
node->lc=newNode();
node->rc=newNode();
makeTree(node->lc, tl, mid);
makeTree(node->rc, mid+1, tr);
}
Node *addTree(Node *node, int tl, int tr, int pos)
{
Node *ret=newNode();
if(pos<tl || tr<pos) return node;
if(tl==tr)
{
ret->cnt=(node->cnt)+1;
ret->sum=(node->sum)+comp[pos];
return ret;
}
int mid=tl+tr>>1;
ret->lc=addTree(node->lc, tl, mid, pos);
ret->rc=addTree(node->rc, mid+1, tr, pos);
ret->cnt=(ret->lc->cnt)+(ret->rc->cnt);
ret->sum=(ret->lc->sum)+(ret->rc->sum);
return ret;
}
ll query(Node *nodel, Node *noder, int tl, int tr, int k)
{
if(tl==tr) return comp[tl]*min((ll)k, (noder->cnt)-(nodel->cnt));
int mid=tl+tr>>1;
ll t=(noder->rc->cnt)-(nodel->rc->cnt);
if(t>=k) return query(nodel->rc, noder->rc, mid+1, tr, k);
else return query(nodel->lc, noder->lc, tl, mid, k-t)+(noder->rc->sum)-(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; 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;
}
컴파일 시 표준 에러 (stderr) 메시지
holiday.cpp: In function 'void makeTree(Node*, int, int)':
holiday.cpp:34:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
holiday.cpp: In function 'Node* addTree(Node*, int, int, int)':
holiday.cpp:51:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
holiday.cpp: In function 'll query(Node*, Node*, int, int, int)':
holiday.cpp:62:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
holiday.cpp: In function 'void solve(int, int, int, int)':
holiday.cpp:74:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int smid=sl+sr>>1, emid; ll val=-1;
~~^~~
holiday.cpp:73:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
holiday.cpp: In function 'll findMaxAttraction(int, int, int, int*)':
holiday.cpp:89: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:89: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:87:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
holiday.cpp: In function 'void solve(int, int, int, int)':
holiday.cpp:81:10: warning: 'emid' may be used uninitialized in this function [-Wmaybe-uninitialized]
solve(sl, smid-1, el, emid);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |