• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #228 Div2 C

 

Sponsored by
In English По-русски
HaibaraAi | Logout
 
 
 
 
 
  • Home
  • Contests
  • Gym
  • Problemset
  • Groups
  • Rating
  • Help
  • Rockethon

 
 
Codeforces Round #229 (Div. 2)
Finished
Practice
Add to favourites
 
 
→ 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.
 
 
→ Submit?
 
Language:
Choose file:
Be careful: there is 50 points penalty for submission which fails the pretests or resubmission (except failure on the first test, denial of judgement or similar verdicts). "Passed pretests" submission verdict doesn't guarantee that the solution is absolutely correct and it will pass system tests.
 
 
→ Last submissions
 
SubmissionTimeVerdict
5967057 02/13/2014 12:39AM Accepted
5967053 02/13/2014 12:38AM Wrong answer on test 11
5966182 02/12/2014 09:37PM Wrong answer on test 11
5956913 02/11/2014 09:39PM Wrong answer on pretest 8
5956184 02/11/2014 09:29PM Wrong answer on pretest 8
5955120 02/11/2014 09:15PM Wrong answer on pretest 2
 
 
→ Problem tags
 
 
 
 
 
dp
 
 
 
 
implementation
No tag edit access
  •  
     
     
  • Problems
  • Submit
  • My submissions
  • Status
  • Hacks
  • Room
  • Standings
  • Custom test
C. Inna and Candy Boxes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Inna loves sweets very much. She has n closed present boxes lines up in a row in front of her. Each of these boxes contains either a candy (Dima's work) or nothing (Sereja's work). Let's assume that the boxes are numbered from 1 to n, from left to right.

As the boxes are closed, Inna doesn't know which boxes contain candies and which boxes contain nothing. Inna chose number k and asked w questions to Dima to find that out. Each question is characterised by two integers li, ri (1 ≤ li ≤ ri ≤ n; r - l + 1 is divisible by k), the i-th question is: "Dima, is that true that among the boxes with numbers from li to ri, inclusive, the candies lie only in boxes with numbers li + k - 1, li + 2k - 1, li + 3k - 1, ..., ri?"

Dima hates to say "no" to Inna. That's why he wonders, what number of actions he will have to make for each question to make the answer to the question positive. In one action, Dima can either secretly take the candy from any box or put a candy to any box (Dima has infinitely many candies). Help Dima count the number of actions for each Inna's question.

Please note that Dima doesn't change the array during Inna's questions. That's why when you calculate the number of operations for the current question, please assume that the sequence of boxes didn't change.

Input

The first line of the input contains three integers n, k and w (1 ≤ k ≤ min(n, 10), 1 ≤ n, w ≤ 105). The second line contains n characters. If the i-th box contains a candy, the i-th character of the line equals 1, otherwise it equals 0.

Each of the following w lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the description of the i-th question. It is guaranteed that ri - li + 1 is divisible by k.

Output

For each question, print a single number on a single line — the minimum number of operations Dima needs to make the answer to the question positive.

Sample test(s)
Input
10 3 3 1010100011 1 3 1 6 4 9
Output
1 3 2
Note

For the first question, you need to take a candy from the first box to make the answer positive. So the answer is 1.

For the second question, you need to take a candy from the first box, take a candy from the fifth box and put a candy to the sixth box. The answer is 3.

For the third question, you need to take a candy from the fifth box and put it to the sixth box. The answer is 2.


Codeforces (c) Copyright 2010-2014 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: 02/13/2014 12:40AM (p1).

 

 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, y, mi, k ,flag, p, ans;
14 int a[maxn], b[maxn];
15 char s[maxn];
16 int main(){
17     cin >> n >> k >> m >> s;
18     for (int i = 0; i <= n-k; i++){
19         for (int j = 0; j < k-1; j++)
20         if (s[i+j]=='1')a[i]++;
21         if (s[i+k-1]=='0')a[i]++;
22     }
23     for (int i = n-k; i >= 0; i--)b[i]=b[i+k]+a[i];
24     while (m--){
25         int l, r;
26         cin >> l >> r;
27         cout << b[l-1] - b[r] << endl;
28     }
29     return 0;
30 }
View Code
 
 
 
 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 }
View Code
posted @ 2014-02-13 04:41  HaibaraAi  阅读(123)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3