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 ≤ nr - l + 1 is divisible byk), 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 nk and w (1 ≤ k ≤ min(n, 10), 1 ≤ n, w ≤ 105). The second line contains ncharacters. 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 thatri - 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.

 分块的思想。
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <set>
using namespace std;
typedef long long LL ; int dp[][] ;
int main(){
int i , j , x ,n , k , w , L ,R ,sum;
string s ;
for(i = ; i < ; i++)
dp[][i] = ;
cin>>n>>k>>w ;
cin>>s ;
for(i = ; i <= n ; i++){
for(j = ; j < k ; j++)
dp[i][j] = dp[i-][j] ;
dp[i][i%k] += s[i-] == '' ;
}
while(w--){
cin>>L>>R ;
sum = ;
x = R % k ;
for(i = ; i < k ; i++){
if(i == x)
sum += (R-L+)/k - (dp[R][i] - dp[L-][i]) ;
else
sum += dp[R][i] - dp[L-][i] ;
}
cout<<sum<<endl ;
}
return ;
}

Codeforces Round #229 (Div. 2) C的更多相关文章

  1. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  2. Codeforces Round #229 (Div. 2) D

    D. Inna and Sweet Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  9. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

随机推荐

  1. blocked file type by sharepoint 分类: Sharepoint 2015-07-05 07:45 6人阅读 评论(0) 收藏

    o add or remove blocked file types by using Central Administration Verify that you have the followin ...

  2. hdu 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.) 分类: hdoj 2015-06-19 11:58 25人阅读 评论(0) 收藏

    use sentinel to avoid boudary testing, use swap trick to avoid extra copy. original version #include ...

  3. 关于GSM基站定位

    一、基站定位两个参数 1、什么是LAC:Location Area Code(LAC)地区区域码,用来划分区域 2、什么是CellID:Cell Tower ID(Cid)CellID代表一个移动基站 ...

  4. svg DOM的一些js操作

    这是第一个实例,其中讲了如何新建svg,添加元素,保存svg document,查看svg. 下面将附上常用一些元素的添加方法:(为js的,但基本上跟java中操作一样,就是类名有点细微差别) Cir ...

  5. 实验三 java敏捷开发与XP

    一.实验内容 (一)敏捷开发与XP 软件开发流程的目的是为了提高软件开发.运营.维护的效率,并提高软件的质量.用户满意度.可靠性和软件的可维护性. 光有各种流程的思想是不够的,我们还要有一系列的工具来 ...

  6. HDU 2254

    http://acm.hdu.edu.cn/showproblem.php?pid=2254 矩阵乘法两个经典问题的综合题,还要离散化和处理边界,好题啊好题 题意容易理解错,每一天是独立的,所以根据加 ...

  7. LintCode Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. [debian]SublimeText>PrettyCode無效

    怣 apt-get install node http://nodejs.org/#download.

  9. codeForce-589D Boulevard(判断线段是否相交)

    题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个 ...

  10. 类似于C# using() java 语法

    From :   https://www.infoq.com/news/2010/08/arm-blocks try(Jedis jedis = jedisPool.getResource()){ S ...