結果

問題 No.399 動的な領主
ユーザー hirayuu_ychirayuu_yc
提出日時 2024-05-14 21:07:22
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 14,113 bytes
コンパイル時間 6,417 ms
コンパイル使用メモリ 300,952 KB
実行使用メモリ 27,648 KB
最終ジャッジ日時 2024-05-14 21:07:32
合計ジャッジ時間 10,020 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 13 ms
6,944 KB
testcase_06 AC 160 ms
14,080 KB
testcase_07 AC 156 ms
14,336 KB
testcase_08 AC 156 ms
13,952 KB
testcase_09 AC 157 ms
14,064 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 11 ms
6,940 KB
testcase_12 AC 118 ms
14,080 KB
testcase_13 AC 115 ms
13,896 KB
testcase_14 AC 75 ms
27,648 KB
testcase_15 AC 78 ms
27,648 KB
testcase_16 AC 90 ms
21,376 KB
testcase_17 AC 158 ms
14,080 KB
testcase_18 AC 186 ms
13,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/root/AtCoder/Halc-Library/Template/Template.hpp"
#include <bits/stdc++.h>
using namespace std;

#line 8 "/root/AtCoder/Halc-Library/Template/InOut.hpp"
inline void scan() {}
inline void scan(int &a) { std::cin >> a; }
inline void scan(unsigned &a) { std::cin >> a; }
inline void scan(long &a) { std::cin >> a; }
inline void scan(long long &a) { std::cin >> a; }
inline void scan(unsigned long long &a) { std::cin >> a; }
inline void scan(char &a) { std::cin >> a; }
inline void scan(float &a) { std::cin >> a; }
inline void scan(double &a) { std::cin >> a; }
inline void scan(long double &a) { std::cin >> a; }
inline void scan(std::vector<bool> &vec) {
    for (int32_t i = 0; i < vec.size(); i++) {
        int a;
        scan(a);
        vec[i] = a;
    }
}
inline void scan(std::string &a) { std::cin >> a; }
template <class T>
inline void scan(std::vector<T> &vec);
template <class T, size_t size>
inline void scan(std::array<T, size> &vec);
template <class T, class L>
inline void scan(std::pair<T, L> &p);
template <class T, size_t size>
inline void scan(T (&vec)[size]);
template <class T>
inline void scan(std::vector<T> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(std::deque<T> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T, size_t size>
inline void scan(std::array<T, size> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T, class L>
inline void scan(std::pair<T, L> &p) {
    scan(p.first);
    scan(p.second);
}
template <class T, size_t size>
inline void scan(T (&vec)[size]) {
    for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(T &a) {
    std::cin >> a;
}
inline void in() {}
template <class Head, class... Tail>
inline void in(Head &head, Tail &...tail) {
    scan(head);
    in(tail...);
}
inline void print() { std::cout << ' '; }
inline void print(const bool &a) { std::cout << a; }
inline void print(const int &a) { std::cout << a; }
inline void print(const unsigned &a) { std::cout << a; }
inline void print(const long &a) { std::cout << a; }
inline void print(const long long &a) { std::cout << a; }
inline void print(const unsigned long long &a) { std::cout << a; }
inline void print(const char &a) { std::cout << a; }
inline void print(const char a[]) { std::cout << a; }
inline void print(const float &a) { std::cout << a; }
inline void print(const double &a) { std::cout << a; }
inline void print(const long double &a) { std::cout << a; }
inline void print(const std::string &a) {
    for (auto &&i : a) print(i);
}
template <class T>
inline void print(const std::vector<T> &vec);
template <class T, size_t size>
inline void print(const std::array<T, size> &vec);
template <class T, class L>
inline void print(const std::pair<T, L> &p);
template <class T, size_t size>
inline void print(const T (&vec)[size]);
template <class T>
inline void print(const std::vector<T> &vec) {
    if (vec.empty()) return;
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T>
inline void print(const std::deque<T> &vec) {
    if (vec.empty()) return;
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T, size_t size>
inline void print(const std::array<T, size> &vec) {
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T, class L>
inline void print(const std::pair<T, L> &p) {
    print(p.first);
    std::cout << ' ';
    print(p.second);
}
template <class T, size_t size>
inline void print(const T (&vec)[size]) {
    print(vec[0]);
    for (auto i = vec; ++i != end(vec);) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T>
inline void print(const T &a) {
    std::cout << a;
}
inline void out() { std::cout << '\n'; }
template <class T>
inline void out(const T &t) {
    print(t);
    std::cout << '\n';
}
template <class Head, class... Tail>
inline void out(const Head &head, const Tail &...tail) {
    print(head);
    std::cout << ' ';
    out(tail...);
}
inline void Yes(bool i = true) { out(i ? "Yes" : "No"); }
inline void No(bool i = true) { out(i ? "No" : "Yes"); }
struct IOsetup {
    IOsetup() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::setprecision(10);
    }
} iosetup;
#line 8 "/root/AtCoder/Halc-Library/Template/Util.hpp"
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pll = std::pair<ll, ll>;
using pii = std::pair<int, int>;
using vl = std::vector<ll>;
using vvl = std::vector<std::vector<ll>>;
using pdd = std::pair<ld, ld>;
using tuplis = std::array<ll, 3>;
template <class T>
using pq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr ll LINF = (1LL << 62) - (1LL << 31);
constexpr int32_t INF = INT_MAX >> 1;
constexpr ll MINF = 1LL << 40;
constexpr ld DINF = std::numeric_limits<ld>::infinity();
constexpr int32_t MODD = 1000000007;
constexpr int32_t MOD = 998244353;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.1415926535897932;
const ll four[] = {0, 1, 0, -1, 0};
const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
ll sum(const T &a) {
    return accumulate(std::begin(a), std::end(a), 0LL);
}
template <class T>
ld dsum(const T &a) {
    return accumulate(std::begin(a), std::end(a), 0.0L);
}
template <class T>
auto min(const T &a) {
    return *min_element(std::begin(a), std::end(a));
}
template <class T>
auto max(const T &a) {
    return *max_element(std::begin(a), std::end(a));
}
#line 1 "/root/AtCoder/Halc-Library/Template/Macro.hpp"
#define _overload3(_1, _2, _3, name, ...) name
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _rep1(i, n) for (ll i = 0; i < (n); i++)
#define _rep2(i, a, b) for (ll i = (a); i < (b); i++)
#define _rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(i, n) for (ll i = (n); i-- > 0;)
#define _rrep2(i, a, b) for (ll i = (b); i-- > (a);)
#define rrep(...) _overload3(__VA_ARGS__, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, ...) for (auto&& i : __VA_ARGS__)
#define all(i) std::begin(i), std::end(i)
#define rall(i) std::rbegin(i), std::rend(i)
#define len(x) ((int)(x).size())
#define fi first
#define se second
#define uniq(x) x.erase(unique(all(x)), std::end(x))
#define vec(type, name, ...) vector<type> name(__VA_ARGS__);
#define vv(type, name, h, ...) std::vector<std::vector<type>> name(h, std::vector<type>(__VA_ARGS__));
#define INT(...) int __VA_ARGS__; in(__VA_ARGS__)
#define LL(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ULL(...) unsigned long long __VA_ARGS__; in(__VA_ARGS__)
#define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; in(__VA_ARGS__)
#define LD(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define VEC(type, name, size) std::vector<type> name(size); in(name)
#define VV(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w)); in(name)
#line 4 "/root/AtCoder/Halc-Library/Graph/Graph.hpp"
template <class T = int32_t>
struct Edge {
    int32_t from, to;
    T cost;
    int32_t idx;
    Edge() = default;
    Edge(int32_t from, int32_t to, T cost = 1, int32_t idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int32_t() { return to; }
    void reverse() { std::swap(from, to); }
};
template <class T = int32_t>
struct Graph {
    std::vector<std::vector<Edge<T>>> gr;
    int32_t eds = 0;
    Graph() = default;
    Graph(int32_t n) { gr.resize(n); }
    void add_edge(int32_t from, int32_t to, T cost = 1, bool directed = false) {
        gr[from].emplace_back(from, to, cost, eds);
        if (!directed) {
            gr[to].emplace_back(to, from, cost, eds);
        }
        eds++;
    }
    void add_directed_edge(int32_t from, int32_t to, T cost = 1) {
        gr[from].emplace_back(from, to, cost, eds);
        eds++;
    }
    inline std::vector<Edge<T>> &operator[](const int32_t &p) { return gr[p]; }
    int32_t size() { return gr.size(); }
};
template <class T>
Graph<T> reverse_edges(Graph<T> &gr) {
    Graph<T> ret(gr.size());
    for (int32_t i = 0; i < gr.size(); i++) {
        for (Edge<T> j : gr[i]) {
            ret[j].emplace_back(j);
            ret[j].back().reverse();
        }
    }
    return ret;
}
#line 5 "/root/AtCoder/Halc-Library/Tree/HLDecomposition.hpp"

#line 7 "/root/AtCoder/Halc-Library/Tree/HLDecomposition.hpp"
struct HLDecomposition {
    struct Segment {
        int32_t lf, ri;
        bool rev;
    };
    int32_t sz;
    std::vector<int32_t> tree_sz;
    std::vector<int32_t> depth;
    std::vector<int32_t> order;
    std::vector<int32_t> path_roots;
    std::vector<int32_t> parent;
    std::vector<int32_t> out;
    template <class T>
    void _build(int32_t pos, Graph<T> &tree) {
        order[pos] = sz;
        sz++;
        int32_t mx = -1, mp = -1;
        for (int32_t i : tree[pos]) {
            if (i == parent[pos]) continue;
            if (mx < tree_sz[i]) {
                mx = tree_sz[i];
                mp = i;
            }
        }
        if (mx == -1) {
            out[pos] = sz;
            return;
        }
        path_roots[mp] = path_roots[pos];
        _build(mp, tree);
        for (int32_t i : tree[pos]) {
            if (i == parent[pos]) continue;
            if (i == mp) continue;
            path_roots[i] = i;
            _build(i, tree);
        }
        out[pos] = sz;
    }
    template <class T>
    int32_t _calc_sz(int32_t pos, Graph<T> &tree) {
        if (tree_sz[pos] != -1) return tree_sz[pos];
        tree_sz[pos] = 1;
        for (int32_t i : tree[pos]) {
            if (parent[pos] != i) {
                parent[i] = pos;
                depth[i] = depth[pos] + 1;
                tree_sz[pos] += _calc_sz(i, tree);
            }
        }
        return tree_sz[pos];
    }
    template <class T>
    HLDecomposition(Graph<T> &tree, int32_t root = 0) {
        sz = tree.size();
        tree_sz.resize(sz, -1);
        depth.resize(sz, -1);
        parent.resize(sz, -1);
        depth[root] = 0;
        _calc_sz(root, tree);
        order.resize(sz, -1);
        out.resize(sz, -1);
        path_roots.resize(sz, -1);
        sz = 0;
        path_roots[root] = root;
        _build(root, tree);
    }
    int32_t operator[](int32_t p) { return order[p]; }
    Segment subtree(int32_t pos) { return {order[pos], out[pos], false}; }
    std::vector<Segment> path(int32_t s, int32_t t) {
        std::vector<Segment> ret;
        std::stack<Segment> right;
        while (path_roots[s] != path_roots[t]) {
            if (depth[path_roots[s]] > depth[path_roots[t]]) {
                ret.emplace_back(
                    Segment{order[path_roots[s]], order[s] + 1, true});
                s = parent[path_roots[s]];
            } else {
                right.push({order[path_roots[t]], order[t] + 1, false});
                t = parent[path_roots[t]];
            }
        }
        if (depth[s] < depth[t]) {
            ret.emplace_back(Segment{order[s], order[t] + 1, false});
        } else {
            ret.emplace_back(Segment{order[t], order[s] + 1, true});
        }
        while (!right.empty()) {
            ret.push_back(right.top());
            right.pop();
        }
        return ret;
    }
    int32_t lca(int32_t s, int32_t t) {
        while (path_roots[s] != path_roots[t]) {
            if (depth[path_roots[s]] > depth[path_roots[t]]) {
                s = parent[path_roots[s]];
            } else {
                t = parent[path_roots[t]];
            }
        }
        if (depth[s] < depth[t]) return s;
        return t;
    }
};
#line 2 "/root/AtCoder/Halc-Library/DataStructure/FenwickTree.hpp"
#include <bit>
#line 5 "/root/AtCoder/Halc-Library/DataStructure/FenwickTree.hpp"
template <class T>
struct FenwickTree {
    std::vector<T> tree;
    int32_t start = 1;
    int32_t siz;
    FenwickTree(int32_t sz) {
        siz = sz;
        tree.resize(sz + 1, 0);
        start = 1 << ((32 - std::countl_zero((uint32_t)siz)) - 1);
    }
    FenwickTree(std::vector<T> def) {
        siz = def.size();
        tree.resize(siz + 1, 0);
        start = 1 << ((32 - std::countl_zero((uint32_t)siz)) - 1);
        for (int32_t i = 0; i < siz; i++) {
            tree[i + 1] += def[i];
            if (i + (i & -i) <= siz) {
                tree[i + (i & -i)] += tree[i];
            }
        }
    }
    void add(int32_t pos, T val) {
        pos++;
        while (pos <= siz) {
            tree[pos] += val;
            pos += pos & -pos;
        }
    }
    T _sum(int32_t pos) {
        T ret = 0;
        while (pos > 0) {
            ret += tree[pos];
            pos -= pos & -pos;
        }
        return ret;
    }
    T sum(int32_t lf, int32_t ri) { return _sum(ri) - _sum(lf); }
    int32_t lower_bound(T w) {
        if (w <= 0) return 0;
        int32_t now = 0;
        T val = 0;
        for (int32_t i = start; i > 0; i >>= 1) {
            if (now + i <= siz && val + tree[now + i] < w) {
                now += i;
                val += tree[now];
            }
        }
        return now + 1;
    }
    int32_t size() { return siz; }
};
#line 5 "main.cpp"
void solve() {
    LL(N);
    Graph gr(N);
    rep(i,N-1){
        LL(u,v);
        gr.add_edge(u-1,v-1);
    }
    FenwickTree<ll> fw(N+1);
    HLDecomposition hld(gr);
    LL(Q);
    rep(i,Q){
        LL(A,B);
        for(auto &[lf,ri,_]:hld.path(A-1,B-1)){
            fw.add(lf,1);
            fw.add(ri,-1);
        }
    }
    ll ans=0;
    rep(i,N){
        ll now=fw.sum(0,i+1);
        ans+=now*(now+1)/2;
    }
    out(ans);
}
int main() { solve(); }
0