QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#666513 | #6509. Not Another Range Query Problem | HJY2022 | WA | 39ms | 29460kb | C++14 | 1.5kb | 2024-10-22 18:52:02 | 2024-10-22 18:52:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MX = 5e5 + 7;
char str[MX];
int s[2][MX];
int P[MX],tot = 0;
int L[MX],R[MX],id[MX],ans[MX];
vector<int > querys[MX];
int lowbit(int x){return x & -x;}
void modify(int t,int x,int v){
for(int i = x;i <= tot;i += lowbit(x))s[t][i] += v;
}
int ask(int t,int x){
int ret = 0;
for(int i = x;i >= 1;i -= lowbit(i))ret += s[t][i];
return ret;
}
int query(int t,int l,int r){return ask(t,r) - ask(t,l - 1);}
int main(){
int N,q;
cin >> N >> q;
cin >> str + 1;
P[1] = 1;tot = 1;
for(int i = 2;i <= N;i++)if(str[i] != str[i - 1])P[++tot] = i;
P[tot + 1] = N + 1;
for(int i = 1;i <= tot;i++)modify(0,i,P[i + 1] - P[i]);
for(int i = 1;i <= tot;i++)modify(1,i,1);
for(int i = 1;i <= tot;i++)id[i] = i;
sort(id + 1,id + 1 + tot,[&](int x,int y){return P[x + 1] - P[x] < P[y + 1] - P[y];});
for(int i = 1;i <= q;i++){
int k;cin >> L[i] >> R[i] >> k;
querys[k].push_back(i);
}
int cur = 1;
for(int i = 0;i <= N;i++){
while(cur <= tot && P[id[cur] + 1] - P[id[cur]] <= i){modify(0,id[cur],P[id[cur]] - P[id[cur] + 1]);modify(1,id[cur],-1);cur++;}
for(auto it : querys[i]){
int ll = upper_bound(P + 1,P + 1 + tot,L[it]) - P - 1,rr = upper_bound(P + 1,P + 1 + tot,R[it]) - P - 1;
if(ll == rr)ans[it] = max(0,R[i] - L[i] + 1 - i);
else{
ans[it] = query(0,ll + 1,rr - 1) - i * query(1,ll + 1,rr - 1);
ans[it] += max(0,P[ll + 1] - L[it] - i);
ans[it] += max(0,R[it] - P[rr] + 1 - i);
}
}
}
for(int i = 1;i <= q;i++)cout << ans[i] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 29460kb
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: 39ms
memory: 29448kb
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:
12 0 0 0 4 0 0 0 0 0 51 0 0 0 23 -29 0 0 11 0 0 0 0 0 0 0 0 6 8 10 0 57 0 0 -28 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 20 0 0 0 2 0 0 0 0 0 0 0 0 31 10 0 0 0 0 0 10 1 0 41 0 32 72 94 46 23 63 0 52 12 2 11 6 0 0 1 0 0 0 0 3 0 18 0 122 0 0 58 22 15 0 18 17 17 7 0 33 54 2 8 0 13 0 0 0 0 0 0 0 95 138 0 0...
result:
wrong answer 1st numbers differ - expected: '8', found: '12'