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\n", ans);
    return 0;
}

/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/10648337.html

时间: 2024-07-30 14:10:07

Codeforces 653F Paper task SA的相关文章

Codeforces 118A String Task

题目链接 http://codeforces.com/problemset/problem/118/A #include<iostream> #include<string> #include<cctype> #include<algorithm> using namespace std; int main() { string str; cin>>str; int i; //将代码变成小写 transform(str.begin(), str.

codeforces 558EA Simple Task

开始试想直接用容器来暴力做,,,,,, 恩.....线段树.... 记不得以前写没写过了,,,, 先来个模板先(就是那种n个询问成段更新的题) #include<stdio.h> #include<string.h> #include <algorithm> #include <bits/stdc++.h> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 ,

Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessar

一些字符串有关的题目

模板可以在上一篇文章中找到. 因为最近都没有做codeforces,所以这篇文章的主要题目来源就是codeforces啦~ 需要这类题目可以在codeforces上找到hashing.string suffix structures之类的标签. 这些题目都是随便点的,所以有些题目和字符串并没有太大的关系 CF653F Paper Task(非常规比赛) 给一个长度为n的由左右括号做成的字符串,求它子串中不同括号序列的个数. (注意不是求是合法括号序列的子串数量,而是不同括号序列个数) 1<=n<

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作,每次操作将(l,r)内的字符升序或降序排列,输出q次操作后的字符串. analyse: 基本思想是计数排序. 所谓计数排序,是对一个元素分布较集中的数字集群进行排序的算法,时间复杂度为O(n),但使用条件很苛刻.首先对n个数扫一遍,映射出每个数字出现的次数,然后再O(n)扫一遍处理出:对于数字ai,

Codeforces 467E Alex and Complicated Task(高效)

题目链接:Codeforces 467E Alex and Complicated Task 题目大意:给定一个长度为n序列,然后从中挑选尽量多的4元组(不能重叠). 解题思路:每次找的四元组的左端肯定是要尽量小的.所以用一个单调栈维护,如果新加入的数x在栈中出现过,那么就将两个数之间的数标记为在x.如果一个数的标记不为空,就意味着找到对应的四元组.有因为序列是从左遍历过去的,所以找到的一定是最优的. #include <cstdio> #include <cstring> #in

Codeforces 431 D. Random Task

很巧妙的单调性...... n,n+1,n+2,.....2*n-2 n+1,n+2,.....2*n-2,2*n-1,2*n 中间一段是相同的,n和2*n里的1是一样多的所以只有2*n-1不一样.....这是满足单调性的(monotone) 然后就是数位DP了..... D. Random Task time limit per test 1 second memory limit per test 256 megabytes input standard input output stand

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have