結果
| 問題 | No.399 動的な領主 |
| コンテスト | |
| ユーザー |
みやべ
|
| 提出日時 | 2025-02-01 18:03:33 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 629 ms / 2,000 ms |
| コード長 | 10,415 bytes |
| コンパイル時間 | 7,464 ms |
| コンパイル使用メモリ | 429,016 KB |
| 実行使用メモリ | 72,460 KB |
| 最終ジャッジ日時 | 2025-02-01 18:03:49 |
| 合計ジャッジ時間 | 15,369 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#ifdef sys
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
using lll = __int128;
using llld = _Float128;
#define el "\n"
#define all(x) x.begin(), x.end()
#define initv2(t,a,...) a, vector<t>(__VA_ARGS__)
#define initv3(t,a,b,...) a, vector<vector<t>>(b, vector<t>(__VA_ARGS__))
#define pair(a,b) pair<a, b>
#define COUNT(a,b) count_if(a, [&](auto _Cx){return _Cx b;})
#define vec vector
#define elif else if
#define pri_q priority_queue
namespace Myb{
long long LLINF = 1010101010101010101;
int INF = 1010101010;
namespace Myb_ios{
template<typename T, typename U>
istream& operator>>(istream& ist, pair<T, U>& p) {cin >> p.first >> p.second; return ist;}
template<typename T>
istream& operator>>(istream& ist, vector<T>& v) {for(T& i : v) cin >> i; return ist;}
istream& operator>>(istream& ist, _Float128& x) {long double n; cin >> n; x = n; return ist;}
void read_d_graph(vector<vector<pair<long long, int>>>& v, int m, int num = -1){
int a, b; long long c;
for(int _ = 0; _ < m; _++){
cin >> a >> b >> c;
a += num; b += num;
v[a].emplace_back(c, b);
} return;
}
void read_d_graph(vector<vector<int>>& v, int m, int num = -1){
int a, b;
for(int _ = 0; _ < m; _++){
cin >> a >> b;
a += num; b += num;
v[a].emplace_back(b);
} return;
}
void read_ud_graph(vector<vector<pair<long long, int>>>& v, int m, int num = -1){
int a, b; long long c;
for(int _ = 0; _ < m; _++){
cin >> a >> b >> c;
a += num; b += num;
v[a].emplace_back(c, b);
v[b].emplace_back(c, a);
} return;
}
void read_ud_graph(vector<vector<int>>& v, int m, int num = -1){
int a, b;
for(int _ = 0; _ < m; _++){
cin >> a >> b;
a += num; b += num;
v[a].emplace_back(b);
v[b].emplace_back(a);
} return;
}
template<typename T>
void read_multi(T& v, int n) {}
template<typename T, typename... U>
void read_multi(T& v, int n, U&&... args){
if(n >= v.size()) read_multi(args...);
else {
cin >> v[n];
read_multi(args..., v, n+1);
}
}
string input() {string res; cin >> res; return res;}
long long inputl() {long long res; cin >> res; return res;}
template<typename T, typename U>
ostream& operator<<(ostream& ost, const pair<T, U> p) {cerr << "{"; ost << p.first << " " << p.second; cerr << "}"; return ost;}
template<typename T>
ostream& operator<<(ostream& ost, const vector<T>& v) {for(int i = 0; i < v.size(); i++) {if(i) ost << " "; ost << v[i];} return ost;}
template<typename T>
ostream& operator<<(ostream& ost, const vector<vector<T>>& v) {for(int i = 0; i < v.size(); i++) {if(i) ost << "\n"; ost << v[i];} return ost;}
} using namespace Myb_ios;
long long add_each(long long n) {return n;}
template<typename T, typename... U>
void add_each(long long n, vector<T>& v, U&... args) {
for(auto& i : v) i += n;
add_each(n, args...);
}
template<typename T, typename U> bool chmin(T& a, U b) {if(a > b){a = b; return true;} return false;}
template<typename T, typename U> bool chmax(T& a, U b) {if(a < b){a = b; return true;} return false;}
template<typename T> T minv(const vector<T>& v) {return *min_element(v.begin(), v.end());}
template<typename T> T maxv(const vector<T>& v) {return *max_element(v.begin(), v.end());}
long long power(long long val, long long num, long long mod = LLONG_MAX){
assert(mod > 0); assert(num >= 0);
val %= mod;
long long res = 1;
if(mod < INT_MAX || mod == LLONG_MAX){
while(num){
if(num&1) res = (res*val)%mod;
val = (val*val)%mod;
num >>= 1;
}
} else {
while(num){
if(num&1) res = (__int128(res)*val)%mod;
val = (__int128(val)*val)%mod;
num >>= 1;
}
}
return res;
}
long long comb(long long N, long long K, int mod = 0){
const int COMBSIZ = 200000;
assert(mod >= 0);
if(N < K || K < 0) return 0;
static vector<long long> combf(COMBSIZ+9, -1);
long long res;
if(mod != 0){
assert(N <= COMBSIZ);
if(combf[0] == -1){
combf[0] = 1;
for(long long i = 1; i <= COMBSIZ; i++) combf[i] = (combf[i-1]*i)%mod;
}
res = (combf[N]*power((combf[N-K]*combf[K])%mod, mod-2, mod))%mod;
return res;
} else {
long long a=1, b=1;
K = min(K, N-K);
for(long long i = N; i > N-K; i--) a *= i;
for(long long i = 2; i <= K; i++) b *= i;
return a/b;
}
}
} using namespace Myb;
#if __has_include(<atcoder/all>)
#include <atcoder/modint>
using namespace atcoder;
using mint9 = modint998244353;
using mint1 = modint1000000007;
ostream& operator<<(ostream& ost, const mint1& x) {ost << x.val(); return ost;}
ostream& operator<<(ostream& ost, const mint9& x) {ost << x.val(); return ost;}
ostream& operator<<(ostream& ost, const modint& x) {ost << x.val(); return ost;}
#endif
/*vvv^vvvv^vvvvv^^^^^^^^^vv^^^^^^vvvvv^^^vvvvv^^^^^^^^vvvvvvvvv^^^^^^^vvvvvvvvv^^^vv^^^vvvvvvvv^^^^vvvvvv^^vvvvvv^^^^vvv^^^vvvvvvvv^^^vv^^^^^^^vvvvvvvvv^^^^^_^^vvvvvvvv^^^^^^^^vvvv^vvvvvvvvv^^^^^^^v*/
#include <atcoder/lazysegtree>
using namespace atcoder;
template<class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct hld_tree_v{
using _HLDT_NODETYPE = lazy_segtree<S, op, e, F, mapping, composition, id>;
int n;
vector<int> dep, in, out, head, par;
_HLDT_NODETYPE node;
hld_tree_v(const vector<vector<int>>& g, const vector<S>& v, int r = 0):
n(g.size()), dep(n), in(n), out(n), head(n), par(n), node(n){
assert(n == v.size());
vector<int> sub_siz(n);
auto dfs1 = [&](auto&& self, int pos, int pre)->void {
par[pos] = pre; sub_siz[pos] = 1;
for(int nex : g[pos]){
if(nex == pre) continue;
self(self, nex, pos);
sub_siz[pos] += sub_siz[nex];
}
}; dfs1(dfs1, r, -1);
int node_siz = 0;
auto dfs2 = [&](auto self, int pos, int pre, int d, int h)->void {
dep[pos] = d; head[pos] = h; in[pos] = node_siz++;
int h_pos = -1;
for(int nex : g[pos]){
if(nex == pre) continue;
if(h_pos == -1 || sub_siz[h_pos] < sub_siz[nex]){
h_pos = nex;
}
}
if(h_pos != -1) self(self, h_pos, pos, d, h);
for(int nex : g[pos]){
if(nex == pre || nex == h_pos) continue;
self(self, nex, pos, d+1, nex);
}
out[pos] = node_siz;
}; dfs2(dfs2, r, -1, 0, r);
for(int i = 0; i < n; i++){
node.set(in[i], v[i]);
}
}
void set(int pos, const S& x){
node.set(in[pos], x);
}
S prod_path(int l, int r){
S ans = e();
while(true){
if(dep[l] < dep[r]) swap(l, r);
if(dep[l] != dep[r] || head[l] != head[r]){
ans = op(ans, node.prod(in[head[l]], in[l]+1));
l = par[head[l]];
} else {
if(in[l] > in[r]) swap(l, r);
ans = op(ans, node.prod(in[l], in[r]+1));
return ans;
}
}
}
S prod_subtree(int r){
return node.prod(in[r], out[r]);
}
int lca(int l, int r){
while(true){
if(dep[l] < dep[r]) swap(l, r);
if(dep[l] != dep[r] || head[l] != head[r]){
l = par[head[l]];
} else {
if(in[l] > in[r]) swap(l, r);
return l;
}
}
}
int dist(int l, int r){
int d = 0;
while(true){
if(dep[l] < dep[r]) swap(l, r);
if(dep[l] != dep[r] || head[l] != head[r]){
d += in[l] - in[head[l]] + 1;
l = par[head[l]];
} else {
if(in[l] > in[r]) swap(l, r);
d += in[r] - in[l];
return d;
}
}
}
//lazy_segtree
void apply_path(int l, int r, const F& x){
while(true){
if(dep[l] < dep[r]) swap(l, r);
if(dep[l] != dep[r] || head[l] != head[r]){
node.apply(in[head[l]], in[l]+1, x);
l = par[head[l]];
} else {
if(in[l] > in[r]) swap(l, r);
node.apply(in[l], in[r]+1, x);
break;
}
}
}
void apply_subtree(int r, const F& x){
node.apply(in[r], out[r], x);
}
/*_*/
};
struct S{
ll val;
int siz;
};
S op(S a, S b){return {a.val+b.val, a.siz+b.siz};}
S e(){return {0, 0};}
S mapping(ll f, S x){return {f*x.siz+x.val, x.siz};}
ll composition(ll f, ll g){return f+g;}
ll id(){return 0;}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n;
vec<S> A(n, {1, 1});
vec<vec<int>> G(n);
read_ud_graph(G, n-1);
hld_tree_v<S, op, e, ll, mapping, composition, id> tree(G, A);
ll ans = 0;
cin >> q;
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
l--; r--;
ans += tree.prod_path(l, r).val;
tree.apply_path(l, r, 1);
}
cout << ans << el;
}
みやべ