#include "Anna.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
const int maxn = 1e4+5;
const int D = 13;
int Q = 18;
int sz,pos,nxt,T[maxn];
int l,r,s,t,d;
int N,L,R;
int f(int x){return (x==1?0:32-__builtin_clz(x-1));}
string get(int k,int d){
string S;
if(d==0) S+="000";
else if(d==1) S+="001";
else for(int i=3;i>=0;i--) S+=char('0'+((d+2)>>i&1));
for(int i=d-1;i>=0;i--) S+=char('0'+(k>>i&1));
return S;
}
}
void InitA(int N, int L, int R) {
::N = N;::L = L;::R = R;
l=0,r=N;
int k=1;
while(d<D){
int m=(l+r)>>1;
if(R<m) r=m,k<<=1;
else if(m<L) l=m+1,k=k<<1|1;
else break;
d++;
}
//cout << "Anna " << l << ' ' << r << ' ' << k << ' ' << d << '\n';
string S=get(k,d);
for(int i=0;i<(int)S.size();i++) SendA(S[i]-'0');
Q-=(int)S.size();
int m=(l+r)>>1;
s=t=m;sz=pos=0;
if(d<D && (r-l)>=2) nxt=f(s-l+1);
else nxt=-1;
}
void ReceiveA(bool x) {
T[sz++]=x;
while(sz==nxt){
Q--;
int k=f(s-l+1),p=0;
for(int i=0;i<k;i++) p|=T[pos++]<<i;
p+=l;
int a=t-s,b=r-l;
int m=(a+b-1)>>1;
int q=p+m+1;
//cout << "ReceiveA " << p << ' ' << q << '\n';
if(p<=L && R<q){
//cout << 1 << '\n';
SendA(1);
l=p,r=q;
}
else{
//cout << 0 << '\n';
SendA(0);
s=p,t=q;
}
//cout << "ReceiveA " << l << ' ' << r << ' ' << s << ' ' << t << '\n';
if(Q>0 && (s-l)+(r-t)>=2) nxt=sz+f(s-l+1);
else nxt=-1;
}
}
int Answer(){
int p=-1;
if(s!=t){
int k=f(t-s);p=s;
for(int i=0;i<k;i++) p+=T[pos++]<<i;
}
vector<int> ord;
for(int i=l;i<s;i++) ord.push_back(i);
if(p!=-1) ord.push_back(p);
for(int i=t;i<r;i++) ord.push_back(i);
vector<int> st;
st.push_back(ord[0]);
int lst=1,ret=-1;
if(L<=ord[0] && ord[0]<=R) ret=ord[0];
while(pos<sz){
if(ret==-1 && L<=ord[lst] && ord[lst]<=R) ret=ord[lst];
if(T[pos++]) st.push_back(ord[lst++]);
else{
int x=st.back();st.pop_back();
if(L<=ord[lst] && ord[lst]<=R && ret==x) ret=ord[lst];
}
}
return ret;
}
#include "Bruno.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
const int maxn = 1e6+5;
const int D = 13;
int Q = 18;
int N,sz=0,pos=0,T[maxn];
int l=-1,r=-1,s,t,d=-1,k=-1;
int f(int x){return (x==1?0:32-__builtin_clz(x-1));}
vector<int> P,ord;
}
void InitB(int N, std::vector<int> P) {
::N=N;::P=P;
l=r=d=k=-1;
sz=pos=0;
}
void ReceiveB(bool y) {
T[sz++]=y;
if(d==-1){
int cc=0;
for(int i=0;i<sz;i++) cc=cc<<1|T[i];
if(sz==3 && cc<=1) d=cc;
else if(sz==4) d=cc-2;
}
if(d==-1) return;
if(k==-1){
pos=3+(d>1);
if(sz==pos+d){
k=1;
for(int i=0;i<d;i++) k=k<<1|T[pos+i];
pos=sz;
}
}
if(k==-1) return;
if(l==-1){
l=0,r=N;
for(int i=d-1;i>=0;i--){
int m=(l+r)>>1;
if(k>>i&1) l=m+1;
else r=m;
}
int m=(l+r)>>1;
ord.push_back(m);
int a=m-1,b=m+1;
while(a>=l || b<r){
if(a<l || (b<r && P[b]>=P[a])) ord.push_back(b++);
else ord.push_back(a--);
}
s=t=m;
//cout << "Bruno " << l << ' ' << r << ' ' << k << ' ' << d << '\n';
}
if(pos<sz){
int a=t-s,b=r-l;
int m=(a+b-1)>>1;
//cout << "ReceiveB " << pos << ' ' << T[pos] << '\n';
if(T[pos++]){
if(ord[m]<=s) l=ord[m],r=l+m+1;
else r=ord[m]+1,l=r-m-1;
}
else{
if(ord[m]<=s) s=ord[m],t=s+m+1;
else t=ord[m]+1,s=t-m-1;
}
//cout << "ReceiveB " << l << ' ' << r << ' ' << s << ' ' << t << '\n';
}
if(d<D && sz<Q && (s-l)+(r-t)>=2){
int a=t-s,b=r-l;
int m=(a+b-1)>>1;
int p=ord[m]-(ord[m]>s)*m-l;
for(int i=0;i<f(s-l+1);i++) SendB(p>>i&1);
//cout << "SendB " << p << '\n';
}
else{
int c=s;
if(s<t){
for(int i=s;i<t;i++) if(P[i]<P[c]) c=i;
for(int i=0;i<f(t-s);i++) SendB((c-s)>>i&1);
}
vector<int> v;
for(int i=l;i<s;i++) v.push_back(i);
if(s<t) v.push_back(c);
for(int i=t;i<r;i++) v.push_back(i);
vector<int> st;
st.push_back(v[0]);
for(int i=1;i<(int)v.size();i++){
int x=v[i];
while(!st.empty() && P[st.back()]>P[x]) st.pop_back(),SendB(0);
st.push_back(x);SendB(1);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
664 KB |
Output is correct |
2 |
Correct |
0 ms |
664 KB |
Output is correct |
3 |
Correct |
2 ms |
664 KB |
Output is correct |
4 |
Correct |
2 ms |
664 KB |
Output is correct |
5 |
Correct |
0 ms |
664 KB |
Output is correct |
6 |
Correct |
1 ms |
664 KB |
Output is correct |
7 |
Correct |
2 ms |
664 KB |
Output is correct |
8 |
Correct |
2 ms |
664 KB |
Output is correct |
9 |
Correct |
0 ms |
664 KB |
Output is correct |
10 |
Correct |
0 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
1 ms |
664 KB |
Output is correct |
14 |
Correct |
0 ms |
664 KB |
Output is correct |
15 |
Correct |
1 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
0 ms |
664 KB |
Output is correct |
19 |
Correct |
2 ms |
664 KB |
Output is correct |
20 |
Correct |
1 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
664 KB |
Output is correct |
2 |
Correct |
0 ms |
664 KB |
Output is correct |
3 |
Correct |
2 ms |
664 KB |
Output is correct |
4 |
Correct |
2 ms |
664 KB |
Output is correct |
5 |
Correct |
0 ms |
664 KB |
Output is correct |
6 |
Correct |
1 ms |
664 KB |
Output is correct |
7 |
Correct |
2 ms |
664 KB |
Output is correct |
8 |
Correct |
2 ms |
664 KB |
Output is correct |
9 |
Correct |
0 ms |
664 KB |
Output is correct |
10 |
Correct |
0 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
1 ms |
664 KB |
Output is correct |
14 |
Correct |
0 ms |
664 KB |
Output is correct |
15 |
Correct |
1 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
0 ms |
664 KB |
Output is correct |
19 |
Correct |
2 ms |
664 KB |
Output is correct |
20 |
Correct |
1 ms |
664 KB |
Output is correct |
21 |
Correct |
1 ms |
920 KB |
Output is correct |
22 |
Correct |
1 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
920 KB |
Output is correct |
24 |
Correct |
2 ms |
664 KB |
Output is correct |
25 |
Correct |
1 ms |
664 KB |
Output is correct |
26 |
Correct |
2 ms |
1176 KB |
Output is correct |
27 |
Correct |
3 ms |
664 KB |
Output is correct |
28 |
Correct |
2 ms |
920 KB |
Output is correct |
29 |
Correct |
2 ms |
920 KB |
Output is correct |
30 |
Correct |
1 ms |
664 KB |
Output is correct |
31 |
Correct |
1 ms |
664 KB |
Output is correct |
32 |
Correct |
2 ms |
664 KB |
Output is correct |
33 |
Correct |
1 ms |
664 KB |
Output is correct |
34 |
Correct |
2 ms |
664 KB |
Output is correct |
35 |
Correct |
1 ms |
664 KB |
Output is correct |
36 |
Correct |
1 ms |
664 KB |
Output is correct |
37 |
Correct |
1 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
16332 KB |
Output is correct |
2 |
Correct |
69 ms |
12348 KB |
Output is correct |
3 |
Correct |
69 ms |
12328 KB |
Output is correct |
4 |
Correct |
77 ms |
16380 KB |
Output is correct |
5 |
Correct |
70 ms |
16528 KB |
Output is correct |
6 |
Correct |
69 ms |
16624 KB |
Output is correct |
7 |
Correct |
70 ms |
12424 KB |
Output is correct |
8 |
Correct |
75 ms |
12340 KB |
Output is correct |
9 |
Correct |
65 ms |
12260 KB |
Output is correct |
10 |
Correct |
64 ms |
12420 KB |
Output is correct |
11 |
Correct |
71 ms |
16612 KB |
Output is correct |
12 |
Correct |
73 ms |
12348 KB |
Output is correct |
13 |
Correct |
64 ms |
12276 KB |
Output is correct |
14 |
Correct |
132 ms |
12624 KB |
Output is correct |
15 |
Correct |
67 ms |
12528 KB |
Output is correct |
16 |
Correct |
72 ms |
12432 KB |
Output is correct |
17 |
Correct |
73 ms |
12332 KB |
Output is correct |
18 |
Correct |
87 ms |
12420 KB |
Output is correct |
19 |
Correct |
76 ms |
12352 KB |
Output is correct |
20 |
Correct |
68 ms |
12324 KB |
Output is correct |
21 |
Correct |
67 ms |
12496 KB |
Output is correct |
22 |
Correct |
67 ms |
12340 KB |
Output is correct |
23 |
Correct |
121 ms |
12356 KB |
Output is correct |
24 |
Correct |
66 ms |
12412 KB |
Output is correct |
25 |
Correct |
67 ms |
12424 KB |
Output is correct |
26 |
Correct |
68 ms |
12416 KB |
Output is correct |
27 |
Correct |
70 ms |
12340 KB |
Output is correct |
28 |
Correct |
83 ms |
12424 KB |
Output is correct |
29 |
Correct |
84 ms |
12340 KB |
Output is correct |
30 |
Correct |
68 ms |
12428 KB |
Output is correct |