Codeforces 828 E DNA Evolution(树状数组方法)

来源:互联网 发布:淘宝摄影店 编辑:程序博客网 时间:2024/05/22 06:20

题目地址:http://codeforces.com/contest/828/problem/E
题意:给你个DNA序列,有两个操作,一个是改变DNA序列里的一个碱基,另一个是查询l~r中有多少是和另一个序列(给出的序列不断重复形成的)碱基相同的,求出相同碱基的数量。
思路:因为操作的次数太多了,所以可以想到用树状数组去存,预处理好,那样查询就特别方便了,

TreeArray T[后面要查询的序列长度][在原序列的位置%序列长度(PS:把它化成一个一个的块)][碱基];

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#define LL long long #define N 100010#define M 50010#define inf 0x3f3f3f3fusing namespace std;const LL mod = 1e9 + 7;const double eps = 1e-9;struct TreeArray {    int d[N];    void add(int x, int y) {        while (x<N) {            d[x] += y;            x += x&-x;        }    }    int get(int x) {        int ans = 0;        while (x) {            ans += d[x];            x -= x&-x;        }        return ans;    }    int get(int l, int r) {        return get(r) - get(l - 1);    }}T[11][11][4];int solve(char s) {    if (s == 'A') return 0;    if (s == 'T') return 1;    if (s == 'C') return 2;    if (s == 'G') return 3;}int main() {    cin.sync_with_stdio(false);    int op, q, num, l, r;    string str, s;    while (cin >> str) {        for (int i = 1; i <= 10; i++) {            for (int j = 0; j < str.length(); j++) {                T[i][(j + 1) % i][solve(str[j])].add(j + 1, 1);            }        }        cin >> q;        while (q--) {            cin >> op;            if (op == 1) {                cin >> num >> s;                for (int i = 1; i <= 10; i++) {                    T[i][num%i][solve(str[num - 1])].add(num, -1);                    T[i][num%i][solve(s[0])].add(num, 1);                }                str[num - 1] = s[0];            }            else {                cin >> l >> r >> s;                int len = s.length();                int ans = 0;                for (int i = 0; i < len; i++) {                    ans += T[len][(l + i) % len][solve(s[i])].get(l, r);                }                cout << ans << endl;            }        }    }    return 0;}
阅读全文
0 0