QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204669#7563. Fun on Treeucup-team1631#TL 0ms3900kbC++2012.2kb2023-10-07 13:38:102023-10-07 13:38:11

Judging History

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

  • [2023-10-07 13:38:11]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3900kb
  • [2023-10-07 13:38:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define elif else if
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int,int>


#define repname(a, b, c, d, e, ...) e
#define rep(...)                    repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x)                     for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x)                  for (int i = 0; i < (x); ++i)
#define rep2(i, l, r)               for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c)            for (int i = (l); i < (r); i += (c))





struct ScalarInput {
    template<class T>
    operator T(){
        T ret;
        cin >> ret;
        return ret;
    }
};
struct VectorInput {
    size_t n;
    VectorInput(size_t n): n(n) {}
    template<class T>
    operator vector<T>(){
        vector<T> ret(n);
        for(T &x : ret) cin >> x;
        return ret;
    }
};
ScalarInput input(){ return ScalarInput(); }
VectorInput input(size_t n){ return VectorInput(n); }

template<typename T>
void print(vector<T> a){
  for(int i=0;i<a.size();i++){
    cout<<a[i]<<" \n"[i+1==a.size()];
  }
}

template<class T>
void print(T x){
    cout << x << '\n';
}
 
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail){
  cout << head << ' ';
  print(forward<Tail>(tail)...);
}

class LCA {
public:
    LCA() = default;
    LCA(const std::vector<std::vector<int>>& G, int root) : G(G), LOG(32 - __builtin_clz(G.size())), depth(G.size()) {
        int V = G.size();
        table.assign(LOG, std::vector<int>(V, -1));

        dfs(root, -1, 0);

        for (int k = 0; k < LOG - 1; ++k) {
            for (int v = 0; v < V; ++v) {
                if (table[k][v] >= 0) {
                    table[k + 1][v] = table[k][table[k][v]];
                }
            }
        }
    }

    int query(int u, int v) const {
        if (depth[u] > depth[v]) std::swap(u, v);

        // go up to the same depth
        for (int k = 0; k < LOG; ++k) {
            if ((depth[v] - depth[u]) >> k & 1) {
                v = table[k][v];
            }
        }
        if (u == v) return u;

        for (int k = LOG - 1; k >= 0; --k) {
            if (table[k][u] != table[k][v]) {
                u = table[k][u];
                v = table[k][v];
            }
        }
        return table[0][u];
    }

    int dist(int u, int v) const {
        return depth[u] + depth[v] - 2 * depth[query(u, v)];
    }

    int parent(int v, int k) const {
        for (int i = LOG - 1; i >= 0; --i) {
            if (k >= (1 << i)) {
                v = table[i][v];
                k -= 1 << i;
            }
        }
        return v;
    }

    int jump(int u, int v, int k) const {
        int l = query(u, v);
        int du = depth[u] - depth[l];
        int dv = depth[v] - depth[l];
        if (du + dv < k) return -1;
        if (k < du) return parent(u, k);
        return parent(v, du + dv - k);
    }


protected:
    const std::vector<std::vector<int>>& G;
    const int LOG;
    std::vector<std::vector<int>> table;
    std::vector<int> depth;

    void dfs(int v, int p, int d) {
        table[0][v] = p;
        depth[v] = d;
        for (int c : G[v]) {
            if (c != p) dfs(c, v, d + 1);
        }
    }
};


using S=pair<ll,int>;
S op(S a,S b){
  return min(a,b);
}
ll inf=(ll)1<<60;
S e(){
  return {inf,0};
}
S mapping(ll f,S x){
  return {x.first+f,x.second};
}
ll composition(ll f,ll g){
  return f+g;
}
ll id(){
  return (ll)0;
}


#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder

