CF_313B_IlyaAndQueries
来源:互联网 发布:网络百科全书 编辑:程序博客网 时间:2024/05/29 19:14
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.
You've got string s = s1s2... sn (n is the length of the string), consisting only of characters "." and "#" and m queries. Each query is described by a pair of integers li, ri (1 ≤ li < ri ≤ n). The answer to the query li, ri is the number of such integers i (li ≤ i < ri), thatsi = si + 1.
Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.
The first line contains string s of length n (2 ≤ n ≤ 105). It is guaranteed that the given string only consists of characters "." and "#".
The next line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li, ri (1 ≤ li < ri ≤ n).
Print m integers — the answers to the queries in the order in which they are given in the input.
......43 42 31 62 6
1154
#..###51 35 61 53 63 4
11220
最开始一看题目,区间和,马上想到树状数组
但是树状数组也打完了,反应过来,这根本没有数据修改的
根本用不到树状数组
直接扫一遍求和就可以了
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int M=1e5+5;char s[M];int a[M];int main(){ int nq; scanf("%s",s); scanf("%d",&nq); int len=strlen(s); for(int i=0;i<len;i++) { if(s[i]==s[i+1]) a[i+1]+=a[i]+1; else a[i+1]=a[i]; } int aa,b; for(int i=1;i<=nq;i++) { scanf("%d%d",&aa,&b); printf("%d\n",a[b-1]-a[aa-1]); } return 0;}