QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#872680#8616. Fast Tree Queriesucup-team055#Compile Error//C++2620.9kb2025-01-26 03:38:422025-01-26 03:38:44

Judging History

你现在查看的是最新测评结果

  • [2025-01-26 03:38:44]
  • 评测
  • [2025-01-26 03:38:42]
  • 提交

answer

#pragma GCC target("arch=icelake-server,prefer-vector-width=512")
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
namespace{
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=INFINITY;
const ld EPS=1e-9;
const ld PI=3.14159265358979323846;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload5(a,b,c,d,e,name,...) name
#define overload4(a,b,c,d,name,...) name
#define overload3(a,b,c,name,...) name
#define rep1(n) rep2(_,n)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each4(w,x,y,z,a) for(auto&&[w,x,y,z]:a)
#define each(...) overload5(__VA_ARGS__,each4,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) rbegin(i),rend(i)
#define rall2(i,a) rbegin(i),rbegin(i)+a
#define rall3(i,a,b) rbegin(i)+a,rbegin(i)+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),mint{})
#define elif else if
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector name(h,vector(w,vector<type>(__VA_ARGS__)))
template<class T> ll sz(const T& a){ return size(a); }
template<class T, class U> ll count(const T& a, const U& b){ return count(all(a), b); }
template<class T, class F> ll count_if(const T& a, F b){ return count_if(all(a), b); }
template<class T, class F> void filter(T& a, F b){ a.erase(remove_if(all(a), not_fn(b)), a.end()); }
template<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }
template<class T> void rev(T& a){ reverse(all(a)); }
template<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }
ll popcnt(ull a){ return __builtin_popcountll(a); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template<class T> bool chmin(T& a, const T& b){ return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T& a, const T& b){ return a < b ? a = b, 1 : 0; }
template<class T, class U> bool chmin(T& a, const U& b){ return a > b ? a = b, 1 : 0; }
template<class T, class U> bool chmax(T& a, const U& b){ return a < b ? a = b, 1 : 0; }
vector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ uniq(a); unordered_map<T, ll> ans; rep(i, a.size()) ans[a[i]] = i; return ans; }
template<class T> auto run_press(const T& a){ vector<pair<decay_t<decltype(a[0])>, ll>> ans; each(x, a){ if(ans.empty() || ans.back().first != x) ans.emplace_back(x, 1); else ans.back().second++; } return ans; }
template<class... Ts> void in(Ts&... t);
[[maybe_unused]] void print(){}
template<class T, class... Ts> void print(const T& t, const Ts&... ts);
template<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\n'; }
namespace IO{
#define VOID(a) decltype(void(a))
struct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }
template<class T> void o(const T& t){ o(t, P<4>{}); }
template<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }
template<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }
template<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }
template<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }
template<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }
template<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }
#undef VOID
}
#define unpack(a) (void)(0 + ... + (a, 0))
template<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }
template<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }
#undef unpack
constexpr ll debug_const(ll judge, ll debug) {
#ifdef DEBUG
    return debug;
#else
    return judge;
#endif
}
#ifdef DEBUG
ll __lg(ull x){ return 63 - __builtin_clzll(x); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
#define YESNO(yes,no) void yes(bool i = 1){ out(i?#yes:#no); } void no(){ out(#no); }
YESNO(first, second)
YESNO(First, Second)
YESNO(Yes, No)
YESNO(YES, NO)
YESNO(possible, impossible)
YESNO(Possible, Impossible)
YESNO(POSSIBLE, IMPOSSIBLE)


// #include "data_structure/csr.hpp"

namespace noya2 {

struct hld_tree {
    int n, root;
    std::vector<int> down, nxt, sub, tour;
    // noya2::internal::csr<int> childs;

    // default constructor (nop)
    hld_tree () {}

    // tree with _n node
    // after construct, call input_edges / input_parents / add_edge _n - 1 times
    hld_tree (int _n, int _root = 0) : n(_n), root(_root), down(n), nxt(n), sub(n, 1), tour(n) {
        if (n == 1){
            nxt[0] = -1;
            down[0] = -1;
            build_from_parents();
        }
    }

    // par[i] < i, par[0] == -1
    hld_tree (const std::vector<int> &par) : n(par.size()), root(0), down(n, -1), nxt(par), sub(n, 1), tour(n){
        build_from_parents();
    }

    // par[i] < i, par[0] == -1
    hld_tree (std::vector<int> &&par) : n(par.size()), root(0), down(n, -1), sub(n, 1), tour(n) {
        nxt.swap(par);
        build_from_parents();
    }

    // distinct unweighted undirected n - 1 edges of tree
    hld_tree (const std::vector<std::pair<int, int>> &es, int _root = 0) : n(es.size() + 1), root(_root), down(n), nxt(n), sub(n, 1), tour(n) {
        for (auto &[u, v] : es){
            down[u]++;
            down[v]++;
            nxt[u] ^= v;
            nxt[v] ^= u;
        }
        build_from_edges();
    }

    // input parents from cin
    template<int indexed = 1>
    void input_parents(){
        // using std::cin;
        nxt[0] = -1;
        for (int u = 1; u < n; u++){
            cin >> nxt[u];
            nxt[u] -= indexed;
        }
        build_from_parents();
    }

    // input n - 1 edges from cin
    template<int indexed = 1>
    void input_edges(){
        // using std::cin;
        for (int i = 1; i < n; i++){
            int u, v; cin >> u >> v;
            u -= indexed;
            v -= indexed;
            down[u]++;
            down[v]++;
            nxt[u] ^= v;
            nxt[v] ^= u;
        }
        build_from_edges();
    }

    void add_edge(int u, int v){
        down[u]++;
        down[v]++;
        nxt[u] ^= v;
        nxt[v] ^= u;
        // use tour[0] as counter
        if (++tour[0] == n - 1){
            build_from_edges();
        }
    }

    size_t size() const {
        return n;
    }

    // top vertex of heavy path which contains v
    int leader(int v) const {
        return nxt[v] < 0 ? v : nxt[v];
    }

    // level ancestor
    // ret is ancestor of v, dist(ret, v) == d
    // if d > depth(v), return -1
    int la(int v, int d) const {
        while (v != -1){
            int u = leader(v);
            if (down[v] - d >= down[u]){
                v = tour[down[v] - d];
                break;
            }
            d -= down[v] - down[u] + 1;
            v = (u == root ? -1 : ~nxt[u]);
        }
        return v;
    }

    // lowest common ancestor of u and v
    int lca(int u, int v) const {
        int du = down[u], dv = down[v];
        if (du > dv){
            std::swap(du, dv);
            std::swap(u, v);
        }
        if (dv < du + sub[u]){
            return u;
        }
        while (du < dv){
            v = ~nxt[leader(v)];
            dv = down[v];
        }
        return v;
    }

    // distance from u to v
    int dist(int u, int v) const {
        int _dist = 0;
        while (leader(u) != leader(v)){
            if (down[u] > down[v]) std::swap(u, v);
            _dist += down[v] - down[leader(v)] + 1;
            v = ~nxt[leader(v)];
        }
        _dist += std::abs(down[u] - down[v]);
        return _dist;
    }

    // d times move from to its neighbor (direction of to)
    // if d > dist(from, to), return -1
    int jump(int from, int to, int d) const {
        int _from = from, _to = to;
        int dist_from_lca = 0, dist_to_lca = 0;
        while (leader(_from) != leader(_to)){
            if (down[_from] > down[_to]){
                dist_from_lca += down[_from] - down[leader(_from)] + 1;
                _from = ~nxt[leader(_from)];
            }
            else {
                dist_to_lca += down[_to] - down[leader(_to)] + 1;
                _to = ~nxt[leader(_to)];
            }
        }
        if (down[_from] > down[_to]){
            dist_from_lca += down[_from] - down[_to];
        }
        else {
            dist_to_lca += down[_to] - down[_from];
        }
        if (d <= dist_from_lca){
            return la(from, d);
        }
        d -= dist_from_lca;
        if (d <= dist_to_lca){
            return la(to, dist_to_lca - d);
        }
        return -1;
    }

    // parent of v (if v is root, return -1)
    int parent(int v) const {
        if (v == root) return -1;
        return (nxt[v] < 0 ? ~nxt[v] : tour[down[v] - 1]);
    }

    // visiting time in euler tour
    // usage : seg.set(index(v), X[v])
    int index(int vertex) const {
        return down[vertex];
    }
    // usage : seg.set(index_edge(e.u, e.v), e.val)
    int index(int vertex1, int vertex2) const {
        return std::max(down[vertex1], down[vertex2]);
    }

    // subtree size of v
    int subtree_size(int v) const {
        return sub[v];
    }

    // prod in subtree v : seg.prod(subtree_l(v), subtree_r(v))
    int subtree_l(int v) const {
        return down[v];
    }
    int subtree_r(int v) const {
        return down[v] + sub[v];
    }

    // v is in subtree r
    bool is_in_subtree(int r, int v) const {
        return subtree_l(r) <= subtree_l(v) && subtree_r(v) <= subtree_r(r);
    }
    
    // distance table from s
    std::vector<int> dist_table(int s) const {
        std::vector<int> table(n, -1);
        table[s] = 0;
        while (s != root){
            table[parent(s)] = table[s] + 1;
            s = parent(s);
        }
        for (int v : tour){
            if (table[v] == -1){
                table[v] = table[parent(v)] + 1;
            }
        }
        return table;
    }

    // dist, v1, v2
    std::tuple<int, int, int> diameter() const {
        std::vector<int> dep = dist_table(root);
        int v1 = std::ranges::max_element(dep) - dep.begin();
        std::vector<int> fromv1 = dist_table(v1);
        int v2 = std::ranges::max_element(fromv1) - fromv1.begin();
        return {fromv1[v2], v1, v2};
    }

    // vertex array {from, ..., to}
    std::vector<int> path(int from, int to) const {
        int d = dist(from, to);
        std::vector<int> _path(d + 1);
        int front = 0, back = d;
        while (from != to){
            if (down[from] > down[to]){
                _path[front++] = from;
                from = parent(from);
            }
            else {
                _path[back--] = to;
                to = parent(to);
            }
        }
        _path[front] = from;
        return _path;
    }

    // path decomposition and query (vertex weighted)
    // if l < r, decsending order tour[l, r)
    // if l > r, acsending order tour(l, r]
    template<bool vertex = true>
    void path_query(int u, int v, auto f) const {
        while (leader(u) != leader(v)){
            if (down[u] < down[v]){
                f(down[leader(v)], down[v] + 1);
                v = ~nxt[leader(v)];
            }
            else {
                f(down[u] + 1, down[leader(u)]);
                u = ~nxt[leader(u)];
            }
        }
        if constexpr (vertex){
            if (down[u] < down[v]){
                f(down[u], down[v] + 1);
            }
            else {
                f(down[u] + 1, down[v]);
            }
        }
        else {
            if (down[u] != down[v]){
                f(down[u] + 1, down[v] + 1);
            }
        }
    }

    // {parent, mapping} : cptree i is correspond to tree mapping[i]. parent[i] is parent of i in cptree.
    // parent[i] < i, parent[0] == -1
    std::pair<std::vector<int>, std::vector<int>> compressed_tree(std::vector<int> vs) const {
        if (vs.empty()){
            return {{},{}};
        }
        auto comp = [&](int l, int r){
            return down[l] < down[r];
        };
        std::ranges::sort(vs, comp);
        int sz = vs.size(); vs.reserve(2*sz);
        for (int i = 0; i < sz-1; i++){
            vs.emplace_back(lca(vs[i], vs[i+1]));
        }
        std::sort(vs.begin() + sz, vs.end(), comp);
        std::ranges::inplace_merge(vs, vs.begin() + sz, comp);
        auto del = std::ranges::unique(vs);
        vs.erase(del.begin(), del.end());
        sz = vs.size();
        std::stack<int> st;
        std::vector<int> par(sz);
        par[0] = -1;
        st.push(0);
        for (int i = 1; i < sz; i++){
            while (!is_in_subtree(vs[st.top()], vs[i])) st.pop();
            par[i] = st.top();
            st.push(i);
        }
        return {par, vs};
    }
    
    auto begin() const {
        return tour.begin();
    }
    auto end() const {
        return tour.end();
    }

  private:
    // nxt[v] : parent of v, nxt[0] == -1
    void build_from_parents(){
        for (int u = n - 1; u >= 1; u--){
            int v = nxt[u];
            sub[v] += sub[u];
            down[v] = std::max(down[v], sub[u]);
        }
        for (int u = n - 1; u >= 1; u--){
            int v = nxt[u];
            if (down[v] == sub[u]){
                sub[u] = ~sub[u];
                down[v] = ~down[v];
            }
        }

        sub[0] = ~down[0] + 1;
        down[0] = 0;
        for (int u = 1; u < n; u++){
            int v = nxt[u];
            int nsub = ~down[u] + 1;
            if (sub[u] < 0){
                down[u] = down[v] + 1;
                nxt[u] = (nxt[v] < 0 ? v : nxt[v]);
            }
            else {
                down[u] = down[v] + sub[v];
                sub[v] += sub[u];
                nxt[u] = ~v;
            }
            sub[u] = nsub;
        }

        for (int u = 0; u < n; u++){
            tour[down[u]] = u;
        }
    }

    // down[v] : degree of v
    // nxt[v] : xor prod of neighbor of v
    void build_from_edges(){
        // use tour as queue
        int back = 0;
        for (int u = 0; u < n; u++){
            if (u != root && down[u] == 1){
                tour[back++] = u;
            }
        }
        for (int front = 0; front < n - 1; front++){
            int u = tour[front];
            down[u] = -1;
            int v = nxt[u]; // parent of v
            nxt[v] ^= u;
            if (--down[v] == 1 && v != root){
                tour[back++] = v;
            }
        }
        // check : now, tour is reverse of topological order

        tour.pop_back();

        // check : now, down[*] <= 1
        for (int u : tour){
            int v = nxt[u];
            // subtree size (initialized (1,1,...,1))
            sub[v] += sub[u];
            // heaviest subtree of its child
            down[v] = std::max(down[v], sub[u]);
        }
        for (int u : tour){
            int v = nxt[u];
            // whether u is not the top of heavy path
            if (down[v] == sub[u]){
                sub[u] = ~sub[u];
                down[v] = ~down[v];
            }
        }

        // after appearing v as u (or v == root),
        // down[v] is the visiting time of euler tour
        // nxt[v] is the lowest vertex of heavy path which contains v
        //   (if v itself, nxt[v] is ~(parent of v))
        // sub[v] + down[v] is the light child's starting time of euler tour
        // note : heavy child's visiting time of euler tour is (the time of its parent) + 1
        sub[root] = ~down[root] + 1;
        down[root] = 0;
        nxt[root] = -1;
        for (int u : tour | std::views::reverse){
            int v = nxt[u];
            int nsub = ~down[u] + 1;
            // heavy child
            if (sub[u] < 0){
                down[u] = down[v] + 1;
                nxt[u] = (nxt[v] < 0 ? v : nxt[v]);
            }
            // light child
            else {
                down[u] = down[v] + sub[v];
                sub[v] += sub[u];
                nxt[u] = ~v;
            }
            sub[u] = nsub;
        }

        // tour is inverse permutation of down
        tour.push_back(0);
        for (int u = 0; u < n; u++){
            tour[down[u]] = u;
        }
    }
};

} // namespace noya2
uint32_t A[debug_const(100000, 10)];
void add(ll L, ll R, uint32_t x) {
    for (ll i = L; i < R; i++) A[i] += x;
}
uint32_t get(ll L, ll R) {
    array<uint32_t, 16> XOR{};
    for (; R - L >= 16; L += 16) {
        for (ll i = 0; i < 16; i++) XOR[i] ^= (A + L)[i];
    }
    uint32_t ans = 0;
    for (ll i = 0; i < 16; i++) ans ^= XOR[i];
    for (; L < R; L++) ans ^= A[L];
    return ans;
}
void solve(){
    LL(n,q);
    noya2::hld_tree g(n);
    rep(n-1){
        LL(a,b);
        g.add_edge(--a, --b);
    }
    rep(i,n) g.path_query(i, i, [&](int l, int r) {
        if (l > r) swap(l, r);
        add(l, r, i+1);
    });
    rep(q){
        CHR(c);
        if(c=='+'){
            LL(a,v,x);
            a--; v--;
            g.path_query(a, v, [&](int l, int r) {
                if (l > r) swap(l, r);
                add(l, r, x);
            });
        }else{
            LL(a,v);
            a--; v--;
            uint32_t ans = 0;
            g.path_query(a, v, [&](int l, int r) {
                if (l > r) swap(l, r);
                ans ^= get(l, r);
            });
            out(ans);
        }
    }
}
}
int main(){
    ll t = 1;
    // in(t); // マルチテストケースかどうか確認!
    rep(t){
        solve();
    }
}

Details

In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from answer.code:3:
/usr/include/c++/14/bits/allocator.h: In destructor ‘constexpr std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()’:
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to ‘always_inline’ ‘constexpr std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = int]’: target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/stl_vector.h:132:14: note: called from here
  132 |       struct _Vector_impl
      |              ^~~~~~~~~~~~