This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define name "Tourism"
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define all(x) x.begin(), x.end()
#define fi(i, a, b) for(int i = a; i <= b; ++i)
#define fid(i, a, b) for(int i = a; i <= b; --i)
#define maxn (int) (1e5 + 7)
using namespace std;
int n, m, q, Time;
int a[maxn], ans[maxn];
int In[maxn], nex[maxn], siz[maxn], h[maxn], par[maxn];
vector<int> edges[maxn];
struct dl { int l, r, id; } qr[maxn];
struct BIT {
int bit[maxn];
void update(int x, int val) {
for(; x > 0; x -= (x & -x)) bit[x] += val;
}
int get(int x, int ans = 0) {
for(; x < maxn; x += (x & -x)) ans += bit[x];
return ans;
}
} Bit;
struct IT {
int st[4 * maxn];
int cb(int x, int y) { return x == y ? x : -1; }
void down(int id) {
if(st[id] != -1) st[id * 2] = st[id * 2 + 1] = st[id];
}
void update(int u, int v, int val, int id = 1, int l = 1, int r = n) {
if(l > v || r < u) return;
if(u <= l && r <= v && st[id] != -1) {
Bit.update(st[id], -(r - l + 1));
Bit.update(val, r - l + 1), st[id] = val;
return;
}
down(id);
int mid = (l + r) >> 1;
update(u, v, val, _left), update(u, v, val, _right);
st[id] = cb(st[id * 2], st[id * 2 + 1]);
}
} St;
int dfs_siz(int u) {
siz[u] = 1;
for(int &v : edges[u]) {
edges[v].erase(find(all(edges[v]), u));
siz[u] += dfs_siz(v);
if(siz[v] > siz[edges[u][0]]) swap(v, edges[u][0]);
}
return siz[u];
}
void dfs_hld(int u) {
In[u] = ++Time;
for(int v : edges[u]) {
par[v] = u, h[v] = h[u] + 1;
nex[v] = v == edges[u][0] ? nex[u] : v;
dfs_hld(v);
}
}
void update(int u, int v, int val) {
for(; nex[u] != nex[v]; u = par[nex[u]]) {
if(h[nex[u]] < h[nex[v]]) swap(u, v);
St.update(In[nex[u]], In[u], val);
}
if(h[u] < h[v]) swap(u, v);
St.update(In[v], In[u], val);
}
int Query(int x) {
int ans = Bit.get(x);
return ans == 0 ? 1 : ans;
}
void solve() {
cin >> n >> m >> q;
fi(i, 1, n - 1) {
int x, y; cin >> x >> y;
edges[x].push_back(y), edges[y].push_back(x);
}
fi(i, 1, m) cin >> a[i];
fi(i, 1, q) {
int l, r, id = i; cin >> l >> r;
qr[i] = {l, r, id};
}
nex[1] = 1;
dfs_siz(1), dfs_hld(1);
int j = 1;
sort(qr + 1, qr + 1 + q, [](dl x, dl y) { return x.r < y.r; });
fi(i, 1, q) {
while(j < qr[i].r) {
++j;
if(j > 1) update(a[j - 1], a[j], j - 1);
}
ans[qr[i].id] = Query(qr[i].l);
}
fi(i, 1, q) cout << ans[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
if(fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
solve();
}
Compilation message (stderr)
tourism.cpp: In function 'int main()':
tourism.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |