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 ...
随机推荐
- java中byte, int的转换
最近在做些与编解码相关的事情,又遇到了byte和int的转换,看着那些关于反码.补码的说明依旧头疼,还是记下些实用的方法吧.int -> byte可以直接使用强制类型转换: byte b = ( ...
- RoseRT 建模学习
目录: 一.RoseRT理论知识 二.一个完整模型的建立 三.TD-SCDMA(UE侧)RRC层建模的学习 四.LTE的RRC层建模(1.自主完成‘2.也可以是L2) 五.参考文献 一.RoseRT理 ...
- Android TextView里显示两种颜色
今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...
- 常用排序算法的Java实现 - 1
学习编程语言时, 我们会接触到许多排序算法, 这里总结了一下常见的排序算法. 不定期更新. * 其实在Java中存在如Collections.sort()这样的方法来自动为我们排序, 不过学习排序算法 ...
- HDU 1536 & 1944
http://acm.hdu.edu.cn/showproblem.php?pid=1536 http://acm.hdu.edu.cn/showproblem.php?pid=1944 一样的题 题 ...
- vim编辑下Python2.0自动补全
Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim python自动补全插件:pydiction 可以实现下面python代码 ...
- B-Tree算法分析与实现
在数据库系统中,或者说在文件系统中,针对存储在磁盘上的数据读取和在内存中是有非常大的区别的,因为内存针对任意在其中的数据是随机访问的,然而从磁盘中读取数据是需要通过机械的方式来读取一个block,不能 ...
- 通俗理解隐马尔科夫模型HMM(转载)
作者:Yang Eninala 链接:https://www.zhihu.com/question/20962240/answer/33438846 来源:知乎 著作权归作者所有,转载请联系作者获得授 ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- [cyber security][php]pfSense目录遍历漏洞分析
0×00 导言 pfSense是一个基于FreeBSD,专为防火墙和路由器功能定制的开源版本. 在本文中,我们将向大家介绍在pfSense的2.1.3以及更低版本中的CVE-2014-4690漏洞:对 ...