結果

問題 No.399 動的な領主
ユーザー pitPpitP
提出日時 2023-02-11 20:44:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 721 ms / 2,000 ms
コード長 5,144 bytes
コンパイル時間 4,747 ms
コンパイル使用メモリ 272,332 KB
実行使用メモリ 25,720 KB
最終ジャッジ日時 2023-09-22 15:59:09
合計ジャッジ時間 12,297 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 45 ms
5,092 KB
testcase_06 AC 688 ms
21,300 KB
testcase_07 AC 666 ms
21,116 KB
testcase_08 AC 672 ms
21,300 KB
testcase_09 AC 692 ms
21,288 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 34 ms
5,208 KB
testcase_12 AC 507 ms
22,004 KB
testcase_13 AC 482 ms
21,896 KB
testcase_14 AC 115 ms
25,712 KB
testcase_15 AC 167 ms
25,720 KB
testcase_16 AC 289 ms
23,276 KB
testcase_17 AC 721 ms
21,448 KB
testcase_18 AC 689 ms
21,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); } 

typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const int MOD = 998244353;
const int MOD2 = 1e9+7;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};

template<typename T>
void print_2dvector(vector<vector<T>> &vec){
    int H = vec.size();
    for(int i=0;i<H;i++){
        cout << i << ' ' << '[';
        for(int j=0;j<vec[i].size();j++){
            if (j) cout << ',' << ' ';
            cout << vec[i][j];
        }
        cout << ']' << endl;
    }
}

template<typename T>
void print_vector(vector<T> &vec){
    int N = vec.size();
    cout << '[';
    for(int i=0;i<N;i++){
        if (i) cout << ',' << ' ';
        cout << vec[i];
    }
    cout << ']' << endl;
}

#ifdef LOCAL
#  include <debug_print.hpp>
#  define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define debug(...) (static_cast<void>(0))
#endif

class heavy_light_decomposition {
    const int n;
    vector<vector<int>> g;
    vector<int> in, out, size, head, par;
    int it;
    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) {
        for (auto& u : g[v]) {
            dfs1(u);
            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) {}
    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) {}
    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    void build(int rt = 0) {
        for (auto v : g[rt]) erase_par(v, rt);
        dfs1(rt);
        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]];
        }
    }
    void path_query(int u, int v, function<void(int, int)> f) {
        while (true) {
            if (in[u] > in[v]) swap(u, v);
            f(max(in[head[v]], in[u]), in[v] + 1);
            if (head[u] == head[v]) return;
            v = par[head[v]];
        }
    }
    void subtree_query(int v, function<void(int, int)> f) {
        f(in[v], out[v]);
    }
};
// https://kazuma8128.hatenablog.com/entry/2018/07/16/010500

struct S{
    long long value;
    int size;
};
using F = long long;

S op(S a, S b){ return {a.value+b.value, a.size+b.size}; }
S e(){ return {0, 0}; }
S mapping(F f, S x){ return {x.value + f*x.size, x.size}; }
F composition(F f, F g){ return f+g; }
F id(){ return 0; }

// https://atcoder.jp/contests/abc138/submissions/38777632
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int N; cin >> N;
    Graph g(N);
    lazy_segtree<S,op,e,F,mapping,composition,id> seg(N);
    rep(i,N) seg.set(i,S{0,1});
    rep(i,N-1){
        int a,b; cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    heavy_light_decomposition hld(g);
    hld.build();
    ll ans = 0ll;
    int Q; cin >> Q;
    while(Q--){
        int a,b; cin >> a >> b;
        a--;
        b--;
        hld.path_query(a,b,[&](int l, int r){ans += seg.prod(l,r).value + seg.prod(l,r).size;});
        hld.path_query(a,b,[&](int l, int r){seg.apply(l,r,1);});
        debug(ans,Q);
        for(int i=0;i<N;i++){
            debug(seg.get(hld.get_id(i)).value, i);
        }
    }
    cout << ans << endl;
}
0