#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
static void run_with_stack_size(void (*func)(void), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack + stsize - 16;
send = (char *)((uintptr_t)send / 16 * 16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r"(send));
func();
asm volatile("mov (%0), %%rsp\n" : : "r"(send));
free(stack);
}
int n, m, q;
char c[MAXN];
int a[MAXN];
int r[MAXN], b[MAXN];
int f(int prefix, int pivot) {
int exc = 2 * prefix + 1;
int actual = prefix + pivot;
if(exc <= actual) actual++;
return actual % (n + 1);
}
void solve(int tc) {
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> c[i];
for(int i=1; i<=m; i++) cin >> a[i];
int st[17][m+1][n+1];
for(int i=0; i<=n; i++) {
for(int j=1; j<=m; j++) {
st[0][j][i] = f(i, a[j]);
}
}
for(int i=1; i<17; i++) {
for(int j=1; j+(1<<i)-1<=m; j++) {
for(int k=0; k<=n; k++) {
int bru = st[i-1][j][k];
int now = j + (1<<(i-1));
st[i][j][k] = st[i-1][now][bru];
}
}
}
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
int bru = n;
for(int i=16; i>=0; i--) {
if(l + (1 << i) - 1 <= r) {
bru = st[i][l][bru];
l += (1 << i);
}
}
cout << bru << "\n";
}
}
void uwu() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
int32_t main() {
#ifdef ONLINE_JUDGE
uwu();
#endif
#ifndef ONLINE_JUDGE
run_with_stack_size(uwu, 1024 * 1024 * 1024); // run with a 1 GiB stack
#endif
}
/*
g++ D.cpp -std=c++17 -O2 -o D
./D < input.txt
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
191 ms |
178180 KB |
Output is correct |
3 |
Correct |
211 ms |
178256 KB |
Output is correct |
4 |
Correct |
182 ms |
178256 KB |
Output is correct |
5 |
Correct |
88 ms |
178332 KB |
Output is correct |
6 |
Correct |
87 ms |
178472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
191 ms |
178180 KB |
Output is correct |
3 |
Correct |
211 ms |
178256 KB |
Output is correct |
4 |
Correct |
182 ms |
178256 KB |
Output is correct |
5 |
Correct |
88 ms |
178332 KB |
Output is correct |
6 |
Correct |
87 ms |
178472 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
191 ms |
178180 KB |
Output is correct |
3 |
Correct |
211 ms |
178256 KB |
Output is correct |
4 |
Correct |
182 ms |
178256 KB |
Output is correct |
5 |
Correct |
88 ms |
178332 KB |
Output is correct |
6 |
Correct |
87 ms |
178472 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |