This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//{ <defines>
#include <bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,-O3")
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _ << ' ' <<
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int((x).size())
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset((x), (y), sizeof (x))
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;
inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
int md = 998244353;
inline int m_add(int&a, int b) {a += b; if(a < 0) a += md; if(a >= md) a -= md; re a;}
inline int m_sum(int a, int b) {a += b; if(a < 0) a += md; if(a >= md) a -= md; re a;}
inline int m_mul(int&a, int b) {re a = 1ll * a * b % md;}
inline int m_prod(int a, int b) {re 1ll * a * b % md;}
int m_bpow(ll A, ll b) {
int a = A % md;
ll ans = 1;
for(ll p = 1ll << 63 - __builtin_clzll(b); p; p >>= 1) {
(ans *= ans) %= md;
if(p & b)
(ans *= a) %= md;
}
re ans;
}
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int oo = 2e9;
const ll OO = 4e18;
//} </defines>
const int N = 3e5 + 5;
int f[N], n, si[N];
list<int> g[N];
ll ans;
void sizes(int v, int pr = -1) {
si[v] = 1;
for(int u : g[v]) {
if(u == pr) continue;
sizes(u, v);
si[v] += si[u];
}
}
int centroid(int n, int v, int pr = -1) {
for(int u : g[v]) {
if(u == pr || (si[u] << 1) < n) continue;
return centroid(n, u, v);
}
return v;
}
template<typename t>
void rec(t &cl, t &op, int v, int pr, int b = 0, int mn = 0, int mx = 0) {
b += f[v];
umax(mx, b);
umin(mn, b);
if(b - mx >= 0) op[b]++;
if(b - mn <= 0) cl[b]++;
for(int u : g[v]) if(u != pr)
rec(cl, op, u, v, b, mn, mx);
}
void make(int v) {
int m = g[v].size();
map<int, int> c, cl[m], op[m];
int i = 0;
for(int u : g[v]) {
rec(cl[i], op[i], u, v);
for(const auto &j : cl[i]) c[j.ft] += j.sd;
ans += f[v] < 0 ? op[i][1] : cl[i][-1];
i++;
}
fo(m) {
for(const auto &j : op[i]) {
int b = -(j.ft + f[v]);
ans += 1ll * j.sd * (c[b] - cl[i][b]);
}
}
}
void centree(int v) {
for(int u : g[v]) g[u].remove(v);
sizes(v);
make(v);
for(int u : g[v])
centree(centroid(si[u], u));
}
void solve() {
cin >> n;
fo(n) {
char c; cin >> c;
f[i] = c == '(' ? +1 : -1;
g[i].clear();
}
fo(n - 1) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].pb(y);
g[y].pb(x);
}
ans = 0;
sizes(0);
centree(centroid(n, 0));
cout << ans << endl;
}
int main() {
#ifdef _LOCAL
freopen("in.txt", "r", stdin);
int tests; cin >> tests;
for(int test = 1; test <= tests; ++test) {
cerr << "case #" << test << endl;
solve();
cerr << endl;
}
#else
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
#endif
return 0;
}
Compilation message (stderr)
zagrade.cpp: In function 'int m_bpow(ll, ll)':
zagrade.cpp:50:26: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
for(ll p = 1ll << 63 - __builtin_clzll(b); p; p >>= 1) {
~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |