解题报告 『语文1(chin1)- 理理思维(ODT)』

原题地址

即使被强制退役也要坚持用珂朵莉树水题。

 

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define IT set<node>::iterator
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)

const int maxn = 5e4 + 5;

int n, m;
char str[maxn];

struct node {
    int l, r;
    mutable char v;
    node(int L, int R = -1, char V = 0): l(L), r(R), v(V) {}
    int operator <(const node &o) const {
        return l < o.l;
    }
};

set<node> s;

int read() {
    int x = 0, flag = 0;
    char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') {
        flag = 1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ '0');
        ch = getchar();
    }
    return flag ? -x : x;
}

IT split(int pos) {
    IT it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) return it;
    it--;
    int L = it->l, R = it->r;
    char V = it->v;
    s.erase(it);
    s.insert(node(L, pos - 1, V));
    return s.insert(node(pos, R, V)).first;
}

void assign(int l, int r, char val) {
    IT itr = split(r + 1), itl = split(l);
    s.erase(itl, itr);
    s.insert(node(l, r, val));
}

int query(int l, int r, char val) {
    IT itr = split(r + 1), itl = split(l);
    int ans = 0;
    while (itl != itr) {
        ans += itl->v == val ? itl->r - itl->l + 1 : 0;
        itl++;
    }
    return ans;
}

void quick_sort(int l, int r) {
    IT itr = split(r + 1), itl = split(l);
    IT it = itl;
    int bow[30];
    memset(bow, 0, sizeof(bow));
    while (itl != itr) {
        bow[itl->v - 'A'] += itl->r - itl->l + 1;
        itl++;
    }
    s.erase(it, itr);
    rep(i, 0, 25)
        if (bow[i]) {
            s.insert(node(l, l + bow[i] - 1, i + 'A'));
            l += bow[i];
        }
}

void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    n = read(), m = read();
    scanf("%s", str + 1);
    int cnt = 1;
    char pre = toupper(str[1]);
    rep(i, 2, n) {
        str[i] = toupper(str[i]);
        if (pre == str[i]) cnt++;
        else {
            s.insert(node(i - cnt, i - 1, pre));
            cnt = 1;
            pre = str[i];
        }
    }
    s.insert(node(n - cnt + 1, n, pre));
    char tmp[3];
    rep(i, 1, m) {
        int l, r, opt;
        opt = read(), l = read(), r = read();
        if (opt == 1) {
            scanf("%s", tmp);
            write(query(l, r, toupper(tmp[0])));
            printf("\n");
        }
        else if (opt == 2) {
            scanf("%s", tmp);
            assign(l, r, toupper(tmp[0]));
        }
        else quick_sort(l, r);
    }
    return 0;
}
View Code
posted @ 2019-08-16 21:07  雲裏霧裏沙  阅读(125)  评论(0编辑  收藏  举报