BZOJ4896 [Thu Summer Camp2016]补退选 【trie树】

题目链接

BZOJ4896

题解

\(thu\)怎么那么喜欢出\(trie\)树的题。。。
我们当然可以按题意模拟建\(trie\)
询问的时候,由于存在删除操作,不满足单调性,不能直接二分答案
我们就在每个节点上用\(vector\)储存每个值第一次出现的时间点
每次询问找到那个点二分一下即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define pb(x) push_back(x)
using namespace std;
const int maxn = 100005,maxm = 7000005,INF = 1000000000;
inline LL read(){
    LL out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int n,lans;
int ch[maxm][10],sum[maxm],mx[maxm],cnt;
vector<cp> t[maxm];
char s[70];
void ins(int now,int v){
    int u = 0,id,len = strlen(s + 1);
    for (int i = 1; i <= len; i++){
        id = s[i] - 'a';
        sum[u] += v;
        if (sum[u] > mx[u]) t[u].pb(mp(sum[u],now)),mx[u] = sum[u];
        u = ch[u][id] ? ch[u][id] : ch[u][id] = ++cnt;
    }
    sum[u] += v;
    if (sum[u] > mx[u]) t[u].pb(mp(sum[u],now)),mx[u] = sum[u];
}
int query(LL v){
    int u = 0,len = strlen(s + 1);
    for (int i = 1; i <= len; i++)
        u = ch[u][s[i] - 'a'];
    int l = 0,r = t[u].size() - 1,mid;
    while (l < r){
        mid = l + r >> 1;
        if (t[u][mid].first > v) r = mid;
        else l = mid + 1;
    }
    return t[u][l].first > v ? t[u][l].second : -1;
}
int main(){
    n = read(); int opt; LL a,b,c,v;
    for (int i = 1; i <= n; i++){
        opt = read();
        scanf("%s",s + 1);
        if (opt == 1) ins(i,1);
        else if (opt == 2) ins(i,-1);
        else {
            a = read(); b = read(); c = read();
            v = (1ll * a * abs(lans) % c + b % c) % c;
            if (v >= i) lans = -1;
            else lans = query(v);
            printf("%d\n",lans);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Mychael/p/9086902.html