QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#598371 | #6340. Tourism | Dimash# | 0 | 33ms | 22436kb | C++17 | 4.4kb | 2024-09-28 21:30:10 | 2024-09-28 21:30:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 12, MOD = (int)1e9 + 7;
#pragma GCC optimize ("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int L = 17;
int n, m, q, tin[N], tout[N], timer, dep[N], s[N], up[N][18], ver[N * 2];
vector<int> g[N];
void dfs(int v, int pr = 1) {
tin[v] = ++timer;
s[v] = 1;
up[v][0] = pr;
ver[timer] = v;
for(int i = 1; i < L; ++i) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:g[v]) {
if(to != pr) {
dep[to] = dep[v] + 1;
dfs(to, v);
s[v] += s[to];
}
}
tout[v] = ++timer;
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
return min(u, v);
if(is(v, u)) return v;
if(is(u, v)) return u;
for(int i = L - 1; i >= 0; i--) {
if(!is(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
int c[N];
pair<int, int> t[N * 4];
void build(int v = 1, int tl = 1, int tr = m - 1) {
if(tl == tr) {
int x = lca(c[tl], c[tl + 1]);
t[v] = {dep[x], x};
} else {
int tm = (tl + tr) >> 1;
build(v + v, tl, tm);
build(v + v + 1, tm + 1, tr);
t[v] = min(t[v + v], t[v + v + 1]);
}
}
const int inf = 1e9;
pair<int, int> get(int l, int r, int v = 1, int tl = 1, int tr = m - 1) {
if(l > r || tl > r || l > tr) return {inf, inf};
if(tl >= l && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
}
struct query{
int l, r, id;
};
vector<query> v;
int res[N];
int bl = 316;
bool cmp(query x,query y){
if(x.l / bl != y.l / bl) return x.l < y.l;
return x.r < y.r;
}
multiset<int> cur;
int ans = 0;
int find(int x, int y) {
return dep[ver[y]] - dep[lca(ver[x], ver[y])];
}
void add(int v) {
v = c[v];
int t = tin[v];
if(cur.count(t)) {
cur.insert(t);
return;
}
if(cur.empty() || t < (*cur.begin())) {
if(!cur.empty()) {
ans -= dep[ver[*cur.begin()]];
ans += find(t, (*cur.begin()));
}
ans += dep[v];
cur.insert(t);
} else {
auto it = cur.upper_bound(t);
auto it1 = cur.lower_bound(t);
it1--;
ans += find(*it1, t);
// cout << ver[*it1] << ' ' << ver[t] << ' ' << find(*it1, t) << '\n';
if(it != cur.end()) {
ans += find(t, *it);
ans -= find(*it1, *it);
}
cur.insert(t);
}
}
void del(int v) {
v = c[v];
int t = tin[v];
cur.erase(cur.find(t));
if(cur.count(t)) {
return;
}
if(cur.empty() || t < (*cur.begin())) {
if(!cur.empty()) {
ans += dep[ver[*cur.begin()]];
ans -= find(t, (*cur.begin()));
}
ans -= dep[v];
} else {
auto it = cur.upper_bound(t);
auto it1 = cur.lower_bound(t);
it1--;
ans -= find(*it1, t);
// cout << ver[*it1] << ' ' << ver[t] << ' ' << find(*it1, t) << '\n';
if(it != cur.end()) {
ans -= find(t, *it);
ans += find(*it1, *it);
}
}
}
void test() {
cin >> n >> m >> q;
for(int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dep[1] = 1;
dfs(1);
for(int i = 1; i <= m; i++) {
cin >> c[i];
}
if(m > 1) build();
for(int i = 1; i <= q; i++) {
query f;
int l, r;
cin >> l >> r;
if(l == r) {
res[i] = 1;
continue;
}
f.l = l;
f.r = r;
f.id = i;
v.push_back(f);
}
sort(v.begin(), v.end(), cmp);
int l = 1, r = 0;
for(int i = 0; i < (int)v.size(); i++) {
while(l > v[i].l) add(--l);
while(r < v[i].r) add(++r);
while(r > v[i].r) del(r--);
while(l < v[i].l) del(l++);
int x = get(v[i].l, v[i].r - 1).second;
res[v[i].id] = ans - dep[x] + 1;
}
for(int i = 1; i <= q; i++) {
cout << res[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 15984kb
input:
166 249 224 158 52 52 82 158 36 117 158 119 117 5 82 158 18 22 36 82 143 105 36 22 152 36 92 117 2 123 158 5 134 119 89 31 119 92 48 105 149 149 17 108 31 134 50 3 52 63 158 3 51 42 22 17 10 103 158 50 122 92 85 50 78 117 159 36 20 143 115 158 83 20 4 142 22 23 3 96 10 19 134 8 10 151 92 65 108 89 5...
output:
0 -2 -2 3 -1 5 -2 -8 1 -1 -4 0 1 3 5 -3 0 -6 0 1 5 8 -9 7 0 10 3 -4 6 4 -5 3 -5 6 1 -4 -4 -2 2 -1 0 -3 -2 0 0 0 3 4 -2 7 -5 -4 6 9 -1 5 3 -3 2 1 0 5 -2 5 -1 -5 -12 8 5 -5 1 0 2 3 4 4 5 11 -3 -5 -6 0 1 -11 5 -6 -10 4 0 6 1 6 1 7 8 -9 -5 -8 3 -1 6 -9 -4 8 0 6 -1 6 0 -4 5 0 0 2 -2 -7 1 5 -2 -11 -2 8 -2...
result:
wrong answer 1st numbers differ - expected: '67', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #56:
score: 0
Time Limit Exceeded
input:
55321 88650 75523 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 51...
output:
result:
Subtask #4:
score: 0
Wrong Answer
Test #69:
score: 0
Wrong Answer
time: 33ms
memory: 22436kb
input:
54738 54525 1797 45211 4527 4527 43609 4527 19876 16325 43609 32183 4527 16325 32579 43609 25554 32183 38972 45211 53953 16325 19810 10881 19810 45211 12698 27967 19810 25554 46338 51894 45211 25388 16325 512 25554 43609 7820 10206 512 30021 32183 48647 43609 46338 44138 16766 7820 10023 53953 19810...
output:
-11 1 22 4 3 3 -18 -10 -8 13 -3 9 22 5 0 7 -12 4 12 21 32 3 -7 0 11 28 -17 -10 0 22 1 -3 4 -9 10 10 -2 15 15 13 -8 -6 -3 6 -3 15 20 1 17 40 20 -12 -15 12 39 14 12 18 11 2 10 14 2 5 -3 6 4 17 -5 -26 15 -3 7 8 3 -6 1 -3 5 -12 3 6 -4 1 10 14 -12 2 3 -22 4 26 7 22 9 -4 5 23 10 0 15 12 15 -17 3 -10 -5 7 ...
result:
wrong answer 1st numbers differ - expected: '276', found: '-11'
Subtask #5:
score: 0
Time Limit Exceeded
Test #102:
score: 0
Time Limit Exceeded
input:
55965 89652 95687 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%