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. C语言基础--数组及相关

    概念: 一堆相同类型的数据的有序集合 格式: 元素类型  数组名称[ 元素个数 ] 定义数组: // 定义了一个名称叫做scores的数组, 数组中可以存放3个int类型的数据 ]; // 只要定义一 ...

  2. 对编写html代码的几点儿小建议

    1.DOCTYPE说明:告诉浏览器要使用哪种规范来解释该文档内容: <!DOCTYPE html PUBLIC "-W3//DTD//XHTML 1.0  Transitional// ...

  3. 三部曲一(数据结构)-1011-Sorting It All Out

    每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为0的点,如果出现了就说明不能确定大小关系.不论出不出现多个入度为0的点拓扑排序都要进行到最后来判断是否出现环,因为一旦出现环 ...

  4. linux命令:find

    1.命令介绍: find用来在整个系统指定的路径下搜索文件,功能强大,但是遍历整个系统时很耗时间. 2.命令格式: find 路径 [选项] [print -exec -ok...] 3.命令参数: ...

  5. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

  6. Grand Central Dispatch(GCD)

     GCD      GCD是异步执行任务的技术之一. GCD使用很简洁的记述方法,实现了极为复杂繁琐的多线程编程. dispatch_async(queue, ^{      //长时间处理     ...

  7. php-访问数据库

    建一个连接,造一个连接对象 $db = new MySQLi("host","username","passwd","databa ...

  8. HBase 实战(1)--HBase的数据导入方式

    前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇博文重点讲解HBase的 ...

  9. Android studio 常用插件

    常用插件 GsonFormat 该插件的功能是根据JSON数据生成相应的实体类,插件装好后按Alt+S快捷键呼不出GsonFormat的界面,如果你也遇到类似的问题,就去改一下快捷键吧!我将快捷键改成 ...

  10. VB6的函数指针传递

    Option Explicit Private Declare Function CallWindowProc Lib "user32" Alias "CallWindo ...