当前位置: 移动技术网 > IT编程>开发语言>C/C++ > loj#6041. 「雅礼集训 2017 Day7」事情的相似度(SAM set启发式合并 二维数点)

loj#6041. 「雅礼集训 2017 Day7」事情的相似度(SAM set启发式合并 二维数点)

2019年03月30日  | 移动技术网IT编程  | 我要评论

acg宅,utilty.exe,先烈事迹

题意

sol

只会后缀数组+暴躁莫队套set\(n \sqrt{n} \log n\)但绝对跑不过去。

正解是sam + set启发式合并 + 二维数点/ sam + lct

但是我只会第一种qwq

首先一个性质是两个前缀的最长公共后缀就是他们再parent树上的lca的len

那么我们考虑每个lca的贡献。

把询问离线下来按右端点排序,对于当前点的子树中的点有一个显然的性质。

若存在四个点\(l, x, y, r\)满足\(l < x < y < r\),那么显然\(l, r\)这对点是没有意义的(因为每对点产生的贡献都相同)。也就说我们在处理子树的时候实际上有一堆点对用不到。我们可以通过set启发式合并来合并子树,也就是说我现在有一堆点集,然后我考虑加入一个新点之后哪些点对会有用,显然只有它与它的前驱/后继这两个点对是有用的。

因为合并的时候是启发式合并,所以总复杂度不会超过\(n \log^2 n\)

然后处理完之后就是一个二维数点取max问题了。

调起来有点自闭qwq

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define pb push_back 
//#define int long long 
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x * x;}
template <typename a, typename b> inline ll fp(a a, b p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename a> a inv(a x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, ans[maxn];
char s[maxn];
vector<pair> q[maxn], p[maxn];
vector<int> v[maxn];
set<int> st[maxn];
int ch[maxn][2], len[maxn], fa[maxn], las = 1, root = 1, tot = 1;
void insert(int x, int id) {
    int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1; 
    st[now].insert(id);

    for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
    if(!pre) fa[now] = root;
    else {
        int q = ch[pre][x];
        if(len[q] == len[pre] + 1) fa[now] = q;
        else {
            int nq = ++tot; 
            fa[nq] = fa[q]; len[nq] = len[pre] + 1; 
            memcpy(ch[nq], ch[q], sizeof(ch[q]));
            for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
            fa[now] = fa[q] = nq;
        }
    }
}
void dfs(int x) {
    set<int> &s = st[x]; 
    for(auto &to : v[x]) {
        dfs(to);
    
        set<int> &sto = st[to];
        if(sto.size() > s.size()) swap(sto, s);
        for(auto &nxt: sto) {
            auto pos = s.insert(nxt).fi;
            if(pos != s.begin()) {
                auto pre = --pos; pos++;
            //  printf("%d %d %d\n", *pos, *pre, len[x]);
                p[*pos].pb({*pre, len[x]});
            }
            if((++pos) != s.end()) {
                pos--;
                auto nxt = ++pos; pos--;
                //printf("%d %d\n", *pos, *nxt);
                p[*nxt].pb({*pos, len[x]});
            }
            s.erase(nxt);
        }
        for(auto &nxt: sto) s.insert(nxt);
    }
}
void build() {
    for(int i = 1; i <= tot; i++) v[fa[i]].push_back(i);
    dfs(1);
    for(int i = 1; i <= n; i++) sort(q[i].begin(), q[i].end()), sort(p[i].begin(), p[i].end());
}
int mx[maxn];
#define lb(x) (x & (-x))
void add(int x, int val) {
    x = n - x + 1;
    while(x <= n) chmax(mx[x], val), x += lb(x);
}
int query(int x) {
    x = n - x + 1;
    int ans = 0;
    while(x) chmax(ans, mx[x]), x -= lb(x);
    return ans;
}
void solve() {
    for(int i = 1; i <= n; i++) {
        int cur = q[i].size() - 1;
        for(int j = p[i].size() - 1; j >= 0; j--) {
            while((~cur) && q[i][cur].fi > p[i][j].fi) ans[q[i][cur].se] = query(q[i][cur].fi), cur--;
            add(p[i][j].fi, p[i][j].se);
        }
        while(~cur) 
            ans[q[i][cur].se] = query(q[i][cur].fi), cur--;
        
    }
}
signed main() {
    //freopen("a.in", "r", stdin);
    n = read(); m = read();
    scanf("%s", s + 1);
    for(int i = 1; i <= n; i++) insert(s[i] - '0', i);
    for(int i = 1; i <= m; i++) {
        int l = read(), r = read();
        q[r].pb({l, i});
    }
    build();
    solve();
    for(int i = 1; i <= m; i++) cout << ans[i] << '\n';
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网