Codeforce Round #228 Div2 C
→ Practice
You are registered for practice. You can solve problems unofficially. Results can be found in the contest status and in the bottom of standings.
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <stack> 6 #include <queue> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define maxn 200005 13 int n, m ,t ,b, y, mi, k ,flag, p, ans; 14 int a[maxn], c[maxn], d[11][maxn]; 15 char s[maxn]; 16 void add1(int x){ 17 while (x <= n){ 18 c[x]++; 19 x += x&-x; 20 } 21 } 22 int sum1(int x){ 23 int res = 0; 24 while (x>0){ 25 res += c[x]; 26 x -= x&-x; 27 } 28 return res; 29 } 30 void add2(int id,int x){ 31 while (x <= n){ 32 d[id][x]++; 33 x += x&-x; 34 } 35 } 36 int sum2(int id,int x){ 37 int res = 0; 38 while (x>0){ 39 res += d[id][x]; 40 x -= x&-x; 41 } 42 return res; 43 } 44 int main(){ 45 //char ss[] = "0000000000001000000100010010001100100111000001010100000100001"; 46 //cout << strlen(ss); 47 cin >> n >> k >> m; 48 scanf("%s", s); 49 for (int i = 1; i <= n; i++){ 50 if (s[i-1] == '1'){ 51 add1(i); 52 } 53 } 54 for (int i = 0; i < k; i++){ 55 for (int j = i; i+j <= n+k; j+=k){ 56 if (s[j-1] == '1'){ 57 add2(i, j); 58 } 59 } 60 } 61 while (m--){ 62 int l, r; 63 cin >> l >> r; 64 int q = (l - 1) % k; 65 int s1 = sum2(q, r) - sum2(q, l - 1); 66 int s2 = sum1(r) - sum1(l - 1); 67 int s3 = s2 - s1 + (r-l+1)/k - s1; 68 printf("%d\n", s3); 69 } 70 return 0; 71 }




浙公网安备 33010602011771号