untitle

public
yousefkarem91 Sep 01, 2024 69 days 44
Clone
C++ past.cpp 83 lines (79 loc) | 2 KB
1
2
#include<bits/stdc++.h>
3
using namespace std;
4
#define ll long long
5
#define all(d) d.begin(),d.end()
6
#define test int t;cin>>t;while(t--)
7
#define ones(n) __builtin_popcount(n)
8
#define last(n) ____builtin_clz(len)
9
const int dx8[8] = {1, 0, -1, 0, -1, -1, 1, 1};
10
const int dy8[8] = {0, 1, 0, -1, -1, 1, -1, 1};
11
int dx[] = {+0, +0, +1, -1};
12
int dy[] = {+1, -1, +0, +0};
13
char di[] = {'R', 'L', 'D', 'U'};
14
15
void JOO() {
16
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
17
// #ifndef ONLINE_JUDGE
18
// freopen("input.txt", "r", stdin);
19
// freopen("output.txt", "w", stdout);
20
// #endif
21
}
22
23
const int N = 2e5 + 5, M = 1e3, LOG = 20, inf = 0x3f3f3f3f;
24
int MOD = 1e9 + 7;
25
ll infLL = 0x3f3f3f3f3f3f3f3f;
26
int n;
27
int ask(int i,int j) {
28
cout<<"? "<<i+1<<' '<<min(n,j+1)<<endl;
29
int val;
30
cin>>val;
31
return ((j-i+1)-val);
32
}
33
using item=int;
34
struct segTree {
35
int sz = 1;
36
vector<item> values;
37
segTree(int n) {
38
while (sz < n)
39
sz *= 2;
40
values.resize(2 * sz, -1);
41
}
42
item query(int k, int i, int lx, int rx) {
43
if(values[i]==-1)
44
values[i]=ask(lx,rx-1);
45
if (lx+1 ==rx) {
46
values[i]=0;
47
return lx;
48
}
49
int m = (lx + rx) / 2;
50
if(values[2 * i + 1]==-1)
51
values[2 * i + 1]=ask(lx,m-1);
52
values[2 * i + 2]=values[i]-values[2 * i + 1];
53
values[i]--;
54
if(values[2 * i + 1]>=k)
55
{
56
return (query(k,2 * i + 1,lx,m));
57
}
58
return query(k-values[2 * i + 1], 2 * i + 2, m, rx);
59
}
60
61
item query( int k) {
62
return query(k, 0, 0, n);
63
}
64
};
65
void solve() {
66
int t;
67
cin>>n>>t;
68
segTree seg (n);
69
while(t--) {
70
int k;cin>>k;
71
int idx=seg.query(k);
72
cout<<"! "<<idx+1<<endl;
73
}
74
}
75
76
signed main() {
77
JOO();
78
// int cnt=1;
79
// test
80
{
81
// cout << "Case " << "#" << cnt++ << ": ";
82
solve();
83
}
84
}