• Codeforces 653F Paper task SA


    Paper task

    如果不要求本质不同直接st表二分找出最右端, 然后计数就好了。

    要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    
    using namespace std;
    
    const int N = 5e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 998244353;
    const double eps = 1e-6;
    const double PI = acos(-1);
    
    int Log[N];
    struct ST {
        int dp[N][20], ty;
        void build(int n, int b[], int _ty) {
            ty = _ty;
            for(int i = -(Log[0]=-1); i < N; i++)
            Log[i] = Log[i - 1] + ((i & (i - 1)) == 0);
            for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i];
            for(int j = 1; j <= Log[n]; j++)
                for(int i = 1; i + (1 << j) - 1 <= n; i++)
                    dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
        }
        int query(int x, int y) {
            int k = Log[y - x + 1];
            return ty * max(dp[x][k], dp[y - (1 << k) + 1][k]);
        }
    };
    
    const int MAX = 6e5;
    const int MIN = -6e5;
    
    namespace SGT {
        int tot = 0, Rt[N];
        struct Node {
            int sum, ls, rs;
        } a[N * 25];
        void modify(int p, int l, int r, int& x, int y) {
            x = ++tot; a[x] = a[y]; a[x].sum++;
            if(l == r) return;
            int mid = l + r >> 1;
            if(p <= mid) modify(p, l, mid, a[x].ls, a[y].ls);
            else modify(p, mid + 1, r, a[x].rs, a[y].rs);
        }
        int query(int p, int l, int r, int x) {
            if(l == r) return a[x].sum;
            int mid = l + r >> 1;
            if(p <= mid) query(p, l, mid, a[x].ls);
            else query(p, mid + 1, r, a[x].rs);
        }
    }
    
    int sa[N], t[N], t2[N], c[N], rk[N], lcp[N];
    void buildSa(char *s, int n, int m) {
        int i, j = 0, k = 0, *x = t, *y = t2;
        for(i = 0; i < m; i++) c[i] = 0;
        for(i = 0; i < n; i++) c[x[i] = s[i]]++;
        for(i = 1; i < m; i++) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
        for(int k = 1; k <= n; k <<= 1) {
            int p = 0;
            for(i = n - k; i < n; i++) y[p++] = i;
            for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
            for(i = 0; i < m; i++) c[i] = 0;
            for(i = 0; i < n; i++) c[x[y[i]]]++;
            for(i = 1; i < m; i++) c[i] += c[i - 1];
            for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
            swap(x, y);
            p = 1; x[sa[0]] = 0;
            for(int i = 1; i < n; i++) {
                if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])
                    x[sa[i]] = p - 1;
                else x[sa[i]] = p++;
            }
            if(p >= n) break;
            m = p;
         }
    
         for(i = 1; i < n; i++) rk[sa[i]] = i;
         for(i = 0; i < n - 1; i++) {
            if(k) k--;
            j = sa[rk[i] - 1];
            while(s[i + k] == s[j + k]) k++;
            lcp[rk[i]] = k;
         }
    }
    
    int n, a[N], p[N];
    char s[N];
    ST rmq;
    
    int main() {
        scanf("%d", &n);
        scanf("%s", s + 1);
        for(int i = 1; i <= n; i++) {
            a[i] = s[i] == '(' ? 1 : -1;
            a[i] += a[i - 1];
        }
        rmq.build(n, a, -1);
        for(int i = 1; i <= n; i++)
            SGT::modify(a[i], MIN, MAX, SGT::Rt[i], SGT::Rt[i - 1]);
        for(int i = 1; i <= n; i++) {
            if(s[i] == ')') continue;
            int low = i + 1, high = n; p[i] = i;
            while(low <= high) {
                int mid = low + high >> 1;
                if(rmq.query(i, mid) >= a[i] - 1) p[i] = mid, low = mid + 1;
                else high = mid - 1;
            }
        }
        LL ans = 0;
        buildSa(s + 1, n + 1, 255);
        for(int i = 1; i <= n; i++) {
            int x = sa[i] + 1;
            if(s[x] == ')') continue;
            int dn = x + lcp[i], up = p[x];
            if(dn <= up) {
                ans += SGT::query(a[x] - 1, MIN, MAX, SGT::Rt[up]);
                ans -= SGT::query(a[x] - 1, MIN, MAX, SGT::Rt[dn - 1]);
            }
        }
        printf("%lld
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    Unity 5.3 Assetbundle热更资源
    自定义协同程序:CustomYieldInstruction
    C# 温故而知新: 线程篇(四)
    C# 温故而知新: 线程篇(三)
    C# 温故而知新: 线程篇(二)
    c# 温故而知新: 线程篇(一)
    C# 温故而知新:Stream篇(六)
    C# 温故而知新:Stream篇(七)
    C# 温故而知新:Stream篇(四)
    Redis高级数据类型
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10648337.html
Copyright © 2020-2023  润新知