[暴力]cf229CInna and Candy Boxes

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 numberk and asked w questions to Dima to find that out. Each question is characterised by two integersli, 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 fromli tori, inclusive, the candies lieonly 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 containsn 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 integersli andri(1 ≤ li ≤ ri ≤ n) — the description of thei-th question. It is guaranteed that ri - li + 1 is divisible byk.

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.


这是静态的统计问题,每隔k位置的1的总数,对k求余,可以预处理出这样的k个和。另外我们还需要知道某个区间非右端点的1的个数(可以用前缀和减去前者)

因此维护出两个前缀和即可,一个记录1~n的1的个数,一个记录1~n的非右端点的1的个数。


#include <cstdio>
int sum[100010];
int sum2[20][100010];
char str[100010];

int main()
{
	int n,k,w;
	scanf("%d%d%d",&n,&k,&w);
	scanf("%s",str+1);
	for (int i=1;i<=n;i++)
	{
		str[i]-='0';
		sum[i] = sum[i-1]+str[i];
		sum2[i%k][i] = str[i];
	}
	for (int j=0;j<k;j++)
	{
		for (int i=1;i<=n;i++)
		{
			sum2[j][i] += sum2[j][i-1];
		}
	}
	for (int i=1;i<=w;i++)
	{
		if (i>1)
			printf("\n");
		int l,r;
		scanf("%d%d",&l,&r);
		int yu = (l-1)%k;
		int mk = sum2[yu][r] - sum2[yu][l-1];
		int nm = sum[r] - sum[l-1] - mk;

		int ans = ((r-l+1)/k-mk)+nm;
		printf("%d",ans);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值