QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121269 | #6509. Not Another Range Query Problem | exxqfu | WA | 4ms | 34072kb | C++14 | 3.7kb | 2023-07-07 20:19:22 | 2023-07-07 20:19:22 |
Judging History
answer
#include <cstdio>
#define rep(i, d, u) for(int i = d; i <= u; ++i)
#define dep(i, u, d) for(int i = u; i >= d; --i)
#define gep(i, a) for(int i = firs[a]; i; i = neig[i])
int ured() {
int re = 0;
char ch;
do {
ch = getchar();
} while('9' < ch || ch < '0');
do {
re = re * 10 + (ch ^ '0');
} while('0' <= (ch = getchar()) && ch <= '9');
return re;
}
void sred(bool *st) {
int re = 0;
char ch;
do {
ch = getchar();
} while(ch != '0' && ch != '1');
do {
st[++re] = ch ^ '0';
} while((ch = getchar()) == '0' || ch == '1');
}
void uwit(int da) {
int ch[21], cn = 0;
do {
ch[++cn] = da - da / 10 * 10;
} while(da /= 10);
do {
putchar('0' ^ ch[cn]);
} while(--cn);
}
int max(int le, int ri) {
return le > ri ? le : ri;
}
const int _maxn = 500011;
int n, q, l[_maxn], r[_maxn], p[_maxn], firs[_maxn], neig[_maxn], rans[_maxn], lans[_maxn], lenr[_maxn], lpos;
int nexa[_maxn], prea[_maxn], nexb[_maxn], fatp[_maxn], fath[_maxn], sizs[_maxn], fats[_maxn << 1], prel[_maxn], regl[_maxn];
bool s[_maxn];
int find(int at) {
if(fath[at] == at) {
return at;
} else {
int re = find(fath[at]);
sizs[at] += sizs[fath[at]];
return fath[at] = re;
}
}
int fids(int at) {
return fats[at] == at ? at : fats[at] = fids(fats[at]);
}
int main() {
lenr[0] = n = ured(), q = ured(), sred(s);
rep(i, 1, q) {
l[i] = ured(), r[i] = ured(), p[i] = ured(), neig[i] = firs[p[i]], firs[p[i]] = i;
}
rep(i, 1, n) {
if(i == n || s[i] ^ s[i + 1]) {
prea[nexa[lpos] = i] = lpos, fatp[i] = lpos + 1, lpos = fath[i] = i, sizs[i] = 0, lpos = fath[i] = i, prel[i] = i;
} else {
nexb[i] = fath[i] = i + 1, sizs[i] = 1;
}
fats[i] = i;
}
nexa[lpos] = 0;
rep(i, 1, q) {
fats[n + 1 + i] = r[i];
}
rep(i, 0, n) {
gep(j, i) {
int po = fids(n + 1 + j);
rans[j] = prel[find(po)] - sizs[po];
}
for(int j = nexa[0]; j; j = nexa[j]) {
regl[j] = prel[j] - prel[prea[j]];
}
for(int j = nexa[0]; j; j = nexa[j]) {
fats[fatp[j]] = prea[j], --regl[j];
if(fatp[j] == j) {
if(nexa[prea[j]] = nexa[j]) {
prea[nexa[j]] = prea[j];
}
} else {
fatp[j] = nexb[fatp[j]];
if(prea[j] && s[j] == s[prea[j]]) {
if(nexa[prea[j]] = nexa[j]) {
prea[nexa[j]] = prea[j];
}
sizs[j] = 1, nexb[j] = fath[j] = fatp[prea[j]], fatp[prea[j]] = fatp[j], regl[prea[j]] += regl[j];
}
}
}
for(int j = nexa[0]; j; j = nexa[j]) {
lenr[i + 1] = prel[j] = prel[prea[j]] + regl[j];
}
}
lpos = n + 1;
dep(i, n, 1) {
if(i == 1 || s[i] ^ s[i - 1]) {
prea[nexa[lpos] = i] = lpos, fatp[i] = lpos - 1, lpos = fath[i] = i, sizs[i] = 0, lpos = fath[i] = i, prel[i] = n - i + 1;
} else {
nexb[i] = fath[i] = i - 1, sizs[i] = 1;
}
fats[i] = i;
}
nexa[lpos] = 0;
rep(i, 1, q) {
fats[n + 1 + i] = l[i];
}
rep(i, 0, n) {
gep(j, i) {
int po = fids(n + 1 + j);
lans[j] = prel[find(po)] - sizs[po];
}
for(int j = nexa[n + 1]; j; j = nexa[j]) {
regl[j] = prel[j] - prel[prea[j]];
}
for(int j = nexa[n + 1]; j; j = nexa[j]) {
fats[fatp[j]] = prea[j], --regl[j];
if(fatp[j] == j) {
if(nexa[prea[j]] = nexa[j]) {
prea[nexa[j]] = prea[j];
}
} else {
fatp[j] = nexb[fatp[j]];
if(prea[j] <= n && s[j] == s[prea[j]]) {
if(nexa[prea[j]] = nexa[j]) {
prea[nexa[j]] = prea[j];
}
sizs[j] = 1, nexb[j] = fath[j] = fatp[prea[j]], fatp[prea[j]] = fatp[j], regl[prea[j]] += regl[j];
}
}
}
for(int j = nexa[n + 1]; j; j = nexa[j]) {
prel[j] = prel[prea[j]] + regl[j];
}
}
rep(i, 1, q) {
uwit(max(lans[i] + rans[i] - lenr[p[i]], 0)), putchar('\n');
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 29920kb
input:
9 7 100110001 2 5 1 3 6 1 4 8 2 2 7 1 1 9 1 1 9 0 1 9 8
output:
2 1 1 3 4 9 0
result:
ok 7 numbers
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 34072kb
input:
100 100000 0000011010101000111011110110000111110101101010111111101011011010111001111010111000001000011000001010 76 99 3 25 84 7 45 83 11 10 12 10 69 86 4 27 28 1 22 42 42 4 86 25 26 91 22 20 81 17 50 78 0 77 93 50 31 50 34 7 46 13 78 89 0 79 98 0 2 84 33 58 93 11 56 75 2 55 77 68 7 9 41 44 46 11 47 ...
output:
19 4 8 0 26 0 0 0 0 0 29 0 0 7 12 20 0 8 0 0 0 5 0 2 0 0 0 20 18 1 0 57 0 0 11 0 24 0 0 6 0 0 0 0 0 14 0 0 0 0 7 0 6 0 21 0 0 0 0 8 0 0 3 0 0 0 0 25 0 0 0 0 11 0 24 0 12 13 0 21 29 36 20 24 26 0 21 22 7 18 25 0 0 0 12 0 0 0 6 0 0 11 51 0 0 4 17 14 0 18 16 9 12 8 22 22 0 2 0 4 0 0 0 0 0 0 0 46 59 0 0...
result:
wrong answer 1st numbers differ - expected: '8', found: '19'