Codeforces Round #229 (Div. 2) C
1 second
256 megabytes
standard input
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 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.
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 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.
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.
10 3 3
1010100011
1 3
1 6
4 9
1
3
2
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
随机推荐
- stanford Protege 4.3 ERROR: Bundle org.protege.common 解决方法
我的java版本是jdk1.8.0_45,安装了protege后打开总显示: ERROR: Bundle org.protege.common [1] Error starting file:/hom ...
- Objective-C学习笔记-第二天(1)
Objective-C中,调用方法采用的是一种消息传递机制. 参考文章:http://blog.csdn.net/xingyevc/article/details/39397873 如果向某个对象传递 ...
- jq 操作radio,设置选中、获取选中值
<label><input type="radio" name="sex" value="1">男</labe ...
- java EE实现动态SQL的
结果图 情况一:
- Python学习路程day12
前端内容学习:HTML和CSS <!DOCTYPE html> <html lang="en"> <head> <meta http-eq ...
- python模块之logging
在现实生活中,记录日志非常重要.银行转账时会有转账记录:飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切.如果有出现什么问题,人们可以通过日志数据来搞清楚到底发生了什么.对于系统开发. ...
- Python12期培训班-day1-登陆验证代码分享
#!/usr/bin/env python import sys import getpass afile = 'afile' bfile = 'bfile' circulation_num=0 #循 ...
- Jena语义Web开发101
2015/05/28更新 代码在 https://github.com/zhoujiagen/semanticWebTutorialUsingJena 前言 该手册参考和扩展“Hebeler J, F ...
- Java笔记5-修饰符,重载,递归,数组
方法的定义修饰符 返回类型 方法名(参数列表) throws 异常类型列表 { //方法体}==如何来设计一个方法:案例:请根据如下的业务分别设计出方法1.根据给定的年份判断是否是闰年?public ...
- jsp打印页面 js代码
function doPrint() { bdhtml=window.document.body.innerHTML; sprnstr=""; //开始打印标识字符串有17个字符 ...