結果
| 問題 |
No.399 動的な領主
|
| コンテスト | |
| ユーザー |
akusyounin2412
|
| 提出日時 | 2018-11-21 16:19:35 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 624 ms / 2,000 ms |
| コード長 | 9,000 bytes |
| コンパイル時間 | 2,515 ms |
| コンパイル使用メモリ | 148,344 KB |
| 実行使用メモリ | 24,576 KB |
| 最終ジャッジ日時 | 2024-12-24 16:02:15 |
| 合計ジャッジ時間 | 9,246 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourth; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans , d[3000][3000], a[400000], b[400000];
string str, ss;
bool f;
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<int>>vec;
vector<edge>ed;
vector<LL>v;
map<Pll, LL>ma;
set<LL>st;
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
struct HLD {
LL n, pos;
vector<vector<LL> > G;
vector<LL> vid, head, sub, hvy, par, dep, inv, type;
HLD() {}
HLD(LL sz) :
n(sz), pos(0), G(n),
vid(n, -1), head(n), sub(n, 1), hvy(n, -1),
par(n), dep(n), inv(n), type(n) {}
void add_edge(LL u, LL v) {
G[u].push_back(v);
G[v].push_back(u);
}
void build(vector<LL> rs = vector<LL>(1, 0)) {
LL c = 0;
for (LL r : rs) {
dfs(r);
bfs(r, c++);
}
}
void dfs(LL rt) {
using T = pair<LL, LL>;
stack<T> st;
par[rt] = -1;
dep[rt] = 0;
st.emplace(rt, 0);
while (!st.empty()) {
LL v = st.top().first;
LL &i = st.top().second;
if (i<(LL)G[v].size()) {
LL u = G[v][i++];
if (u == par[v]) continue;
par[u] = v;
dep[u] = dep[v] + 1;
st.emplace(u, 0);
}
else {
st.pop();
LL res = 0;
for (LL u : G[v]) {
if (u == par[v]) continue;
sub[v] += sub[u];
if (res<sub[u]) res = sub[u], hvy[v] = u;
}
}
}
}
void bfs(LL r, LL c) {
LL &k = pos;
queue<LL> q({ r });
while (!q.empty()) {
LL h = q.front(); q.pop();
for (LL i = h; i != -1; i = hvy[i]) {
type[i] = c;
vid[i] = k++;
inv[vid[i]] = i;
head[i] = h;
for (LL j : G[i])
if (j != par[i] && j != hvy[i]) q.push(j);
}
}
}
// for_each(vertex)
// [l,r] <- attention!!
void for_each(LL u, LL v, LL x, const function<void(LL, LL)>& f) {
while (1) {
if (vid[u] > vid[v]) swap(u, v);
f(max(vid[head[v]], vid[u]), vid[v]);
if (head[u] != head[v]) v = par[head[v]];
else break;
}
}
// for_each(edge)
// [l,r] <- attention!!
void for_each_edge(LL u, LL v, const function<void(LL, LL)>& f) {
while (1) {
if (vid[u]>vid[v]) swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
}
else {
if (u != v) f(vid[u] + 1, vid[v]);
break;
}
}
}
LL lca(LL u, LL v) {
while (1) {
if (vid[u]>vid[v]) swap(u, v);
if (head[u] == head[v]) return u;
v = par[head[v]];
}
}
LL distance(LL u, LL v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
};
struct Seg {
LL n;
vector<LL>node, lazy;
Seg(vector<LL>v) {
n = 1; while (n < v.size())n *= 2;
node.resize(n * 2 - 1);
lazy.resize(n * 2 - 1);
rep(i, n)node[n + i - 1] = v[i];
Rrep(i, n - 2)node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
//rep(i, n * 2 - 1)lazy[i] = -1;
}
Seg(LL n_) {
n = 1; while (n < n_)n *= 2;
node.resize(n * 2 - 1);
lazy.resize(n * 2 - 1);
rep(i, n)node[n + i - 1] = 0;
Rrep(i, n - 2)node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
//区間最小などの時
//rep(i, n * 2 - 1)lazy[i] = -1;
}
//区間の和の遅延
void eval(LL k, LL l, LL r) {
if (lazy[k] != 0) {
node[k] += lazy[k];
if (r - l >= 1) {
lazy[k * 2 + 1] += lazy[k] / 2;
lazy[k * 2 + 2] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
//区間の最小の遅延
void ceval(LL k, LL l, LL r) {
if (lazy[k] != -1) {
node[k] = lazy[k];
if (r - l >= 1) {
lazy[k * 2 + 1] = lazy[k];
lazy[k * 2 + 2] = lazy[k];
}
lazy[k] = -1;
}
}
void update(LL i, LL x) {
i = i + n - 1;
node[i] = x;
while (i > 0) {
i = (i - 1) / 2;
node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
}
}
void update(LL i, LL x, LL a, LL b, LL l, LL r) {
//ceval(i, a, b);
if (b < l || r < a)return;
if (l <= a && b <= r) {
lazy[i] = x;
ceval(i, a, b);
}
else {
update(i * 2 + 1, x, a, (a + b) / 2, l, r);
update(i * 2 + 2, x, (a + b + 1) / 2, b, l, r);
node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
}
}
LL find(LL i, LL a, LL b, LL l, LL r) {
ceval(i, a, b);
if (b < l || r < a)return INF;
if (l <= a && b <= r)return node[i];
return min(find(i * 2 + 1, a, (a + b) / 2, l, r), find(i * 2 + 2, (a + b + 1) / 2, b, l, r));
}
void add(LL i, LL x) {
i = i + n - 1;
node[i] += x;
while (i > 0) {
i = (i - 1) / 2;
node[i] = (node[i * 2 + 1] + node[i * 2 + 2]);
}
}
void add(LL i, LL x, LL a, LL b, LL l, LL r) {
eval(i, a, b);
if (b < l || r < a)return;
if (l <= a && b <= r) {
lazy[i] += (b - a + 1)*x;
eval(i, a, b);
}
else {
add(i * 2 + 1, x, a, (a + b) / 2, l, r);
add(i * 2 + 2, x, (a + b + 1) / 2, b, l, r);
node[i] = node[i * 2 + 1] + node[i * 2 + 2];
}
}
LL getsum(LL i, LL a, LL b, LL l, LL r) {
eval(i, a, b);
if (b < l || r < a)return 0;
if (l <= a && b <= r)return node[i];
return (getsum(i * 2 + 1, a, (a + b) / 2, l, r) + getsum(i * 2 + 2, (a + b + 1) / 2, b, l, r));
}
};
Seg seg(200000);
class heavy_light_decomposition {
const int n;
vector<vector<int>> g;
vector<int> in, out, size, head, par,dep,type;
int it,type_size=0;
void erase_par(int v, int prev) {
par[v] = prev;
for (auto& u : g[v]) {
if (u == g[v].back()) break;
if (u == prev) swap(u, g[v].back());
erase_par(u, v);
}
g[v].pop_back();
}
void dfs1(int v,int d) {
dep[v] = d;
for (auto& u : g[v]) {
type[u] = type[v];
dfs1(u,d+1);
size[v] += size[u];
if (size[u] > size[g[v][0]]) swap(u, g[v][0]);
}
}
void dfs2(int v) {
in[v] = it++;
for (auto u : g[v]) {
head[u] = (u == g[v][0] ? head[v] : u);
dfs2(u);
}
out[v] = it;
}
public:
heavy_light_decomposition(int n_)
: n(n_), g(n), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), it(0),dep(n,-1),type(n,-1) {}
heavy_light_decomposition(const vector<vector<int>>& G)
: n(G.size()), g(G), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), it(0),dep(n,-1),type(n,-1) {}
void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void build() {
for (int i = 0; i < n; i++) {
if (dep[i] == -1) {
dep[i] = 0;
type[i] = type_size;
type_size++;
int rt = i;
for (auto v : g[rt]) erase_par(v, rt);
dfs1(rt, 0);
head[rt] = rt;
dfs2(rt);
}
}
}
int get_id(int v) {
return in[v];
}
int get_lca(int u, int v) {
while (true) {
if (in[u] > in[v]) swap(u, v);
if (head[u] == head[v]) return u;
v = par[head[v]];
}
}
// [l,r)
void path_query(int u, int v, function<void(int, int)> f) {
while (true) {
if (in[u] > in[v]) swap(u, v);
seg.add(0, 1, 0, seg.n - 1, max(in[head[v]], in[u]), in[v]);
ans += seg.getsum(0, 0, seg.n - 1, max(in[head[v]], in[u]), in[v]);
//f(max(in[head[v]], in[u]), in[v] + 1);
if (head[u] == head[v]) return;
v = par[head[v]];
}
}
// [l,r)
void edge_query(int u, int v, const function<void(LL, LL)>& f) {
while (1) {
if (in[u]>in[v]) swap(u, v);
if (head[u] != head[v]) {
f(in[head[v]], in[v]+1);
v = par[head[v]];
}
else {
if (u != v) f(in[u] + 1, in[v]+1);
break;
}
}
}
// [l,r)
void subtree_query(int v, function<void(int, int)> f) {
f(in[v], out[v]);
}
int distance(int u, int v) {
return dep[u] + dep[v] - 2 * dep[get_lca(u, v)];
}
};
int main() {
cin >> n;
heavy_light_decomposition hld(n);
rep(i, n - 1) {
LL x, y;
cin >> x >> y;
hld.add_edge(x - 1, y - 1);
}
hld.build();
cin >> q;
rep(i, q) {
int x, y;
cin >> x >> y;
hld.path_query(x - 1, y - 1, (function<void(LL, LL)>)[](int x, int y) {});
}
cout << ans << endl;
return 0;
}
akusyounin2412