Unaccepted 100分 TLE on Subtask #1 求卡常
查看原帖
Unaccepted 100分 TLE on Subtask #1 求卡常
556676
CJZJC楼主2025/1/16 11:34
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
const int base = 131, mod = 998442353;
int n, m, rt, idx;
long long bas[N];
string s;
struct fhqtreap
{
#define ls t[p].l
#define rs t[p].r
    int tot;
    struct stu
    {
        int l, r, s, val;
        long long hsh;
    } t[N];
    int newnode(int v)
    {
        tot++;
        t[tot].l = t[tot].r = 0;
        t[tot].s = 1;
        t[tot].val = t[tot].hsh = v;
        return tot;
    }
    void pushup(int p)
    {
        if (!p)
        {
            return;
        }
        t[p].s = t[ls].s + t[rs].s + 1;
        t[p].hsh = (t[ls].hsh * bas[t[rs].s + 1] % mod + t[p].val * bas[t[rs].s] % mod + t[rs].hsh) % mod;
    }
    void split(int p, int &l, int &r, int k)
    {
        if (!p)
        {
            l = 0;
            r = 0;
            return;
        }
        if (t[ls].s + 1 <= k)
        {
            l = p;
            split(rs, t[l].r, r, k - t[ls].s - 1);
        }
        else
        {
            r = p;
            split(ls, l, t[r].l, k);
        }
        pushup(p);
    }
    void merge(int x, int y, int &rot)
    {
        if ((!x) || (!y))
        {
            rot = x + y;
            return;
        }
        if (55667699 % (t[x].s + t[y].s) < t[x].s)
        {
            rot = x;
            merge(t[x].r, y, t[rot].r);
            pushup(x);
        }
        else
        {
            rot = y;
            merge(x, t[y].l, t[rot].l);
            pushup(y);
        }
    }
    void upd(int p, int v)
    {
        t[p].l = t[p].r = 0;
        t[p].s = 1;
        t[p].val = t[p].hsh = v;
    }
    int build(int l, int r)
    {
        if (l == r)
        {
            return newnode(s[l] - 'a' + 1);
        }
        int rot, mid = (l + r) >> 1;
        merge(build(l, mid), build(mid + 1, r), rot);
        return rot;
    }
} mtr;
bool check(int p1, int p2, int tt)
{
    int x, y, z, h1, h2;
    mtr.split(rt, x, y, p1 - 1);
    mtr.split(y, y, z, tt);
    h1 = mtr.t[y].hsh;
    mtr.merge(x, y, y);
    mtr.merge(y, z, rt);
    mtr.split(rt, x, y, p2 - 1);
    mtr.split(y, y, z, tt);
    h2 = mtr.t[y].hsh;
    mtr.merge(x, y, y);
    mtr.merge(y, z, rt);
    // cout << p1 << ' ' << p2 << ' ' << tt << ' ' << h1 << ' ' << h2 << '\n';
    return h1 == h2;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    bas[0] = 1;
    for (int i = 1; i <= 1000000; i++)
    {
        bas[i] = bas[i - 1] * base % mod;
    }
    cin >> s >> m;
    n = s.size();
    s = " " + s;
    mtr.merge(rt, mtr.build(1, n), rt);
    idx = n;
    while (m--)
    {
        char opt;
        cin >> opt;
        if (opt == 'R')
        {
            int pos;
            char c;
            cin >> pos >> c;
            int x, y, z;
            mtr.split(rt, x, y, pos - 1);
            mtr.split(y, y, z, 1);
            mtr.upd(y, c - 'a' + 1);
            mtr.merge(x, y, y);
            mtr.merge(y, z, rt);
        }
        if (opt == 'I')
        {
            int pos;
            cin >> pos >> s[1];
            int x, y;
            mtr.split(rt, x, y, pos);
            mtr.merge(x, mtr.build(1, 1), x);
            mtr.merge(x, y, rt);
            idx++;
        }
        if (opt == 'Q')
        {
            int x, y;
            cin >> x >> y;
            if (x > y)
            {
                swap(x, y);
            }
            int l = 1, r = idx - y + 1, ans = 0;
            while (l <= r)
            {
                int mid = (l + r) >> 1;
                if (check(x, y, mid))
                {
                    ans = mid;
                    l = mid + 1;
                }
                else
                {
                    r = mid - 1;
                }
            }
            cout << ans << '\n';
        }
    }
    return 0;
}
2025/1/16 11:34
加载中...