QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#659784 | #5516. Modern Machine | liuziao | 15 | 533ms | 802764kb | C++23 | 8.7kb | 2024-10-19 21:59:49 | 2024-10-19 21:59:51 |
Judging History
answer
#include <bits/stdc++.h>
// #define int int64_t
const int kMaxN = 1.2e5 + 5, kLog = 18;
int n, m, q, cr, cb;
int a[kMaxN], nxt[kLog][kLog][kMaxN], rx[kMaxN], bx[kMaxN], lg[kMaxN], prer[kMaxN];
int64_t sumr[kLog][kLog][kMaxN], sumb[kLog][kLog][kMaxN];
std::string s;
struct SGT {
std::vector<std::pair<int, int>> v[kMaxN * 4];
int func(int x, int val) {
auto it = std::upper_bound(v[x].begin(), v[x].end(), std::pair<int, int>(val, 1e9)) - 1;
return val + it->second;
}
void pushup(int x) {
int ls = (x << 1), rs = (x << 1 | 1);
for (int i = 0; i < (int)v[ls].size(); ++i) {
auto [L, val] = v[ls][i];
int R = (i + 1 == (int)v[ls].size() ? n + 1 : v[ls][i + 1].first);
auto itl = std::lower_bound(v[rs].begin(), v[rs].end(), std::pair<int, int>(L + val, -1e9));
auto itr = std::lower_bound(v[rs].begin(), v[rs].end(), std::pair<int, int>(R + val, -1e9));
v[x].emplace_back(L, 0);
for (auto it = itl; it != itr; ++it) {
v[x].emplace_back(it->first - val, 0);
}
}
std::sort(v[x].begin(), v[x].end());
v[x].erase(std::unique(v[x].begin(), v[x].end()), v[x].end());
for (auto &[xx, val] : v[x]) {
val = func(rs, func(ls, xx)) - xx;
}
}
void build(int x, int l, int r) {
if (l == r) {
if (n - a[l] > 0) {
v[x].emplace_back(0, a[l] + 1);
if (n - a[l] <= a[l] - 1) v[x].emplace_back(n - a[l], a[l] - n);
} else {
v[x].emplace_back(0, a[l] - n);
}
if (a[l] < n + 1 - a[l]) {
v[x].emplace_back(a[l], a[l]);
v[x].emplace_back(n - a[l] + 1, a[l] - (n + 1));
} else {
v[x].emplace_back(a[l], a[l] - (n + 1));
}
std::sort(v[x].begin(), v[x].end());
return;
}
int mid = (l + r) >> 1;
build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
pushup(x);
}
int query(int x, int l, int r, int ql, int qr, int val) {
if (l > qr || r < ql) {
return val;
} else if (l >= ql && r <= qr) {
return func(x, val);
}
int mid = (l + r) >> 1;
return query(x << 1 | 1, mid + 1, r, ql, qr, query(x << 1, l, mid, ql, qr, val));
}
} sgt;
void prework() {
sgt.build(1, 1, m);
lg[0] = -1;
for (int i = 1; i <= 1.2e5; ++i) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; ++i) prer[i] = prer[i - 1] + (s[i] == 'R');
for (int i = 1; i <= n + 1; ++i) {
if (i == n + 1 || s[i] == 'B') {
rx[++cr] = i - 1;
}
}
for (int i = n; ~i; --i) {
if (!i || s[i] == 'R') {
bx[++cb] = n - i;
}
}
for (int x = 0; x <= lg[n] + 1; ++x) {
for (int y = 0; y <= lg[n] + 1; ++y) {
int lenr, lenb;
if (!x) lenr = 0;
else lenr = (1 << (x - 1));
if (!y) lenb = 0;
else lenb = (1 << (y - 1));
if (lenr + lenb > n) continue;
for (int i = 1; i <= m; ++i) {
sumr[x][y][i] = sumr[x][y][i - 1];
sumb[x][y][i] = sumb[x][y][i - 1];
if (a[i] <= lenr) sumr[x][y][i] += a[i];
else if (n - a[i] + 1 <= lenb) sumb[x][y][i] += n - a[i];
}
nxt[x][y][m + 1] = m + 1;
for (int i = m; i; --i) {
if (a[i] > lenr && a[i] <= n - lenb) nxt[x][y][i] = i;
else nxt[x][y][i] = nxt[x][y][i + 1];
}
}
}
}
int getcntr(int l, int r) {
if (l > r) return 0;
return prer[r] - prer[l - 1];
}
int getcntb(int l, int r) {
if (l > r) return 0;
return (r - l + 1) - (prer[r] - prer[l - 1]);
}
char getch(int x, int nowr, int nowb) {
assert(x >= 1 && x <= n);
if (x <= rx[nowr]) return 'R';
else if (x > n - bx[nowb]) return 'B';
else return s[x];
}
int solve(int l, int r) {
int pos = l - 1, nowr = 1, nowb = 1, val = -1;
for (;;) {
assert(rx[nowr] + bx[nowb] <= n);
if (rx[nowr] + bx[nowb] >= n) {
val = rx[nowr];
break;
}
int x = lg[rx[nowr]] + 1, y = lg[bx[nowb]] + 1;
int nxtp = std::min(nxt[x][y][pos + 1], r + 1);
int detr = std::min<int>(sumr[x][y][nxtp - 1] - sumr[x][y][pos], n);
int detb = std::min<int>(sumb[x][y][nxtp - 1] - sumb[x][y][pos], n);
// std::cerr << pos << ' ' << nxtp << ' ' << rx[nowr] << ' ' << bx[nowb] << ' ' << detr << ' ' << detb << '\n';
if (rx[std::min(nowr + detr, cr)] + bx[std::min(nowb + detb, cb)] < n) {
// std::cerr << pos << ' ' << nxtp << ' ' << detr << ' ' << detb << ' ' << rx[nowr] << ' ' << bx[nowb] << ' ' << rx[std::min(nowr + detr, cr)] + bx[std::min(nowb + detb, cb)] << '\n';
nowr += detr, nowb += detb, pos = nxtp;
if (pos == r + 1) break;
int cntr = rx[nowr] + getcntr(rx[nowr] + 1, n - bx[nowb]);
if (getch(a[pos], nowr, nowb) == 'B') ++cntr;
// std::cerr << "!!! " << pos << ' ' << getch(a[pos], nowr, nowb) << ' ' << nxtp << ' ' << rx[nowr] << ' ' << bx[nowb] << ' ' << cntr << '\n';
if (n - cntr >= a[pos]) { // 把前 a[pos] 个 B 变成 R
nowr = std::min(nowr + a[pos], cr);
if (s[a[pos]] == 'B') ++nowr;
nowr = std::min(nowr, cr);
// std::cerr << "??? " << pos << ' ' << nxtp << ' ' << rx[nowr] << ' ' << bx[nowb] << ' ' << cntr << '\n';
if (rx[nowr] + bx[nowb] >= n) {
val = cntr + a[pos];
break;
}
} else { // 把后 n - a[pos] + 1 个 R 变成 B
assert(cntr >= n - a[pos] + 1);
nowb = std::min(nowb + n - a[pos] + 1, cb + 1);
if (s[a[pos]] == 'B') --nowb;
// std::cerr << nowb << ' ' << rx[nowr] << ' ' << bx[nowb] << '\n';
nowb = std::min(nowb, cb);
if (rx[nowr] + bx[nowb] >= n) {
val = cntr - (n - a[pos] + 1);
break;
}
}
} else {
// assert(pos < nxtp);
// std::cerr << pos << ' ' << nxtp << '\n';
// for (int i = pos + 1; i < nxtp; ++i) {
// int cntr = rx[nowr] + getcntr(rx[nowr] + 1, n - bx[nowb]);
// if (getch(a[pos], nowr, nowb) == 'B') ++cntr;
// int detr = 0, detb = 0;
// if (a[i] <= n - cntr) detr = a[i];
// else detb = n - a[i];
// if (rx[std::min(nowr + detr, cr)] + bx[std::min(nowb + detb, cb)] >= n) {
// pos = i;
// if (n - cntr >= a[i]) {
// val = cntr + a[i];
// break;
// } else {
// val = cntr - (n - a[i]);
// break;
// }
// } else {
// nowr += detr, nowb += detb;
// }
// }
// assert(val != -1);
// break;
int L = pos, R = nxtp, res = pos;
while (L + 1 < R) {
int mid = (L + R) >> 1;
int detr = std::min<int>(sumr[x][y][mid] - sumr[x][y][pos], n);
int detb = std::min<int>(sumb[x][y][mid] - sumb[x][y][pos], n);
if (rx[std::min(nowr + detr, cr)] + bx[std::min(nowb + detb, cb)] >= n) R = res = mid;
else L = mid;
}
assert(res > pos);
int detr = std::min<int>(sumr[x][y][res - 1] - sumr[x][y][pos], n);
int detb = std::min<int>(sumb[x][y][res - 1] - sumb[x][y][pos], n);
nowr += detr, nowb += detb;
assert(nowr <= cr && nowb <= cb && rx[nowr] + bx[nowb] < n);
int cntr = rx[nowr] + getcntr(rx[nowr] + 1, n - bx[nowb]);
if (getch(a[res], nowr, nowb) == 'B') ++cntr;
// if (a[res] > rx[nowr] && a[res] <= n - bx[nowb] && getch(a[res], nowr, nowb) == 'B') ++cntr;
// std::cerr << "heige " << res << ' ' << cntr << ' ' << rx[nowr] << ' ' << bx[nowb] << '\n';
pos = res;
if (a[res] <= n - cntr) {
val = cntr + a[res];
// val = rx[nowr + a[res]];
break;
} else {
// val = n - bx[nowb + n - a[res]];
val = cntr - (n - a[res] + 1);
break;
}
}
}
// std::cerr << "fuckkkk " << val << ' ' << pos << ' ' << r << '\n';
if (val == -1) {
return rx[nowr] + getcntr(rx[nowr] + 1, n - bx[nowb]);
} else {
for (int i = pos + 1; i <= r; ++i) {
if (a[i] <= val) {
val = (val + a[i]) % (n + 1);
} else {
val = (val + a[i] + 1) % (n + 1);
}
}
return val;
return sgt.query(1, 1, m, pos + 1, r, val);
}
}
void dickdreamer() {
std::cin >> n >> m >> s;
s = " " + s;
for (int i = 1; i <= m; ++i) std::cin >> a[i];
prework();
std::cin >> q;
for (int i = 1; i <= q; ++i) {
int l, r;
std::cin >> l >> r;
std::cout << solve(l, r) << '\n';
}
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
详细
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 0ms
memory: 13816kb
input:
3 1 BBR 3 1 1 1
output:
0
result:
ok single line: '0'
Test #2:
score: 3
Accepted
time: 0ms
memory: 12068kb
input:
3 10 BBB 3 3 3 1 3 3 3 3 3 1 1 2 5
output:
2
result:
ok single line: '2'
Test #3:
score: 3
Accepted
time: 0ms
memory: 22244kb
input:
10 1 RBBBBRRBBR 9 1 1 1
output:
3
result:
ok single line: '3'
Test #4:
score: 3
Accepted
time: 0ms
memory: 22252kb
input:
10 10 RRRBBRRBRB 5 9 4 6 7 7 4 3 8 2 1 1 10
output:
1
result:
ok single line: '1'
Test #5:
score: 3
Accepted
time: 0ms
memory: 62276kb
input:
300 300 BRRRRBBRBRBRBBBRBRRRBRBBRBBRRBBRBBRBRBRBRBBRRBBRBBRBBBBRRRRBRBRBBBBRBBBRBBRBBRBRBRRBRBBRRRRBRBBRRRBRRBRBBBRRRRBRRRBBBBRRBBRBBBRRRBRBBRBRBBRRBRBRRRBRBRBBBBRRBRBRRRRRBRRRRRBRRRBRBRRRRRBBBRRBBRBBRRRRBBBBRBRBBBRRRRBBBBBBBBBBRBRRBRBBRRRRBRBBRRRBBRBRRRBBRBRRBBRBBBBRBRBRBBBRBBRBRBBRBBRBRBRBRRRBBBBR...
output:
29
result:
ok single line: '29'
Test #6:
score: 3
Accepted
time: 0ms
memory: 68636kb
input:
300 300 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
149
result:
ok single line: '149'
Test #7:
score: 3
Accepted
time: 0ms
memory: 15960kb
input:
5 2 RBRBB 2 5 1 1 2
output:
4
result:
ok single line: '4'
Subtask #2:
score: 12
Accepted
Dependency #1:
100%
Accepted
Test #8:
score: 12
Accepted
time: 15ms
memory: 160164kb
input:
7000 7000 RRBRRRRRRBBRBRBRRRBRRRBRRRRBRBRBRBRBBBRBRRRBBBRBBBBBBRBBBRRBRBRBBRRRBBBBRRBBRBBRBBRRRBBRRBRBRRRRRBBRRBRBBBRBBRRRRBRBBBBRBBBBBBRRRBRBRBBRBRRBBBBBRRBBBBBBRBBBRRBBRRBBBBBBRBRBRBBRBBRBBBRRBRBBRBBBBBRBRRBRRBRRRBRBBBBBRRBRBRRBRRRRBBBBRRBBRRRRRBBBBBRRBBBBBBRRBBRBRRRRRRRBRRBRRBBBBBBBBRRRRBBRRRRRRR...
output:
5505
result:
ok single line: '5505'
Test #9:
score: 12
Accepted
time: 15ms
memory: 163072kb
input:
7000 7000 RRBRRRBBRRBBRBBBBRBRBRRBBBBBBBBBBRRRBRRBBRRRRBBRRBRRRRRBBRBBBBBRRBBBBRBBBBRBRBBBBBRBBRRRBBBBRRBRRBRRBRBBBRRRRBBRRBRRBRBBRBBBRBBBRRBRBBBRRBBBBBRBBRBRBRBBBBRRBBBRRRRBRRBBRBRBBBBBBRRRRRBBRRBBRRBBBRBBBRRRRRBRBBRBRBBRRBBRBRRRBBRRBBBRRBRRRRRRRBBRBRRBRBRBRBRBRBBRBRRBRBBBBBBBRRBRRRBBBBRRBBRRRBBRBR...
output:
5936
result:
ok single line: '5936'
Test #10:
score: 12
Accepted
time: 16ms
memory: 167028kb
input:
7000 7000 RBBRBRBRRRBBRBRRBRBBRBRRRBRBBBRBBRBRBRBRRBBBBRBRRRBBRRRRBBRRRRBBRBRBRRRRBBRRBBRBRBBBRBBBBBRBBRBBRRBBRRRRRBBRBRBRRBBRRRBRBBBRBRRBRRRBRRRBRBBBBRBBBBBBRBRRRBRRRBRBBBRBRRBBRRBRRBBBBBRBBRRRRBBRRBBBBRRBRBRRRRBBRRRRRBRBRRBBRRRRRBRRBRBBBBRRBBBBRBRRRBBRBRRRRRBRRBRRRBRRBRRRRRBBBRBBBRBRRRBRBBBBRRRRRR...
output:
6260
result:
ok single line: '6260'
Test #11:
score: 12
Accepted
time: 7ms
memory: 165600kb
input:
7000 7000 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
output:
3499
result:
ok single line: '3499'
Test #12:
score: 12
Accepted
time: 8ms
memory: 168608kb
input:
7000 7000 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
3906
result:
ok single line: '3906'
Subtask #3:
score: 0
Wrong Answer
Dependency #2:
100%
Accepted
Test #13:
score: 10
Accepted
time: 459ms
memory: 793980kb
input:
120000 120000 BBBBRBBRBRRBRRRBBBRRRRRBRRRRRRRRRRRBBBBRBRBBBBBBRBRBBBRBBRBRRBBRBRRRBRBBBBBRBRRBBRRRRRBBRBBBBRBBRRBRRRRBRBBBRRRBBBRRBRRBBBRRRBRRRBBRBBRRRBBBRBBRBBBBBRBRRRRBBRBRBRBBRRRRRRRBBRRBBBBRRRBBRRRRRBBBBRBBBBRBRRBBBRBRBBBRBRBRRBBRBRBBBRRBBRBBBBRBBRRBRBRBRBRRBBRBBBBBBBRRRRBRBRBRBRRRRBRRBBBRBRRBRB...
output:
110620 106126 106019 110676 111965
result:
ok 5 lines
Test #14:
score: 0
Wrong Answer
time: 533ms
memory: 802764kb
input:
120000 120000 BRRBBBRRRRRRRBRRRRRRRBBRBBRRBBRRRRRBRBRRBRRRRRBRRBBBBRRBRRRBBBBRRBRBRBBBRBBRRBRRRBRRBRBBBBRRRBRBRBRBBBBRBRRRBRBBRBBBRRBBBRBBBBRRBBRRBBRBRRRBBBRBBBRRRRBBBBBRRRRBBBBBBBRRRRRRRBBBRRRBBRRRRBBBRBBBRRBBBRRRRRRRRRRRRRBRRBRRBRBRRBBBBBBRRRRBRRBRBBBBRRBRRRRRBRRBRRRBBRRRBBRRRRRBRBRRRRBBBBBBBRBRRR...
output:
96347 101002 105432 99656 104213
result:
wrong answer 1st lines differ - expected: '96348', found: '96347'
Subtask #4:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
input:
10 120000 RRRRRRRRRR 9 1 9 6 1 9 6 10 3 2 6 4 2 5 7 10 8 9 2 6 7 10 2 9 7 5 9 9 2 7 4 1 8 3 3 6 9 4 7 9 6 8 8 2 5 6 8 3 9 2 5 2 5 9 8 4 8 3 2 2 5 1 9 1 7 9 9 4 9 5 2 10 7 4 3 8 1 10 7 6 6 2 3 8 7 8 5 9 2 2 1 10 2 2 5 4 2 5 9 1 8 4 5 1 2 8 3 10 10 6 7 2 1 5 10 1 9 10 1 7 3 10 8 10 6 8 3 1 10 9 10 5 5...
output:
10 6 1 8 1 9 8 1 3 0 3 4 4 2 2 2 10 8 10 7 0 7 0 1 10 3 10 7 3 2 10 8 10 0 0 10 1 2 5 4 0 7 1 0 6 7 6 8 3 2 4 7 0 6 7 5 2 8 4 8 1 4 4 10 3 7 7 7 3 1 3 10 3 4 0 2 1 3 9 8 4 6 10 9 3 7 9 2 5 7 2 7 2 1 0 6 0 6 3 6 2 8 1 10 9 4 5 2 5 10 9 4 1 8 5 3 6 3 8 9 10 6 4 10 7 2 8 7 9 7 7 0 9 5 5 6 3 7 4 7 9 6 7...
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #26:
score: 0
Time Limit Exceeded
input:
120000 120000 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
output:
23441 85217 55120 85435 90828 27927 63967 41321 97520 63588 53976 9028 23460 86914 85688 112911 68792 102907 14973 38475 71931 17922 62077 53185 57121 43774 12583 21332 82917 28259 92209 28023 51606 65188 44768 34904 96969 18999 3653 3865 42795 86270 55638 108702 7871 68493 109086 40077 60406 38299 ...
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
input:
120000 120000 RRBBRBRBBBRBRRBRBRRRBRBRRBRRRBBRBBBBBBRBBRBRBRBRRBRBRBRRRBRBBBBRRBBRRBBRRRBRRBRBBRRRBBBRRBBBBRBBBRBBBRBBBRBBRBBBBRBBBBRRBRRRRRRRBRRRBRBBBRBBBBRBBRBRBRRBBRRBRBBBBRBBBRBBRRBBBBRBBBBRBRRBRBBRBBBBRRBBBBBBRBBRBBBBBBRBBRRRRRBBBRBBBRBRRBBRBRBRBBRRBRRBBBRBRBRRRBBRRBBRRRRBRRBBRRBBBBBBBRBBBRBBBR...
output:
83101 101089 86861 90592 87368 108738 85550 95262 105111 86861 84761 89448 86987 84642 85927 98400 101647 115069 86358 88161 88124 102000 90017 82325 88897 98142 108041 110441 85050 97578 100054 87655 103638 97493 109084 94342 87147 88779 96474 87274 96348 81879 109022 85971 93668 106944 103118 8279...
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%