0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P3059 [USACO12NOV]Concurrently Balanced Strings G

https://www.luogu.com.cn/record/84217751
This commit is contained in:
Baoshuo Ren 2022-08-18 07:50:38 +08:00
parent aec161e895
commit ac163af77c
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
13 changed files with 117 additions and 0 deletions

81
Luogu/P3059/P3059.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 50005;
const int hash = 1000003;
const int mod = 1e9 + 7;
int k, n, a[15][N], s[15][N], e[N], next[15][N], ans;
std::vector<int> nums, p[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> k >> n;
for (int i = 1; i <= k; i++) {
std::string str;
cin >> str;
for (int j = 1; j <= n; j++) {
a[i][j] = str[j - 1] == '(' ? 1 : -1;
s[i][j] = s[i][j - 1] + a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
e[i] = ((static_cast<long long>(e[i]) * hash % mod + s[j][i]) % mod + mod) % mod;
}
nums.push_back(e[i]);
}
std::sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
p[0].emplace_back(0);
for (int i = 1; i <= n; i++) {
e[i] = std::lower_bound(nums.begin(), nums.end(), e[i]) - nums.begin() + 1;
p[e[i]].emplace_back(i);
}
for (int i = 1; i <= k; i++) {
std::stack<int> st;
for (int j = 1; j <= n; j++) {
while (!st.empty() && s[i][st.top()] > s[i][j]) {
next[i][st.top()] = j;
st.pop();
}
st.emplace(j);
}
}
for (int i = 1; i <= n; i++) {
int min = n;
for (int j = 1; j <= k; j++) {
if (next[j][i]) {
min = std::min(min, next[j][i]);
}
}
ans += std::upper_bound(p[e[i]].begin(), p[e[i]].end(), min) - std::lower_bound(p[e[i]].begin(), p[e[i]].end(), i + 1);
}
cout << ans << endl;
return 0;
}

BIN
Luogu/P3059/data/P3059_1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P3059/data/P3059_9.out (Stored with Git LFS) Normal file

Binary file not shown.