QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#80685 | #5403. 树数术 | Scintilla | 100 ✓ | 950ms | 255580kb | C++14 | 3.3kb | 2023-02-24 19:58:47 | 2023-02-24 19:58:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl
using ll = long long;
const int N = 2e6 + 10;
int read() {
int x = 0, f = 1; char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
return x * f;
}
struct qry { int l, r, id; } ;
vector <int> G[N];
vector <qry> Q[N]; ll ans[N];
int n, m, q, z, u, v, ql, qr, tp, a[N], st[N], sz[N], fa[N], top[N], son[N], dep[N];
void add (int u, int v) {
G[u].push_back(v), G[v].push_back(u);
}
void dfs1 (int u, int F) {
sz[u] = 1, dep[u] = dep[F] + 1, fa[u] = F;
for (auto v : G[u]) if(v != F) {
dfs1(v, u), sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2 (int u, int topf) {
top[u] = topf;
if(son[u]) dfs2(son[u], topf);
for (auto v : G[u]) if(v != fa[u] && v != son[u]) dfs2(v, v);
}
int LCA (int x, int y) {
if(!x || !y) return x + y;
for ( ; top[x] != top[y]; x = fa[top[x]])
if(dep[top[x]] < dep[top[y]]) swap(x, y);
return dep[x] < dep[y] ? x : y;
}
namespace FT {
int c[N];
void add (int p, int k) {
for (int i = p; i <= m; i += (i & (-i)))
c[i] += k;
}
int qry (int p) {
int ans = 0;
for (int i = p; i; i -= (i & (-i)))
ans += c[i];
return ans;
}
int query (int l, int r) {
return l > r ? 0 : qry(r) - qry(l - 1);
}
}
namespace ST1 {
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid ((l + r) >> 1)
int t[N << 2];
void build (int p, int l, int r) {
if(l == r) return t[p] = a[l], void();
build(ls, l, mid), build(rs, mid + 1, r);
t[p] = LCA(t[ls], t[rs]);
}
int query (int p, int l, int r, int x, int y) {
if(l >= x && r <= y) return t[p];
if(mid < x) return query(rs, mid + 1, r, x, y);
if(mid >= y) return query(ls, l, mid, x, y);
return LCA(query(ls, l, mid, x, y), query(rs, mid + 1, r, x, y));
}
}
int calc (int n, int v) {
int l = 1, r = n;
while (l < r) {
int Mid = (l + r + 1) / 2;
if(dep[a[st[Mid]]] <= dep[v]) l = Mid;
else r = Mid - 1;
}
if(r && dep[a[st[r]]] > dep[v]) --r;
return !r ? m + 1 : st[r];
}
int main () {
n = read(), m = read(), q = read();
rep(i, 1, n - 1) u = read(), v = read(), add(u, v);
dfs1(1, 0), dfs2(1, 1);
rep(i, 1, m) a[i] = read();
ST1 :: build(1, 1, m);
rep(i, 1, q) {
z = read(); int anc = 0;
rep(j, 1, z) {
ql = read(), qr = read();
Q[ql].push_back((qry){anc, qr, i});
anc = LCA(anc, ST1 :: query(1, 1, m, ql, qr));
}
}
drep(i, m, 1) {
int l = LCA(a[st[tp]], a[i]);
for ( ; tp && dep[a[st[tp]]] > dep[l]; --tp) FT :: add(st[tp], -1);
FT :: add(i, 1), st[++tp] = i;
for (auto j : Q[i]) {
l = LCA(a[st[tp]], j.l);
int pos = calc(tp, l);
ans[j.id] += FT :: query(pos, j.r);
}
}
rep(i, 1, q) printf("%lld\n", ans[i]);
return 0;
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 917ms
memory: 255580kb
input:
699990 699995 233333 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50...
output:
121987 139520 87432 42773 311773 194269 177810 454211 417130 299450 366103 227736 186417 163975 292497 216139 401719 16634 192685 27351 52127 54938 142169 275076 339876 117003 51132 229206 187647 350043 398455 83905 100490 338070 356664 60211 366172 142627 16903 222331 471095 184145 170758 238393 14...
result:
ok 233333 lines
Subtask #2:
score: 20
Accepted
Test #2:
score: 20
Accepted
time: 950ms
memory: 211936kb
input:
699992 699994 699992 2 1 3 1 4 3 5 3 6 5 7 5 8 7 9 7 10 9 11 9 12 11 13 11 14 13 15 13 16 15 17 15 18 17 19 17 20 19 21 19 22 21 23 21 24 23 25 23 26 25 27 25 28 27 29 27 30 29 31 29 32 31 33 31 34 33 35 33 36 35 37 35 38 37 39 37 40 39 41 39 42 41 43 40 44 43 45 43 46 45 47 45 48 47 49 47 50 49 51 ...
output:
211594 160846 176729 128251 32662 70710 74884 9095 68282 91324 154262 24279 31878 173468 75265 139715 91660 87525 194302 16875 81535 172911 29145 20483 151883 5255 9548 58890 38076 94152 14358 74859 48870 23981 41390 60976 13795 125823 427 26641 130620 174231 73241 55291 2364 78864 23391 4866 36002 ...
result:
ok 699992 lines
Subtask #3:
score: 30
Accepted
Test #3:
score: 30
Accepted
time: 98ms
memory: 116244kb
input:
99998 99993 33333 2 1 3 1 4 3 5 3 6 5 7 5 8 7 9 7 10 9 11 9 12 11 13 11 14 13 15 13 16 15 17 15 18 17 19 17 20 19 21 19 22 21 23 21 24 23 25 23 26 25 27 25 28 27 29 27 30 29 31 24 32 31 33 31 34 33 35 33 36 35 37 35 38 37 39 37 40 39 41 39 42 41 43 41 44 43 45 43 46 45 47 45 48 47 49 47 50 49 51 49 ...
output:
9733 11330 8403 5136 22207 23231 12686 27288 23739 20937 18153 16379 8991 14036 11669 14685 20272 18955 21680 7927 21383 23576 14834 5714 15707 10013 7905 13254 13883 10446 16140 16796 11009 11912 15761 20419 11157 12192 14327 18260 19095 5239 4114 16522 7412 5005 16844 8747 19377 22600 12023 9161 9...
result:
ok 33333 lines
Subtask #4:
score: 40
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #4:
score: 40
Accepted
time: 877ms
memory: 207776kb
input:
699991 700000 233333 2 1 3 1 4 3 5 3 6 5 7 5 8 7 9 7 10 9 11 9 12 11 13 11 14 13 15 13 16 15 17 15 18 17 19 17 20 19 21 19 22 21 23 21 24 23 25 23 26 25 27 25 28 27 29 27 30 29 31 29 32 31 33 31 34 33 35 33 36 35 37 35 38 37 39 37 40 39 41 39 42 41 43 41 44 43 45 43 46 45 47 45 48 47 49 47 50 49 51 ...
output:
129494 81962 52243 146978 53574 96058 72866 34286 184144 65111 77323 90610 168465 55995 66592 60741 118767 119126 150909 50321 114540 119576 122486 84546 19256 4585 22720 133448 124662 148500 173817 100034 135869 88299 68523 62880 73171 139874 90190 100601 100854 137709 106869 126901 40873 93249 136...
result:
ok 233333 lines