untitle

public
yousefkarem91 Sep 01, 2024 60 days 44
Clone
C++ past.cpp 78 lines (73 loc) | 1.88 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
28
using item=int;
29
struct segTree {
30
int sz = 1;
31
vector<item> values;
32
segTree(int n) {
33
while (sz < n)
34
sz *= 2;
35
values.resize(2 * sz, -1);
36
}
37
item query(int k, int i, int lx, int rx) {
38
if(values[i]==-1)
39
values[i]=ask(lx,rx-1);
40
if (lx+1 ==rx) {
41
values[i]=0;
42
return lx;
43
}
44
int m = (lx + rx) / 2;
45
if(values[2 * i + 1]==-1)
46
values[2 * i + 1]=ask(lx,m-1);
47
values[2 * i + 2]=values[i]-values[2 * i + 1];
48
values[i]--;
49
if(values[2 * i + 1]>=k)
50
{
51
return (query(k,2 * i + 1,lx,m));
52
}
53
return query(k-values[2 * i + 1], 2 * i + 2, m, rx);
54
}
55
56
item query( int k) {
57
return query(k, 0, 0, n);
58
}
59
};
60
void solve() {
61
int t;
62
cin>>n>>t;
63
segTree seg (n);
64
while(t--) {
65
int k;cin>>k;
66
int idx=seg.query(k);
67
cout<<"! "<<idx+1<<endl;
68
}
69
}
70
71
signed main() {
72
JOO();
73
// int cnt=1;
74
// test
75
{
76
// cout << "Case " << "#" << cnt++ << ": ";
77
solve();
78
}
79
}