using namespace atcoder;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n,q;
  cin>>n>>q;
  vll a=input(n);
  vector<vector<pair<int,ll>>>child(n);
  vector<pair<int,ll>>par(n,{-1,-1});
  vector<vector<int>>edge(n);
  rep(i,1,n){
    int p=input();
    ll w=input();
    p--;
    par[i]={p,w};
    child[p].push_back({i,w});
    edge[i].push_back(p);
    edge[p].push_back(i);
  }

  vll depth(n,(ll)0);
  vi todo;
  todo.push_back(~0);
  todo.push_back(0);
  vi visited;
  vi eular_order(n,0);
  vi eular_out(n,0);
  vector<S> seg_init(n,e());
  int tmp=0;
  while(!todo.empty()){
    int v=todo.back();
    todo.pop_back();
    if(v>=0){
      visited.push_back(v);
      eular_order[v]=tmp;
      seg_init[tmp]={a[v]-depth[v],v};
      tmp+=1;
      for(auto [u,w]:child[v]){
        depth[u]=depth[v]+w;
        todo.push_back(~u);
        todo.push_back(u);
      }
    }
    else{
      v=~v;
      eular_out[v]=tmp;
    }
  }

  lazy_segtree<S,op,e,ll,mapping,composition,id>seg(seg_init);
  int M=1;
  while(M*M<=q)M+=1;
  int bucket_len=(q/M)+1;
  vector<vector<pii>>bucket(bucket_len);
  vector<pair<pair<int,int>,ll>>queries;
  rep(i,q){
    int x=input();
    int y=input();
    ll w=input();
    x--;
    y--;
    bucket[i/M].push_back({i,eular_order[x]});
    queries.push_back({{x,y},w});
  }

  rep(i,bucket_len){
    if(i&1){
      sort(bucket[i].begin(), bucket[i].end(), [](pair<int, int> A, pair<int, int> B){ return A.second < B.second; });
    }
    else{
      sort(bucket[i].begin(), bucket[i].end(), [](pair<int, int> A, pair<int, int> B){ return A.second > B.second; });
    }
  }

  int query_idx=0;
  int v_idx=0;
  vector<pair<int,ll>>ans(q);
  LCA lca(edge,0);
  ll all_off=(ll)0;

  rep(bucket_i,bucket_len){
    for(auto [i,j]:bucket[bucket_i]){
      int xi=queries[i].first.first;
      int yi=queries[i].first.second;
      int frm=visited[v_idx];
      int to=visited[j];
      if(frm!=to){
        int L=lca.query(frm,to);
        int now=frm;
        while(true){
          if(now==L)break;
          int p=par[now].first;
          ll w=par[now].second;
          all_off+=w;
          //seg.apply(0,n,w);
          seg.apply(eular_order[now],eular_out[now],-2*w);
          now=par[now].first;
        }
        now=to;
        while(true){
          if(now==L)break;
          int p=par[now].first;
          ll w=par[now].second;
          all_off-=w;
          //seg.apply(0,n,-w);
          seg.apply(eular_order[now],eular_out[now],2*w);
          now=par[now].first;
        }
      }
      v_idx=j;
      while(query_idx!=i+1){
        if(query_idx<i+1){
          int y=queries[query_idx].first.second;
          ll w=queries[query_idx].second;
          seg.apply(eular_order[y],eular_out[y],w);
          query_idx+=1;
        }
        else{
          query_idx-=1;
          int y=queries[query_idx].first.second;
          ll w=queries[query_idx].second;
          seg.apply(eular_order[y],eular_out[y],-w);
        }
      }
      ans[i]=seg.all_prod();
      ans[i].first+=all_off;
    }
  }
  rep(i,q){
    print(ans[i].second+1,-ans[i].first);
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3660kb

input:

6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

output:

6 100005
6 10
6 10
1 4
1 -1
1 1

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3664kb

input:

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2

output:

4 -87
1 17
4 13
1 19
1 17
1 15

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3900kb

input:

6 3
0 0 0 0 0 0
1 10
1 10
1 -100
4 10
4 11
1 1 0
4 1 0
1 4 1000

output:

2 10
6 11
2 10

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: -100
Time Limit Exceeded

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:


result: