CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作:
“1 a b”:把a位置的字母换成b
“2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的匹配个数,如图。

思路:用树状数组预处理。因为模板串是不断重复循环的,所以我们可以一个位置一个位置求。对于长len的模板串来说,如果位置i,j满足 i%len == j%len,那么i和j匹配时对模板串来说是一样的(匹配同一个字符)。所以我们定义node[字母][模板串长度][相对位置][位置]来遍历某个位置字符在所有可能的相对位置的情况。
参考:Codeforces - 828E DNA Evolution —— 很多棵树状数组
代码:
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = + ;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
char s[maxn];
int h[];
int node[][][][maxn];
int lowbit(int x){
return x&(-x);
}
void update(int letter, int x, int val){
for(int i = x; i < maxn; i += lowbit(i)){
for(int len = ; len <= ; len++){
node[letter][len][x % len][i] += val;
}
}
}
int sum(int letter, int x, int len, int pos){
int ans = ;
for(int i = x; i > ; i -= lowbit(i)){
ans += node[letter][len][pos][i];
}
return ans;
}
int query(int l, int r, int len, int letter, int pos){
return sum(letter, r, len, pos) - sum(letter, l - , len, pos);
}
int main(){
memset(node, , sizeof(node));
h['A'] = , h['G'] = , h['C'] = , h['T'] = ;
scanf("%s", s + );
int len = strlen(s + );
for(int i = ; i <= len; i++){
update(h[s[i]], i, );
}
int q;
scanf("%d", &q);
while(q--){
int o,u,v;
char ss[];
scanf("%d", &o);
if(o == ){
scanf("%d%s", &u, ss);
update(h[s[u]], u, -);
update(h[ss[]], u, );
s[u] = ss[];
}
else{
scanf("%d%d%s", &u, &v, ss);
len = strlen(ss);
int ans = ;
for(int i = ; i < len; i++){
ans += query(u, v, len, h[ss[i]], (u + i) % len);
}
printf("%d\n", ans);
}
}
return ;
}
/*
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
*/
CodeForces 828E DNA Evolution(树状数组)题解的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
- Codeforces 650D - Zip-line(树状数组)
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...
- Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足 L >= li && R ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 522D Closest Equals 树状数组
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- VIM 插入
不知道有多少VIM新手和我当年(去年)一样,信誓旦旦的以为只有i可以插入 唉,现在想想都觉得可笑,都是Windows下的编辑器用多了的结果 鼠标一点,妈妈再也不用担心我的文本插入了……悲剧! 好了,让 ...
- linux 下 git gem 等代理设置问题
github.com,作为程序员的代码仓库,我们经常会用到.但有时候我们不能直接通过网络链接它,只能通过代理. 这里我有一台代理服务器,起初我以为在终端设置了代理环境就行了,其设置为在你的~/.bas ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- 【BZOJ5102】[POI2018]Prawnicy 堆
[BZOJ5102][POI2018]Prawnicy Description 定义一个区间(l,r)的长度为r-l,空区间的长度为0. 给定数轴上n个区间,请选择其中恰好k个区间,使得交集的长度最大 ...
- 【BZOJ4764】弹飞大爷 LCT
[BZOJ4764]弹飞大爷 Description 自从WC退役以来,大爷是越来越懒惰了.为了帮助他活动筋骨,也是受到了弹飞绵羊一题的启发,机房的小伙伴们决定齐心合力构造一个下面这样的序列.这个序列 ...
- 【BZOJ2768】[JLOI2010]冠军调查/【BZOJ1934】[Shoi2007]Vote 善意的投票 最小割
[BZOJ2768][JLOI2010]冠军调查 Description 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着卫冕冠军巴萨罗那的淘汰,英超劲旅切尔西成为了头号热门.新浪体育最近在吉林教 ...
- go练习3 --map的操作
func T2_1() { // 键值string , 值 int 类型 m1 := map[string]int{} //添加一个元素 m1["str1&q ...
- Appium中长按按钮操作
在一次项目中,appium要对某个按钮进行长按操作(大于2s),类似拍微信小视频,参考网上长按视频会报错 action1 = TouchActions(self.driver) el = self.d ...
- jvm原理之内存机制
转自:https://www.cnblogs.com/dreamowneryong/p/6381633.html JVM栈由堆.方法区,栈.本地方法栈.程序计数器等部分组成,结构图如下所示: 还有一张 ...
- java获取年份的第一天和最后一天
Calendar cal = Calendar.getInstance();cal.set(Calendar.MONTH, 0);cal.set(Calendar.DATE, 1);String da ...