QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#198262 | #5017. 相等树链 | hos_lyric# | 30 | 1154ms | 45856kb | C++14 | 10.8kb | 2023-10-03 11:20:04 | 2024-07-04 02:15:30 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// T: monoid representing information of an interval.
// T() should return the identity.
// T(S s) should represent a single element of the array.
// T::push(T &l, T &r) should push the lazy update.
// T::pull(const T &l, const T &r) should pull two intervals.
template <class T> struct SegmentTreeRange {
int logN, n;
vector<T> ts;
SegmentTreeRange() : logN(0), n(0) {}
explicit SegmentTreeRange(int n_) {
for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
ts.resize(n << 1);
}
template <class S> explicit SegmentTreeRange(const vector<S> &ss) {
const int n_ = ss.size();
for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
ts.resize(n << 1);
for (int i = 0; i < n_; ++i) at(i) = T(ss[i]);
build();
}
T &at(int i) {
return ts[n + i];
}
void build() {
for (int u = n; --u; ) pull(u);
}
inline void push(int u) {
ts[u].push(ts[u << 1], ts[u << 1 | 1]);
}
inline void pull(int u) {
ts[u].pull(ts[u << 1], ts[u << 1 | 1]);
}
// Applies T::f(args...) to [a, b).
template <class F, class... Args>
void ch(int a, int b, F f, Args &&... args) {
assert(0 <= a); assert(a <= b); assert(b <= n);
if (a == b) return;
a += n; b += n;
for (int h = logN; h; --h) {
const int aa = a >> h, bb = b >> h;
if (aa == bb) {
if ((aa << h) != a || (bb << h) != b) push(aa);
} else {
if ((aa << h) != a) push(aa);
if ((bb << h) != b) push(bb);
}
}
for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
if (aa & 1) (ts[aa++].*f)(args...);
if (bb & 1) (ts[--bb].*f)(args...);
}
for (int h = 1; h <= logN; ++h) {
const int aa = a >> h, bb = b >> h;
if (aa == bb) {
if ((aa << h) != a || (bb << h) != b) pull(aa);
} else {
if ((aa << h) != a) pull(aa);
if ((bb << h) != b) pull(bb);
}
}
}
// Calculates the product for [a, b).
T get(int a, int b) {
assert(0 <= a); assert(a <= b); assert(b <= n);
if (a == b) return T();
a += n; b += n;
for (int h = logN; h; --h) {
const int aa = a >> h, bb = b >> h;
if (aa == bb) {
if ((aa << h) != a || (bb << h) != b) push(aa);
} else {
if ((aa << h) != a) push(aa);
if ((bb << h) != b) push(bb);
}
}
T prodL, prodR, t;
for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
if (aa & 1) { t.pull(prodL, ts[aa++]); prodL = t; }
if (bb & 1) { t.pull(ts[--bb], prodR); prodR = t; }
}
t.pull(prodL, prodR);
return t;
}
// Calculates T::f(args...) of a monoid type for [a, b).
// op(-, -) should calculate the product.
// e() should return the identity.
template <class Op, class E, class F, class... Args>
#if __cplusplus >= 201402L
auto
#else
decltype((std::declval<T>().*F())())
#endif
get(int a, int b, Op op, E e, F f, Args &&... args) {
assert(0 <= a); assert(a <= b); assert(b <= n);
if (a == b) return e();
a += n; b += n;
for (int h = logN; h; --h) {
const int aa = a >> h, bb = b >> h;
if (aa == bb) {
if ((aa << h) != a || (bb << h) != b) push(aa);
} else {
if ((aa << h) != a) push(aa);
if ((bb << h) != b) push(bb);
}
}
auto prodL = e(), prodR = e();
for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...));
if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR);
}
return op(prodL, prodR);
}
// Find min b s.t. T::f(args...) returns true,
// when called for the partition of [a, b) from left to right.
// Returns n + 1 if there is no such b.
template <class F, class... Args>
int findRight(int a, F f, Args &&... args) {
assert(0 <= a); assert(a <= n);
if ((T().*f)(args...)) return a;
if (a == n) return n + 1;
a += n;
for (int h = logN; h; --h) push(a >> h);
for (; ; a >>= 1) if (a & 1) {
if ((ts[a].*f)(args...)) {
for (; a < n; ) {
push(a);
if (!(ts[a <<= 1].*f)(args...)) ++a;
}
return a - n + 1;
}
++a;
if (!(a & (a - 1))) return n + 1;
}
}
// Find max a s.t. T::f(args...) returns true,
// when called for the partition of [a, b) from right to left.
// Returns -1 if there is no such a.
template <class F, class... Args>
int findLeft(int b, F f, Args &&... args) {
assert(0 <= b); assert(b <= n);
if ((T().*f)(args...)) return b;
if (b == 0) return -1;
b += n;
for (int h = logN; h; --h) push((b - 1) >> h);
for (; ; b >>= 1) if ((b & 1) || b == 2) {
if ((ts[b - 1].*f)(args...)) {
for (; b <= n; ) {
push(b - 1);
if (!(ts[(b <<= 1) - 1].*f)(args...)) --b;
}
return b - n - 1;
}
--b;
if (!(b & (b - 1))) return -1;
}
}
};
////////////////////////////////////////////////////////////////////////////////
using Value = int;
constexpr Value INF = 1001001001;
struct NodeCountMin {
Value mn, lz;
int cnt;
NodeCountMin() : mn(INF), lz(0), cnt(0) {}
NodeCountMin(Value val) : mn(val), lz(0), cnt(1) {}
void push(NodeCountMin &l, NodeCountMin &r) {
if (lz) {
l.add(lz);
r.add(lz);
lz = 0;
}
}
void pull(const NodeCountMin &l, const NodeCountMin &r) {
if (l.mn < r.mn) {
mn = l.mn;
cnt = l.cnt;
} else if (l.mn > r.mn) {
mn = r.mn;
cnt = r.cnt;
} else {
mn = l.mn;
cnt = l.cnt + r.cnt;
}
}
void add(Value val) {
mn += val;
lz += val;
}
// leaf
void change(int val) {
mn = val;
cnt = 1;
}
};
////////////////////////////////////////////////////////////////////////////////
int N;
vector<int> P[2];
// bidirectional
vector<vector<int>> graph[2];
namespace brute {
Int ans;
vector<int> on, deg;
int isol, leaf;
void add(int u) {
on[u] = 1;
++isol;
for (const int v : graph[1][u]) if (on[v]) {
if (deg[u] == 0) { --isol; ++leaf; }
if (deg[v] == 0) { --isol; ++leaf; }
if (deg[u] == 1) --leaf;
if (deg[v] == 1) --leaf;
++deg[u];
++deg[v];
}
}
void rem(int u) {
for (const int v : graph[1][u]) if (on[v]) {
--deg[u];
--deg[v];
if (deg[u] == 1) ++leaf;
if (deg[v] == 1) ++leaf;
if (deg[u] == 0) { ++isol; --leaf; }
if (deg[v] == 0) { ++isol; --leaf; }
}
--isol;
on[u] = 0;
}
void dfs(int u, int p) {
add(u);
if (isol == 0 && leaf == 2) {
// cerr<<" u = "<<u<<", on = "<<on<<", deg = "<<deg<<", isol = "<<isol<<", leaf = "<<leaf<<endl;
++ans;
}
for (const int v : graph[0][u]) if (p != v) {
dfs(v, u);
}
rem(u);
}
Int run() {
cerr<<"[brute::run]"<<endl;
ans = 0;
on.assign(N, 0);
deg.assign(N, 0);
isol = leaf = 0;
for (int u = 0; u < N; ++u) {
// cerr<<"root: "<<u<<endl;
dfs(u, -1);
}
assert(ans % 2 == 0);
ans /= 2;
ans += N;
return ans;
}
} // brute
namespace sub2 {
int zeit;
vector<int> idss[2];
void dfs(int h, int u, int p) {
idss[h][u] = zeit++;
for (const int v : graph[h][u]) if (p != v) {
dfs(h, v, u);
}
}
Int run() {
cerr<<"[sub2::run]"<<endl;
for (int h = 0; h < 2; ++h) {
for (int u = 0; u < N; ++u) if (graph[h][u].size() <= 1) {
zeit = 0;
idss[h].assign(N, -1);
dfs(h, u, -1);
break;
}
}
vector<int> perm(N, -1);
for (int u = 0; u < N; ++u) {
perm[idss[0][u]] = idss[1][u];
}
// cerr<<"perm = "<<perm<<endl;
SegmentTreeRange<NodeCountMin> seg(N);
for (int i = 0; i < N; ++i) {
seg.at(i) = NodeCountMin(i);
}
Int ans = 0;
vector<pair<int, int>> stackMin, stackMax;
stackMin.emplace_back(-INF, 0);
stackMax.emplace_back(+INF, 0);
for (int i = 0; i < N; ++i) {
for (; stackMin.back().first > perm[i]; ) {
const auto p = stackMin.back();
stackMin.pop_back();
seg.ch(stackMin.back().second, p.second, &NodeCountMin::add, +p.first);
}
seg.ch(stackMin.back().second, i + 1, &NodeCountMin::add, -perm[i]);
stackMin.emplace_back(perm[i], i + 1);
for (; stackMax.back().first < perm[i]; ) {
const auto p = stackMax.back();
stackMax.pop_back();
seg.ch(stackMax.back().second, p.second, &NodeCountMin::add, -p.first);
}
seg.ch(stackMax.back().second, i + 1, &NodeCountMin::add, +perm[i]);
stackMax.emplace_back(perm[i], i + 1);
const auto res = seg.get(0, i + 1);
if (res.mn <= i) {
ans += res.cnt;
}
}
return ans;
}
} // sub2
int main() {
for (; ~scanf("%d", &N); ) {
for (int h = 0; h < 2; ++h) {
P[h].assign(N, -1);
for (int u = 1; u < N; ++u) {
scanf("%d", &P[h][u]);
--P[h][u];
}
}
for (int h = 0; h < 2; ++h) {
graph[h].assign(N, {});
for (int u = 1; u < N; ++u) {
graph[h][P[h][u]].push_back(u);
graph[h][u].push_back(P[h][u]);
}
}
int maxDeg = -1;
for (int h = 0; h < 2; ++h) {
for (int u = 0; u < N; ++u) {
chmax(maxDeg, (int)graph[h][u].size());
}
}
Int ans;
if (maxDeg <= 2) {
ans = sub2::run();
} else if (N <= 5000) {
ans = brute::run();
} else {
assert(false);
}
printf("%lld\n", ans);
#ifdef LOCAL
if(N<=5000){
const Int brt=brute::run();
if(brt!=ans)cerr<<"brt = "<<brt<<", ans = "<<ans<<endl;
assert(brt==ans);
}
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1124ms
memory: 4452kb
input:
5000 1296 1400 867 4533 1296 2007 2059 115 821 2960 3187 1597 2409 2708 4743 4778 1345 3967 911 3400 4249 3793 339 3145 3490 607 4148 3513 3264 3852 568 775 828 1348 423 3678 305 1938 1096 1373 2662 1048 4328 4208 203 779 3103 3372 4523 192 264 792 4943 2211 2494 3513 3555 4935 3277 3390 4624 128 18...
output:
76002
result:
ok 1 number(s): "76002"
Test #2:
score: 0
Accepted
time: 1061ms
memory: 4472kb
input:
5000 1820 281 610 3735 3580 3994 2060 2424 3338 2859 281 532 1286 1771 825 3738 3793 2260 556 4068 3793 4169 4411 4941 122 4270 4711 524 4037 2508 50 3343 2030 1151 4002 533 2994 1440 1762 3851 3050 4470 555 1979 3178 3933 3793 281 4810 520 3793 3535 2526 4422 2859 1561 1544 649 4544 2882 1236 2749 ...
output:
604316
result:
ok 1 number(s): "604316"
Test #3:
score: 0
Accepted
time: 1154ms
memory: 4476kb
input:
5000 4333 51 707 3055 3433 1451 1305 1431 3081 302 1633 88 2024 441 120 4650 3927 4970 2578 3170 4245 4204 2102 4954 3140 1039 360 3173 4203 4927 4437 4337 4502 1712 3598 2968 2 4884 1260 1768 585 1815 4346 2938 4638 4886 4482 1095 1452 298 2702 2257 1375 2819 4482 711 220 396 3907 4792 2798 4445 42...
output:
912032
result:
ok 1 number(s): "912032"
Test #4:
score: 0
Accepted
time: 972ms
memory: 4488kb
input:
5000 362 4710 4997 4405 4728 3964 4258 3568 4997 2924 2931 4997 1094 2174 2220 127 4739 260 2591 4130 4916 1614 1408 324 2924 3272 4997 4020 2924 4216 2924 2931 2924 4783 271 1101 2924 246 4953 2553 4588 2924 1770 3738 4617 2508 2486 2137 1348 4847 2632 596 1011 1442 1287 4665 2924 2203 4411 726 109...
output:
1176721
result:
ok 1 number(s): "1176721"
Test #5:
score: 0
Accepted
time: 1084ms
memory: 4460kb
input:
5000 3579 3530 3328 388 4864 4954 4597 3600 2428 1610 4533 1797 427 1296 3595 3861 4703 2914 4194 3195 451 585 3600 1134 1649 470 2049 2843 2845 3473 26 845 484 3301 1929 1342 1937 2003 1543 832 2301 2543 1889 1211 1619 1937 4471 585 4440 3600 1398 4687 2931 3982 2334 589 388 4012 873 66 2406 3861 1...
output:
1226857
result:
ok 1 number(s): "1226857"
Subtask #2:
score: 20
Accepted
Test #6:
score: 20
Accepted
time: 275ms
memory: 45256kb
input:
200000 13177 40498 104798 83659 186055 32788 86489 72123 13521 134868 158968 60124 166316 163390 120935 103000 83938 57807 97940 40447 137723 154683 191864 59080 102808 3969 21451 165592 128776 49468 4101 26441 139317 59503 18053 118809 187783 149816 21609 98521 165692 52964 60425 23437 29614 106886...
output:
5859368
result:
ok 1 number(s): "5859368"
Test #7:
score: 0
Accepted
time: 272ms
memory: 45044kb
input:
200000 161252 109349 161307 131176 54282 35989 53345 116328 52886 20845 166068 198634 185607 110703 32172 153437 50060 194586 193712 73284 32556 105087 55275 157714 22357 182552 31342 100889 145473 91759 18280 144489 108133 130988 11561 20028 121278 138065 83647 33848 33634 31990 198971 110781 12801...
output:
110388948
result:
ok 1 number(s): "110388948"
Test #8:
score: 0
Accepted
time: 249ms
memory: 45480kb
input:
200000 36915 117643 88659 78272 142053 101722 71138 149291 152470 118051 45210 31645 187500 22733 178345 55170 28768 44890 26946 76823 76271 9886 197447 130337 74747 175940 118067 191159 19884 113644 73935 160371 97288 153196 50668 47567 113533 73075 90904 115114 191694 127924 127338 41621 134964 59...
output:
469103910
result:
ok 1 number(s): "469103910"
Test #9:
score: 0
Accepted
time: 261ms
memory: 45220kb
input:
200000 3943 160214 22824 98337 873 3550 102218 67841 56961 130137 87920 154401 45794 144615 52487 75188 13477 151928 41794 147148 88519 25499 59155 187395 70572 37799 7846 166650 165689 178923 110784 68004 124416 7070 37566 126445 23236 78630 190578 145179 81517 809 99830 98383 67869 158370 182186 1...
output:
254891707
result:
ok 1 number(s): "254891707"
Test #10:
score: 0
Accepted
time: 280ms
memory: 45296kb
input:
200000 78377 9603 105868 5816 97565 17017 11229 64332 152282 115911 5141 119594 138303 67697 62645 28928 113832 166252 170769 60777 39110 85804 122988 117490 80461 169830 15334 189378 9037 191383 143689 123124 18788 113025 35138 63649 116803 33050 135937 99323 119570 44477 174794 28051 74975 174331 ...
output:
779879990
result:
ok 1 number(s): "779879990"
Test #11:
score: 0
Accepted
time: 262ms
memory: 45856kb
input:
200000 31706 198038 102731 72443 44408 116386 129202 193795 176464 175136 12293 17325 194955 2759 172903 37032 60623 73343 55344 138068 10675 29053 29280 94350 175071 73192 10795 127030 18516 28564 170635 88693 143311 110487 10208 57489 1052 33420 156977 149595 34056 171577 39262 71741 71633 61355 1...
output:
4797642624
result:
ok 1 number(s): "4797642624"
Subtask #3:
score: 0
Runtime Error
Dependency #2:
100%
Accepted
Test #12:
score: 0
Runtime Error
input:
200000 62936 42114 49454 95737 154735 83651 12241 12518 111465 87130 38023 12482 194231 193238 50051 69033 102675 40262 72917 146819 56538 159148 35426 119935 46694 63476 37721 177034 120832 10487 177187 12093 118464 95232 28721 165669 13308 116990 16648 187886 3227 181605 10993 174426 186874 45794 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%