forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
113 lines (100 loc) · 2.69 KB
/
F.cpp
File metadata and controls
113 lines (100 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int Z = 500100;
struct SuffixArray
{
string s;
int n;
vector <int> sa, rank, tempSa, tempRank, c, lcp, prev;
SuffixArray(string _s) : s(_s) { initialize(); }
SuffixArray(char _s[], int len) : s(_s, len) { initialize(); }
void initialize()
{
n = s.size();
rank = sa = tempSa = tempRank = lcp = prev = vector <int>(n);
c = vector <int>(max(266, n));
}
void countingSort(int shift)
{
int C = max(266, n);
for (int i = 0; i < C; i++) c[i] = 0;
for (int i = 0; i < n; i++) c[rank[i]]++;
for (int i = 0, sum = 0 ; i < C; i++) sum += c[i], c[i] = sum - c[i];
for (int i = 0; i < n; i++)
if (sa[i] + shift < n) tempSa[c[rank[sa[i] + shift]]++] = sa[i];
else tempSa[c[rank[sa[i] + shift - n]]++] = sa[i];
for (int i = 0; i < n; i++) sa[i] = tempSa[i];
}
void construct()
{
for (int i = 0; i < n; i++) rank[i] = int(s[i]), sa[i] = i;
for (int shift = 1; shift < n; shift <<= 1)
{
countingSort(shift);
countingSort(0);
int R = tempRank[sa[0]] = 0;
for (int i = 1; i < n; i++)
{
if (rank[sa[i]] != rank[sa[i - 1]] || rank[(sa[i] + shift) % n] != rank[(sa[i - 1] + shift) % n]) R++;
tempRank[sa[i]] = R;
}
for (int i = 0; i < n; i++) rank[i] = tempRank[i];
}
}
void calcLCP()
{
for (int i = 0; i < n; i++) prev[sa[i]] = i ? sa[i - 1] : -1;
for (int i = 0, match = 0; i < n; i++)
if (prev[i] < 0) lcp[i] = 0;
else
{
while (s[(i + match) % n] == s[(prev[i] + match) % n]) match++;
lcp[i] = match;
if (!match--) match = 0;
}
}
};
char s[Z];
int st[Z], last;
vector <int> a[Z + Z];
int calc(const vector <int>&a, int val)
{
int low = 0, high = a.size() - 1, where = -1;
while (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] > val) where = mid, low = mid + 1;
else high = mid - 1;
}
return where + 1;
}
int main()
{
int n;
scanf("%d\n", &n);
scanf("%s", s);
s[n++] = '$';
SuffixArray SA(s, n);
SA.construct();
SA.calcLCP();
for (int i = 0; i <= n + Z; i++) a[i].clear();
a[Z].push_back(n - 1);
st[last = 1] = 0;
long long ans = 0;
for (int i = n - 2, x = 0; i >= 0; i--)
if (s[i] == ')') st[++last] = ++x, a[x + Z].push_back(i);
else
{
x--;
while (last && st[last] > x) a[st[last--] + Z].pop_back();
st[++last] = x;
ans += calc(a[x + Z], i + SA.lcp[i]);
a[x + Z].push_back(i);
}
cout << ans << endl;
}