結果
| 問題 |
No.399 動的な領主
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-11 19:30:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,645 ms / 2,000 ms |
| コード長 | 10,574 bytes |
| コンパイル時間 | 2,930 ms |
| コンパイル使用メモリ | 206,372 KB |
| 実行使用メモリ | 29,192 KB |
| 最終ジャッジ日時 | 2024-09-25 06:01:15 |
| 合計ジャッジ時間 | 17,499 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountll((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzll(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
#define mp(x, y) make_pair(x, y)
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int x, y; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class itr> void array_output(itr start, itr goal){ for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); cout << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }
lint inv2 = power(2, mod - 2, mod);
struct io_init {
io_init() {
cin.tie(nullptr); cout.tie(nullptr);
std::ios::sync_with_stdio(false);
}
} io_init;
template<class T, class Op = T>
struct LazySegmentTree{
using F = function<T(T, T)>;
using G = function<T(T, Op)>;
using H = function<Op(Op, Op)>;
using P = function<Op(Op, int)>;
int sz;
vector<T> data;
vector<Op> lazy;
vector<bool> is_lazy;
F f;
G g;
H h;
P p;
T id;
LazySegmentTree() { }
LazySegmentTree(int n, F f, G g, H h, P p, T id): f(f), g(g), h(h), p(p), id(id) {
sz = 1;
while(sz < n) sz *= 2;
data.resize(sz * 2, id);
lazy.resize(sz * 2);
is_lazy.resize(sz * 2, false);
}
void set(int k, T x){
data[k + sz] = x;
}
void build(){
repr2(i, 1, sz){
data[i] = f(data[i * 2], data[i * 2 + 1]);
}
}
void eval(int l, int r, int at){
if(is_lazy[at]){
data[at] = g(data[at], p(lazy[at], r - l));
if(r - l > 1){
lazy[at * 2] = (is_lazy[at * 2] ? h(lazy[at * 2], lazy[at]) : lazy[at]);
lazy[at * 2 + 1] = (is_lazy[at * 2 + 1] ? h(lazy[at * 2 + 1], lazy[at]) : lazy[at]);
is_lazy[at * 2] = is_lazy[at * 2 + 1] = true;
}
is_lazy[at] = false;
}
}
void update(int a, int b, Op x, int l, int r, int at){
eval(l, r, at);
if(r <= a || b <= l){
return;
}else if(a <= l && r <= b){
lazy[at] = (is_lazy[at] ? h(lazy[at], x) : x);
is_lazy[at] = true;
eval(l, r, at);
return;
}
update(a, b, x, l, (l + r) / 2, at * 2);
update(a, b, x, (l + r) / 2, r, at * 2 + 1);
data[at] = f(data[at * 2], data[at * 2 + 1]);
}
void update(int a, int b, Op x){
update(a, b, x, 0, sz, 1);
}
T query(int a, int b, int l, int r, int at){
eval(l, r, at);
if(r <= a || b <= l){
return id;
}else if(a <= l && r <= b){
return data[at];
}
return f(query(a, b, l, (l + r) / 2, at * 2), query(a, b, (l + r) / 2, r, at * 2 + 1));
}
T query(int a, int b){
return query(a, b, 0, sz, 1);
}
};
template<class T>
struct SegmentTree{
using F = function<T(T, T)>;
int sz;
vector<T> data;
F f;
T id;
SegmentTree(): sz(1), f([](T a, T b){ return a + b; }), data(2, id) {}
SegmentTree(int n, F f, T id): f(f), id(id) {
sz = 1;
while(sz < n) sz *= 2;
data.resize(sz * 2, id);
}
void update(int k, T x){
k += sz;
data[k] = x;
while(k > 1){
k /= 2;
data[k] = f(data[k * 2], data[k * 2 + 1]);
}
}
T query(int a, int b, int l, int r, int at){
if(a <= l && r <= b){
return data[at];
}else if(r <= a || b <= l){
return id;
}
return f(query(a, b, l, (l + r) / 2, at * 2), query(a, b, (l + r) / 2, r, at * 2 + 1));
}
T query(int a, int b){
return query(a, b, 0, sz, 1);
}
T operator [](int k){
return data[k + sz];
}
};
template<class T, class Op = T>
struct LazyHLDCommutative{
using F = function<T(T, T)>;
using G = function<T(T, Op)>;
using H = function<Op(Op, Op)>;
using P = function<Op(Op, int)>;
F f;
G g;
H h;
P p;
T id;
int sz;
LazySegmentTree<lint> seg;
vector<vector<int>> graph;
vector<vector<int>> child;
vector<int> size, depth, par;
vector<int> in, head;
vector<int> tour, tour_id;
SegmentTree<int> rmq;
LazyHLDCommutative(int n, F f, G g, H h, P p, T id): sz(n), f(f), g(g), h(h), p(p), id(id), graph(n), child(n), size(n), depth(n), par(n), in(n), head(n), tour_id(n) {
seg = LazySegmentTree<T>(n, f, g, h, p, id);
}
void add_edge(int a, int b){
graph[a].push_back(b);
graph[b].push_back(a);
}
void dfs_sz(int at, int back){
size[at] = 1;
par[at] = back;
for(int to: graph[at]){
if(to == back) continue;
depth[to] = depth[at] + 1;
dfs_sz(to, at);
child[at].push_back(to);
size[at] += size[to];
}
}
void dfs_hl(int at, int par, int &num){
in[at] = num;
num++;
if(child[at].empty()) return;
for(int &c: child[at]){
if(size[c] > size[child[at][0]]){
swap(c, child[at][0]);
}
}
int heavy = child[at][0];
for(int c: child[at]){
if(c == heavy){
head[c] = head[at];
}else{
head[c] = c;
}
dfs_hl(c, at, num);
}
}
void dfs_lca(int at, int back){
tour_id[at] = int(tour.size());
tour.push_back(at);
for(int to: graph[at]){
if(to == back) continue;
dfs_lca(to, at);
tour.push_back(at);
}
}
void build(){
depth.resize(sz);
depth[0] = 0;
dfs_sz(0, -1);
head[0] = 0;
int tmp = 0;
dfs_hl(0, -1, tmp);
tour.clear();
dfs_lca(0, -1);
depth.push_back(INT_MAX);
auto f = [&](int a, int b){ if(depth[a] < depth[b]) return a; else return b; };
rmq = SegmentTree<int>(int(tour.size()), f, sz);
rep(i, tour.size()){
rmq.update(i, tour[i]);
}
}
// s -> t, [s, t)
T query_up(int s, int t){
if(s == t) return id;
T ans = id;
while(depth[head[s]] > depth[t]){
int top = head[s];
ans = f(ans, seg.query(in[top], in[s] + 1));
s = par[top];
}
ans = f(ans, seg.query(in[t] + 1, in[s] + 1));
return ans;
}
// s -> t, (s, t]
T query_down(int s, int t){
return query_up(t, s);
}
int lca(int a, int b){
if(tour_id[a] > tour_id[b]) swap(a, b);
return rmq.query(tour_id[a], tour_id[b] + 1);
}
// s -> t, [s, t]
T query(int s, int t){
int l = lca(s, t);
return f(f(query_up(s, l), seg.query(in[l], in[l] + 1)), query_down(l, t));
}
// s -> t, [s, t)
void update_up(int s, int t, T x){
if(s == t) return;
while(depth[head[s]] > depth[t]){
int top = head[s];
seg.update(in[top], in[s] + 1, x);
s = par[top];
}
seg.update(in[t] + 1, in[s] + 1, x);
}
// [s, t]
void update(int s, int t, T x){
int l = lca(s, t);
update_up(s, l, x);
// cout << l << endl;
seg.update(in[l], in[l] + 1, x);
update_up(t, l, x);
}
};
int main(){
int n;
cin >> n;
auto f = [](lint a, lint b){ return a + b; };
auto p = [](lint a, int len){ return a * len; };
LazyHLDCommutative<lint> graph(n, f, f, f, p, 0);
rep(i, n - 1){
int a, b;
cin >> a >> b;
a--, b--;
graph.add_edge(a, b);
}
graph.build();
rep(i, n){
graph.update(i, i, 1);
}
int q;
cin >> q;
lint ans = 0;
rep(_, q){
int a, b;
cin >> a >> b;
a--, b--;
ans += graph.query(a, b);
graph.update(a, b, 1);
}
cout << ans << endl;
}