結果

問題 No.399 動的な領主
ユーザー ミドリムシミドリムシ
提出日時 2021-08-11 16:39:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,213 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 206,128 KB
実行使用メモリ 33,332 KB
最終ジャッジ日時 2024-09-25 03:22:53
合計ジャッジ時間 17,840 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//#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];
    }
};

struct HLD{
    int sz;
    LazySegmentTree<lint> seg, seg_rev;
    
    vector<vector<int>> graph;
    vector<vector<int>> child;
    vector<int> size, depth, par;
    vector<int> in, head;
    
    vector<int> tour, id;
    SegmentTree<int> rmq;
    
    HLD(int n): sz(n), graph(n), child(n), size(n), depth(n), par(n), in(n), head(n), id(n) {
        auto f = [](int a, int b) { return a + b; };
        auto p = [](int a, int len) { return a * len; };
        seg = LazySegmentTree<lint>(n, f, f, f, p, 0);
        seg_rev = LazySegmentTree<lint>(n, f, f, f, p, 0);
    }
    
    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){
        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[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)
    lint query_up(int s, int t){
        lint ans = 0;
        while(depth[head[s]] > depth[t]){
            int top = head[s];
            ans += seg.query(in[top], in[s] + 1);
            s = par[top];
        }
        ans += seg.query(in[t] + 1, in[s] + 1);
        return ans;
    }
    
    // s -> t, (s, t]
    lint query_down(int s, int t){
        return query_up(t, s);
    }
    
    int lca(int a, int b){
        if(id[a] > id[b]) swap(a, b);
        return rmq.query(id[a], id[b] + 1);
    }
    
    lint get(int k){
        return seg.query(id[k], id[k + 1]);
    }
    
    // s -> t, [s, t]
    lint query(int s, int t){
        int l = lca(s, t);
        return query_up(s, l) + get(l) + query_down(l, t);
    }

    // s -> t, [s, t)
    void update_up(int s, int t, lint x){
        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, lint x){
        int l = lca(s, t);
        update_up(s, l, x);
        seg.update(in[l], in[l] + 1, x);
        update_up(t, l, x);
    }
};

int main(){
    int n;
    cin >> n;
    HLD graph(n);
    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;
}
0