#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define ff first
#define ss second
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int sz = 1e5 + 5;
ll m, n, q;
ll a[sz], b[sz];
int segsSize, lensSize;
vector<pll> segs;
vector<pair<pll, ll>> lensAll; //len, -idx, amt
vector<pll> lens[sz];
void input() {
scanf("%lld %lld %lld", &m, &n, &q);
foru(i, 0, n) {
scanf("%lld", &a[i]); a[i]--;
}
foru(i, 0, q) {
scanf("%lld", &b[i]); b[i]--;
}
if (a[0] != 0)segs.pb({ a[0],-1 });
foru(i, 0, n - 1) {
if (a[i + 1] - a[i] != 1)segs.pb({ a[i + 1] - a[i] - 1,i });
}
if (a[n - 1] != m - 1)segs.pb({ m - 1 - a[n - 1],n - 1 });
segsSize = segs.size();
}
void interpretSegs() {
map<ll, ll, greater<ll>> mem;
foru(idx, 0, segsSize) {
mem[segs[idx].ff] = 1;
while (!mem.empty()) {
pll x = *mem.begin(); mem.erase(x.first);
lens[idx].pb(x);
lensAll.pb({ {x.ff, -idx},x.ss });
ll amt = x.ff;
if (amt & 1) {
if (amt != 1)mem[amt >> 1] += (x.ss * 2);
}
else {
mem[amt >> 1] += x.ss;
if (amt != 2)mem[(amt >> 1) - 1] += x.ss;
}
}
}
}
ll countSegAmount(ll initialLen, ll val) {
if (initialLen <= val)return (initialLen == val);
unordered_map<ll, ll> mem, mem2;
mem[initialLen] = 1;
ll ret = 0;
while (!mem.empty()) {
for (pll x : mem) {
if (x.ff == val)ret += x.ss;
else if (x.ff > val) {
mem2[x.ff >> 1] += x.ss;
mem2[(x.ff - 1) >> 1] += x.ss;
}
}
mem = mem2;
mem2.clear();
}
return ret;
}
inline ll findIndex(ll serialNum, ll val, ll lseg, ll rseg) {
ll len = rseg - lseg + 1, mid = (lseg + rseg) / 2;
if (len == val)return mid;
ll cntLeft = countSegAmount((len - 1) / 2, val);
if (cntLeft >= serialNum) return findIndex(serialNum, val, lseg, mid - 1);
else return findIndex(serialNum - cntLeft, val, mid + 1, rseg);
}
inline pll findLR(int id) {
int aid = segs[id].ss;
if (aid == -1)return { 0,a[0] - 1 };
else if (aid == n - 1)return { a[aid] + 1,m - 1 };
else return { a[aid] + 1,a[aid + 1] - 1 };
}
int main() {
fast;
input();
interpretSegs();
sort(lensAll.begin(), lensAll.end(), greater<pair<pll, ll>>());
lensSize = lensAll.size();
int qid = 0, lenid = 0;
ll crid = n;
while (qid < q) {
if (b[qid] < n)printf("%lld\n", a[b[qid++]] + 1);
else {
while (crid <= b[qid] && lenid < lensSize) {
if (crid + lensAll[lenid].ss <= b[qid]) crid += lensAll[lenid++].ss;
else {
pll fs = findLR(-lensAll[lenid].ff.ss);
printf("%lld\n", findIndex(b[qid++] - crid + 1, lensAll[lenid].ff.ff, fs.ff, fs.ss) + 1);
break;
}
}
}
}
return 0;
}
Compilation message
ogledala.cpp: In function 'void input()':
ogledala.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
31 | scanf("%lld %lld %lld", &m, &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ogledala.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
33 | scanf("%lld", &a[i]); a[i]--;
| ~~~~~^~~~~~~~~~~~~~~
ogledala.cpp:36:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
36 | scanf("%lld", &b[i]); b[i]--;
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
78 ms |
5928 KB |
Output is correct |
4 |
Correct |
55 ms |
6048 KB |
Output is correct |
5 |
Correct |
93 ms |
14812 KB |
Output is correct |
6 |
Correct |
83 ms |
15072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
3180 KB |
Output is correct |
2 |
Correct |
49 ms |
3052 KB |
Output is correct |
3 |
Correct |
1293 ms |
290092 KB |
Output is correct |
4 |
Correct |
1295 ms |
287932 KB |
Output is correct |
5 |
Correct |
1415 ms |
279700 KB |
Output is correct |
6 |
Correct |
1438 ms |
277908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2723 ms |
148088 KB |
Output is correct |
2 |
Correct |
2625 ms |
144248 KB |
Output is correct |
3 |
Execution timed out |
4024 ms |
302876 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |