题目链接:
http://codeforces.com/problemset/problem/827/C
题解:
很好的树状数组的题目
代码:
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 1e5+10;
int Tree[4][11][11][maxn];
map<char,int> mp;
void init()
{
met(Tree,0);
mp.clear();
mp['A']=0;
mp['T']=1;
mp['G']=2;
mp['C']=3;
}
int lowbit(int k)
{
return k&(-k);
}
void add(int type,int pos,int val)
{
for(int i=pos;i<maxn;i+=lowbit(i))
for(int j=1;j<=10;j++)
Tree[type][j][pos%j][i]+=val;
}
int query(int type,int len,int mod,int pos)
{
int ans=0;
for(int i=pos;i>0;i-=lowbit(i))
{
ans+=Tree[type][len][mod][i];
}
return ans;
}
int sum(int l,int r,int type,int len,int mod)
{
return query(type,len,mod,r)-query(type,len,mod,l-1);
}
int main()
{
init();
string s;
cin>>s;
for(int i=0;i<s.size();i++)
add(mp[s[i]],i+1,1);
int q;
scanf("%d",&q);
while(q--)
{
int num;
scanf("%d",&num);
if(num==1)
{
int x;
char y[100];
scanf("%d%s",&x,y);
add(mp[s[x-1]],x,-1);
add(mp[y[0]],x,1);
s[x-1]=y[0];
}
else
{
int x,y;
string ss;
scanf("%d%d",&x,&y);
cin>>ss;
int ans=0;
int len=(int)ss.size();
for(int i=0;i<len;i++)
{
ans+=sum(x,y,mp[ss[i]],len,(i+x)%len);
}
printf("%d\n",ans);
}
}
}