This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 21.03.2023 16:15:05 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
inline char gc() { // like getchar()
static char buf[1 << 16];
static size_t bc, be;
if (bc >= be) {
buf[0] = 0, bc = 0;
be = fread(buf, 1, sizeof(buf), stdin);
}
return buf[bc++]; // returns 0 on EOF
}
int readInt() {
int a, c;
while ((a = gc()) < 40);
if (a == '-') return -readInt();
while ((c = gc()) >= 48) a = a * 10 + c - 480;
return a - 48;
}
// index from 0
template <class T = int>
struct Fenwick {
T *bit = nullptr;
int n;
Fenwick(int _n = 0) {
resize(_n);
}
void reset(void) {
fill(bit, bit + 1 + n, T(0));
}
void resize(int _n) {
if (bit != nullptr) delete[] bit;
n = _n;
bit = new T[n + 1];
reset();
}
void update(int i, T val) {
assert(0 <= i);
++i;
for (; i <= n; i += i & -i) bit[i] += val;
}
T get(int i) {
if (i < 0) return T(0);
++i;
i = min(i, n);
T res(0);
for (; i > 0; i -= i & -i)
res += bit[i];
return res;
}
int upper_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
int lower_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
};
// end of template
const int MAX = 1e5 + 5;
const int LOG = 20;
int N, M, Q, C[MAX];
vector <int> adj[MAX], pos[MAX];
vector <pair <int, int>> queriesAt[MAX];
void init(void) {
N = readInt(); M = readInt(); Q = readInt();
FOR(i, 1, N) {
int u = readInt(), v = readInt();
adj[u].push_back(v);
adj[v].push_back(u);
}
FORE(i, 1, M) {
C[i] = readInt();
pos[C[i]].push_back(i);
}
REP(i, Q) {
int l = readInt(), r = readInt();
queriesAt[l].emplace_back(r, i);
}
}
int sz[MAX], depth[MAX], in[MAX], tin[MAX], tout[MAX], rin[MAX], nodes[LOG][MAX + MAX], nodeCount, lca[LOG][MAX];
#define MIN_DEPTH(u, v) (depth[(u)] < depth[(v)] ? (u) : (v))
void dfs(int u) {
sz[u] = 1;
static int td = 0;
tin[u] = ++td;
rin[td] = u;
nodes[0][++nodeCount] = u;
in[u] = nodeCount;
for (int v: adj[u]) {
adj[v].erase(find(ALL(adj[v]), u));
depth[v] = depth[u] + 1;
dfs(v);
sz[u] += sz[v];
nodes[0][++nodeCount] = u;
}
tout[u] = td;
}
bool isChild(int u, int v) {
return tin[u] <= tin[v] && tin[v] <= tout[u];
}
int __lca(int u, int v) {
u = in[u]; v = in[v];
if (u > v) swap(u, v);
int h = __lg(v - u + 1);
return MIN_DEPTH(nodes[h][u], nodes[h][v - MASK(h) + 1]);
}
int res[MAX];
vector <pair <int, int>> addAt[MAX];
set <int> s;
void erase(int u) {
for (int i: pos[u]) s.erase(i);
for (int v: adj[u]) erase(v);
}
void add(int u, int cnt) {
for (int i: pos[u]) {
auto it = s.insert(i).fi;
int pre = *prev(it) + 1;
int nxt = *next(it) - 1;
addAt[pre].emplace_back(nxt, cnt);
addAt[pre].emplace_back(i - 1, -cnt);
addAt[i + 1].emplace_back(nxt, -cnt);
}
for (int v: adj[u]) add(v, cnt);
}
void dfs2(int u, int cnt, bool keep) {
int heavy = -1;
for (int v: adj[u]) if (heavy == -1 || sz[heavy] < sz[v]) heavy = v;
for (int v: adj[u]) if (v != heavy) dfs2(v, 1, false);
if (heavy != -1) dfs2(heavy, cnt + 1, true);
for (int i: pos[u]) {
auto it = s.insert(i).fi;
int pre = *prev(it) + 1;
int nxt = *next(it) - 1;
addAt[pre].emplace_back(nxt, cnt);
addAt[pre].emplace_back(i - 1, -cnt);
addAt[i + 1].emplace_back(nxt, -cnt);
}
for (int v: adj[u]) if (v != heavy) add(v, cnt);
if (!keep) erase(u);
}
int get_lca(int u, int v) {
int h = __lg(v - u + 1);
return __lca(lca[h][u], lca[h][v - MASK(h) + 1]);
}
void process(void) {
dfs(1);
FOR(k, 1, LOG) FORE(i, 1, nodeCount - MASK(k) + 1) nodes[k][i] = MIN_DEPTH(nodes[k - 1][i], nodes[k - 1][i + MASK(k - 1)]);
FORE(i, 1, M) lca[0][i] = C[i];
FOR(k, 1, LOG) FORE(i, 1, M - MASK(k) + 1) lca[k][i] = __lca(lca[k - 1][i], lca[k - 1][i + MASK(k - 1)]);
s.insert(0);
s.insert(M + 1);
dfs2(1, 1, 1);
Fenwick <int> bit(M + 1);
FORE(l, 1, M) {
for (auto [r, v]: addAt[l]) {
bit.update(l, v);
bit.update(r + 1, -v);
}
for (auto [r, i]: queriesAt[l]) res[i] = bit.get(r) - depth[get_lca(l, r)];
}
REP(i, Q) cout << res[i] << '\n';
}
int main(void) {
file("tourism");
init();
process();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
tourism.cpp: In function 'int main()':
tourism.cpp:27:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:239:2: note: in expansion of macro 'file'
239 | file("tourism");
| ^~~~
tourism.cpp:27:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:239:2: note: in expansion of macro 'file'
239 | file("tourism");
| ^~~~
# | 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... |