#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
vector <int> a[200000];
int ch[200000],ch2[200000],st[800001],f[200001];
void build(int node, int l, int r){
st[node]=r-l+1;
if (l==r)
return;
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
}
void update(int node, int l, int r, int i){
if (l>i||r<i)
return;
if (l==r){
st[node]=0;
return;
}
int mid=(l+r)>>1;
update(node<<1,l,mid,i);
update(node<<1|1,mid+1,r,i);
st[node]=st[node<<1]+st[node<<1|1];
}
int get(int node, int l, int r, int u, int v){
if (l>v||r<u||l>r)
return 0;
if (l>=u&&r<=v)
return st[node];
int mid=(l+r)>>1;
return get(node<<1,l,mid,u,v)+get(node<<1|1,mid+1,r,u,v);
}
int get(int node, int l, int r, int x){
if (l==r)
return l;
int mid=(l+r)>>1;
return (st[node<<1]>=x?get(node<<1,l,mid,x):get(node<<1|1,mid+1,r,x-st[node<<1]));
}
void query(int i){
if (a[i].empty())
a[i]=ask(i);
}
void update(int i){
ch2[i]=1;
i++;
for (;i<=200000;i+=i&(-i))
f[i]++;
}
int get(int i){
i++;
int res=0;
for (;i;i-=i&(-i))
res+=f[i];
return res;
}
int b[4]={1,5,26,472};
int best(vector <int> v, int idx){
memset(ch,0,sizeof(ch));
memset(ch2,0,sizeof(ch2));
memset(f,0,sizeof(f));
if (v.size()==1)
return v[0];
if (v.size()<=b[idx]){
for (int i=0;i<v.size();i++){
query(v[i]);
if (!a[v[i]][0]&&!a[v[i]][1])
return v[i];
}
}
int n=v.size(),mx=0;
build(1,0,n-1);
for (int i=0;i<b[idx];i++){
query(v[i]);
if (!a[v[i]][0]&&!a[v[i]][1])
return v[i];
mx=max(mx,a[v[i]][0]+a[v[i]][1]);
}
int cur=0;
for (int i=0;i<b[idx];i++){
update(1,0,n-1,i);
if (a[v[i]][0]+a[v[i]][1]==mx){
cur=i;
break;
}
else
update(i);
}
priority_queue <int, vector <int>, greater <int>> q;
vector <int> v2;
while (a[v[cur]][1]<n-cur-1){
while (!q.empty()){
if (a[v[q.top()]][0]==get(q.top())){
cur=q.top();
q.pop();
}
else
break;
}
vector <int> ve;
int l=1,r=get(1,0,n-1,cur,(q.empty()?n-1:q.top()-1)),val=get(1,0,n-1,0,cur-1);
if (!q.empty()&&q.top()-cur-1==a[v[q.top()]][0]-a[v[cur]][0]){
for (int i=cur+1;i<q.top();i++){
v2.push_back(v[i]);
update(1,0,n-1,i);
if (!ch2[i])
update(i);
}
continue;
}
while (l<=r){
int mid=(l+r)>>1;
int pos=get(1,0,n-1,val+mid);
query(v[pos]);
ve.push_back(pos);
if (!a[v[pos]][0]&&!a[v[pos]][1])
return v[pos];
if (a[v[pos]][0]+a[v[pos]][1]==mx){
if (a[v[pos]][0]>get(pos)){
if (!ch[pos])
q.push(pos);
ch[pos]=1;
r=mid-1;
}
else{
cur=pos;
break;
}
continue;
}
update(pos);
v2.push_back(v[pos]);
break;
}
for (int i:ve)
update(1,0,n-1,i);
}
for (int i=cur+1;i<n;i++)
v2.push_back(v[i]);
sort(v2.begin(),v2.end());
v2.resize(unique(v2.begin(),v2.end())-v2.begin());
return best(v2,idx-1);
}
int find_best(int n){
vector <int> v;
for (int i=0;i<n;i++)
v.push_back(i);
return best(v,3);
}
Compilation message
prize.cpp: In function 'int best(std::vector<int>, int)':
prize.cpp:64:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
64 | if (v.size()<=b[idx]){
| ~~~~~~~~^~~~~~~~
prize.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12236 KB |
Output is correct |
2 |
Correct |
5 ms |
12236 KB |
Output is correct |
3 |
Correct |
6 ms |
12236 KB |
Output is correct |
4 |
Correct |
6 ms |
12236 KB |
Output is correct |
5 |
Correct |
5 ms |
12236 KB |
Output is correct |
6 |
Correct |
4 ms |
12236 KB |
Output is correct |
7 |
Correct |
6 ms |
12236 KB |
Output is correct |
8 |
Correct |
5 ms |
12236 KB |
Output is correct |
9 |
Correct |
6 ms |
12236 KB |
Output is correct |
10 |
Correct |
6 ms |
12236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12236 KB |
Output is correct |
2 |
Correct |
5 ms |
12236 KB |
Output is correct |
3 |
Correct |
5 ms |
12236 KB |
Output is correct |
4 |
Correct |
6 ms |
12076 KB |
Output is correct |
5 |
Correct |
5 ms |
12236 KB |
Output is correct |
6 |
Correct |
3 ms |
12236 KB |
Output is correct |
7 |
Correct |
5 ms |
12488 KB |
Output is correct |
8 |
Correct |
6 ms |
12320 KB |
Output is correct |
9 |
Correct |
6 ms |
12236 KB |
Output is correct |
10 |
Correct |
7 ms |
12236 KB |
Output is correct |
11 |
Correct |
8 ms |
12236 KB |
Output is correct |
12 |
Correct |
6 ms |
12236 KB |
Output is correct |
13 |
Correct |
7 ms |
12236 KB |
Output is correct |
14 |
Correct |
6 ms |
10584 KB |
Output is correct |
15 |
Correct |
9 ms |
12236 KB |
Output is correct |
16 |
Correct |
22 ms |
12580 KB |
Output is correct |
17 |
Correct |
5 ms |
12236 KB |
Output is correct |
18 |
Correct |
22 ms |
12572 KB |
Output is correct |
19 |
Correct |
5 ms |
12236 KB |
Output is correct |
20 |
Correct |
9 ms |
11528 KB |
Output is correct |
21 |
Correct |
24 ms |
12308 KB |
Output is correct |
22 |
Correct |
7 ms |
12236 KB |
Output is correct |
23 |
Correct |
6 ms |
12236 KB |
Output is correct |
24 |
Correct |
8 ms |
12232 KB |
Output is correct |
25 |
Correct |
18 ms |
12236 KB |
Output is correct |
26 |
Correct |
23 ms |
12552 KB |
Output is correct |
27 |
Correct |
7 ms |
12236 KB |
Output is correct |
28 |
Correct |
23 ms |
12564 KB |
Output is correct |
29 |
Correct |
18 ms |
12236 KB |
Output is correct |
30 |
Correct |
27 ms |
12812 KB |
Output is correct |
31 |
Correct |
5 ms |
12236 KB |
Output is correct |
32 |
Correct |
6 ms |
12236 KB |
Output is correct |
33 |
Correct |
2 ms |
10584 KB |
Output is correct |
34 |
Correct |
25 ms |
12804 KB |
Output is correct |
35 |
Correct |
7 ms |
12236 KB |
Output is correct |
36 |
Correct |
9 ms |
12236 KB |
Output is correct |
37 |
Correct |
6 ms |
12236 KB |
Output is correct |
38 |
Correct |
6 ms |
12432 KB |
Output is correct |
39 |
Correct |
13 ms |
12236 KB |
Output is correct |
40 |
Correct |
22 ms |
12312 KB |
Output is correct |
41 |
Correct |
18 ms |
12236 KB |
Output is correct |
42 |
Correct |
15 ms |
12236 KB |
Output is correct |
43 |
Correct |
29 ms |
12296 KB |
Output is correct |
44 |
Correct |
11 ms |
12236 KB |
Output is correct |
45 |
Correct |
10 ms |
12236 KB |
Output is correct |
46 |
Correct |
5 ms |
12236 KB |
Output is correct |
47 |
Correct |
14 ms |
12236 KB |
Output is correct |
48 |
Correct |
22 ms |
12236 KB |
Output is correct |
49 |
Correct |
25 ms |
12312 KB |
Output is correct |
50 |
Correct |
27 ms |
12556 KB |
Output is correct |
51 |
Correct |
13 ms |
12236 KB |
Output is correct |
52 |
Correct |
6 ms |
12236 KB |
Output is correct |
53 |
Correct |
6 ms |
12236 KB |
Output is correct |
54 |
Correct |
36 ms |
12296 KB |
Output is correct |
55 |
Correct |
6 ms |
12236 KB |
Output is correct |
56 |
Correct |
23 ms |
12580 KB |
Output is correct |
57 |
Correct |
27 ms |
12548 KB |
Output is correct |
58 |
Correct |
20 ms |
12236 KB |
Output is correct |
59 |
Correct |
19 ms |
12236 KB |
Output is correct |
60 |
Correct |
15 ms |
12236 KB |
Output is correct |
61 |
Correct |
6 ms |
12236 KB |
Output is correct |
62 |
Correct |
7 ms |
12236 KB |
Output is correct |
63 |
Correct |
6 ms |
12236 KB |
Output is correct |
64 |
Correct |
7 ms |
12076 KB |
Output is correct |
65 |
Correct |
6 ms |
12236 KB |
Output is correct |
66 |
Correct |
7 ms |
12236 KB |
Output is correct |
67 |
Correct |
7 ms |
12080 KB |
Output is correct |
68 |
Correct |
5 ms |
12236 KB |
Output is correct |
69 |
Correct |
6 ms |
12236 KB |
Output is correct |
70 |
Correct |
6 ms |
12236 KB |
Output is correct |
71 |
Correct |
31 ms |
12564 KB |
Output is correct |
72 |
Correct |
8 ms |
12236 KB |
Output is correct |
73 |
Correct |
35 ms |
12560 KB |
Output is correct |
74 |
Correct |
35 ms |
12324 KB |
Output is correct |
75 |
Correct |
6 ms |
12236 KB |
Output is correct |
76 |
Correct |
35 ms |
12552 KB |
Output is correct |
77 |
Correct |
30 ms |
12572 KB |
Output is correct |
78 |
Correct |
8 ms |
12236 KB |
Output is correct |
79 |
Correct |
14 ms |
12236 KB |
Output is correct |
80 |
Correct |
26 ms |
12572 KB |
Output is correct |
81 |
Correct |
31 ms |
12308 KB |
Output is correct |
82 |
Correct |
24 ms |
12572 KB |
Output is correct |
83 |
Correct |
7 ms |
12236 KB |
Output is correct |
84 |
Correct |
25 ms |
12236 KB |
Output is correct |
85 |
Correct |
31 ms |
12740 KB |
Output is correct |
86 |
Correct |
7 ms |
12236 KB |
Output is correct |
87 |
Correct |
6 ms |
12236 KB |
Output is correct |
88 |
Correct |
6 ms |
12236 KB |
Output is correct |
89 |
Correct |
5 ms |
12236 KB |
Output is correct |
90 |
Correct |
6 ms |
12236 KB |
Output is correct |
91 |
Correct |
6 ms |
12236 KB |
Output is correct |
92 |
Correct |
6 ms |
12236 KB |
Output is correct |
93 |
Correct |
7 ms |
12236 KB |
Output is correct |
94 |
Correct |
9 ms |
12236 KB |
Output is correct |
95 |
Correct |
7 ms |
12236 KB |
Output is correct |
96 |
Correct |
7 ms |
12236 KB |
Output is correct |
97 |
Correct |
6 ms |
12236 KB |
Output is correct |