#include "minerals.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0; i<(n); ++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
int query(int x) {
return Query(x+1);
}
void Solve(int n) {
if (0 && 4*n*n <= 1000000) {
for(int i=0; i<2*n; ++i) {
query(i);
for(int j=i+1; j<2*n; ++j) {
int x = query(j);
//cout<<"? "<<i<<' '<<j<<' '<<x<<'\n';
if (x>1) query(j);
else {
Answer(i+1,j+1);
query(j);
break;
}
}
query(i);
}
return;
}
vector<int> in(2*n), c(2*n);
vector<int> f,s;
int last=0;
forn(i,2*n) {
int z = query(i);
if (z > last) f.pb(i);
else s.pb(i);
in[i]=1;
last = z;
}
forn(i,n) c[f[i]]=i;
vector<pi> v(n);
forn(i,n) v[i]={0,n-1};
multiset<int> st;
forn(i,n) st.insert(n/2);
vector<vector<int>> ok(n);
forn(i,n) ok[n/2].pb(i);
set<int> alive;
forn(i,n) alive.insert(i);
vector<vector<int>> other(n);
//cout<<"f: "; for(auto&x:f) cout<<x<<' '; cout<<'\n';
//cout<<"s: "; for(auto&x:s) cout<<x<<' '; cout<<'\n';
//return;
int dir = -1;
int pos=n-1;
while (st.size()) {
//cout<<"? "<<pos<<' '<<dir<<'\n'; cout.flush();
//for(auto&x:st) cout<<x<<' '; cout<<'\n';
//for(auto&x:alive) cout<<x<<' '; cout<<'\n';
//cout.flush();
if (dir == 1) {
auto it = st.lower_bound(pos);
if (it==st.end()) {
assert(0);
dir *= -1;
continue;
}
for(auto&x:ok[pos]) {
st.erase(st.find(pos));
int l=v[x].f, r=v[x].s;
if (l==r) continue;
int m=(l+r+1)>>1;
int q = query(s[x]);
int z = -1;
if (r-l==1) {
z = l;
}
if (in[s[x]]) {
if (q < last) {
l = m;
} else {
r = m-1;
}
} else {
if (q > last) {
l = m;
} else {
r = m-1;
}
}
in[s[x]]^=1;
last = q;
v[x] = {l,r};
m = (l+r+1)>>1;
if (z!=-1) {
assert(l==r);
int a=other[z][0],b=other[z][1];
assert(x==a || x==b);
if (x==a) {
if (r==z) v[b]={z+1,z+1};
else v[b]={z,z};
} else {
if (r==z) v[a]={z+1,z+1};
else v[a]={z,z};
}
alive.erase(z);
alive.erase(z+1);
}
if (l!=r) {
ok[m].pb(x);
st.insert(m);
}
if (r-l==1) {
other[l].pb(x);
}
}
ok[pos].clear();
it = st.lower_bound(pos);
if (it==st.end()) {
dir*=-1;
continue;
}
last = query(f[pos]);
auto it2 = alive.upper_bound(pos);
pos = *it2;
} else {
auto it = st.lower_bound(pos);
if (it == st.begin() && (*it) > pos) {
assert(0);
dir *= -1;
continue;
}
last = query(f[pos]);
for(auto&x:ok[pos]) {
//cout<<"??? "<<pos<<' '<<x<<'\n'; cout.flush();
//cout<<st.size()<<'\n';
//for(auto&x:st) cout<<x<<' '; cout<<'\n'; cout.flush();
st.erase(st.find(pos));
int l=v[x].f, r=v[x].s;
if (l==r) continue;
int m=(l+r+1)>>1;
int q = query(s[x]);
int z = -1;
if (r-l==1) {
z = l;
}
//cout<<"? "<<pos<<' '<<x<<' '<<s[x]<<' '<<in[s[x]]<<' '<<last<<' '<<q<<'\n';
if (in[s[x]]) {
if (q < last) {
l = m;
} else {
r = m-1;
}
} else {
if (q > last) {
l = m;
} else {
r = m-1;
}
}
in[s[x]]^=1;
last = q;
v[x] = {l,r};
m = (l+r+1)>>1;
if (z!=-1) {
assert(l==r);
int a=other[z][0],b=other[z][1];
//cout<<"?! "<<z<<' '<<a<<' '<<b<<" "<<x<<' '<<l<<' '<<r<<'\n';
assert(x==a || x==b);
if (x==a) {
if (r==z) v[b]={z+1,z+1};
else v[b]={z,z};
} else {
if (r==z) v[a]={z+1,z+1};
else v[a]={z,z};
}
alive.erase(z);
alive.erase(z+1);
}
if (l!=r) {
ok[m].pb(x);
st.insert(m);
}
if (r-l==1) {
other[l].pb(x);
}
}
ok[pos].clear();
//cout<<"? "<<pos<<'\n'; cout.flush();
it = st.lower_bound(pos);
if (it == st.begin()) {
dir *= -1;
continue;
}
auto it2 = alive.lower_bound(pos);
--it2;
pos = *it2;
}
}
//forn(i,n) cout<<s[i]<<": "<<v[i].f<<' '<<v[i].s<<'\n';
forn(i,n) Answer(s[i]+1,f[v[i].f]+1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
5 ms |
856 KB |
Output is correct |
3 |
Correct |
11 ms |
1368 KB |
Output is correct |
4 |
Correct |
25 ms |
2372 KB |
Output is correct |
5 |
Correct |
51 ms |
4144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
19 |
Correct |
167 ms |
11364 KB |
Output is correct |
20 |
Correct |
168 ms |
11364 KB |
Output is correct |
21 |
Correct |
161 ms |
11212 KB |
Output is correct |
22 |
Correct |
143 ms |
11112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
19 |
Correct |
167 ms |
11364 KB |
Output is correct |
20 |
Correct |
168 ms |
11364 KB |
Output is correct |
21 |
Correct |
161 ms |
11212 KB |
Output is correct |
22 |
Correct |
143 ms |
11112 KB |
Output is correct |
23 |
Incorrect |
171 ms |
11224 KB |
Wrong Answer [2] |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
19 |
Correct |
167 ms |
11364 KB |
Output is correct |
20 |
Correct |
168 ms |
11364 KB |
Output is correct |
21 |
Correct |
161 ms |
11212 KB |
Output is correct |
22 |
Correct |
143 ms |
11112 KB |
Output is correct |
23 |
Incorrect |
171 ms |
11224 KB |
Wrong Answer [2] |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
19 |
Correct |
167 ms |
11364 KB |
Output is correct |
20 |
Correct |
168 ms |
11364 KB |
Output is correct |
21 |
Correct |
161 ms |
11212 KB |
Output is correct |
22 |
Correct |
143 ms |
11112 KB |
Output is correct |
23 |
Incorrect |
171 ms |
11224 KB |
Wrong Answer [2] |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
856 KB |
Output is correct |
7 |
Correct |
11 ms |
1368 KB |
Output is correct |
8 |
Correct |
25 ms |
2372 KB |
Output is correct |
9 |
Correct |
51 ms |
4144 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
40 ms |
3160 KB |
Output is correct |
12 |
Correct |
56 ms |
4084 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
14 |
Correct |
46 ms |
4056 KB |
Output is correct |
15 |
Correct |
157 ms |
11084 KB |
Output is correct |
16 |
Correct |
195 ms |
11092 KB |
Output is correct |
17 |
Correct |
144 ms |
10872 KB |
Output is correct |
18 |
Correct |
139 ms |
10832 KB |
Output is correct |
19 |
Correct |
167 ms |
11364 KB |
Output is correct |
20 |
Correct |
168 ms |
11364 KB |
Output is correct |
21 |
Correct |
161 ms |
11212 KB |
Output is correct |
22 |
Correct |
143 ms |
11112 KB |
Output is correct |
23 |
Incorrect |
171 ms |
11224 KB |
Wrong Answer [2] |
24 |
Halted |
0 ms |
0 KB |
- |