[SCOI2015]情报传递[树剖+主席树]

[SCOI2015]情报传递

题意大概就是 使得在 \(i\) 时刻加入一个情报员帮您传情报 然后询问 \(x,y,c\)\(x\)\(y\)多少个人有风险…(大于c)的都有风险…每天风险值+1

看起来…不太可做…

每次要整棵树+1复杂度也需要\(log^2\)的树套树吧

但是显然不用啊 查询的时候减掉就可以了…

所以直接树剖上面无脑主席树就可以了啊…

#include 
// #define int long long
#define rep(a , b , c) for(int a = b ; a <= c ; ++ a)
#define Rep(a , b , c) for(int a = b ; a >= c ; -- a)
#define go(u) for(int i = G.head[u] , v = G.to[i] , w = G.dis[i] ; i ; v = G.to[i = G.nxt[i]] , w = G.dis[i])

using namespace std ;
using ll = long long ;
using pii = pair < int , int > ;
using vi = vector < int > ;

int read() {
  int x = 0 ; bool f = 1 ; char c = getchar() ;
  while(c < 48 || c > 57) { if(c == '-') f = 0 ; c = getchar() ; }
  while(c > 47 && c < 58) { x = (x << 1) + (x << 3) + (c & 15) ; c = getchar() ; }
  return f ? x : -x ;
}

template  void print(T x , char c = '\n') {
  static char st[100] ; int stp = 0 ;
  if(! x) { putchar('0') ; }
  if(x < 0) { x = -x ; putchar('-') ; }
  while(x) { st[++ stp] = x % 10 ^ 48 ; x /= 10 ; }
  while(stp) { putchar(st[stp --]) ; } putchar(c) ;
}

template  void cmax(T & x , T y) { x < y ? x = y : 0 ; }
template  void cmin(T & x , T y) { x > y ? x = y : 0 ; }

const int _N = 1e6 + 10 ;
struct Group {
  int head[_N] , nxt[_N << 1] , to[_N] , dis[_N] , cnt = 1 ;
  Group () { memset(head , 0 , sizeof(head)) ; }
  void add(int u , int v , int w = 1) { nxt[++ cnt] = head[u] ; to[cnt] = v ; dis[cnt] = w ; head[u] = cnt ; }
} G;

const int N = 2e5 + 10  ;
typedef int arr[N] ;
int n , q ;
arr X , Y , c , rt , sz , fa , son , d , val ;
int root = 0 ;
void dfs(int u) {
    sz[u] = 1 ; go(u) {
        d[v] = d[u] + 1 ;
        dfs(v) ; sz[u] += sz[v] ;
        if(sz[v] > sz[son[u]]) son[u] = v ;
    }
}
int idx = 0 ;
arr top , id ;
void dfs(int u , int t){
    top[u] = t ; id[u] = ++ idx ;
    if(son[u]) dfs(son[u] , t) ;
    go(u) if(v ^ son[u]) dfs(v , v) ;
}
int cnt = 0 ;
int ls[N << 5] , rs[N << 5] , sum[N << 5] ;
void upd(int pre , int & p , int l , int r , int pos) {
    ls[p = ++ cnt] = ls[pre] ;
    rs[p] = rs[pre] ;
    sum[p] = sum[pre] + 1 ;
    if(l == r) return ;
    int mid = l + r >> 1 ;
    pos <= mid ? upd(ls[pre] , ls[p] , l , mid , pos) : upd(rs[pre] , rs[p] , mid + 1 , r , pos) ;
}
int query(int L , int R , int l , int r , int x) {
    if(l == r) return sum[R] - sum[L] ;
    int mid = l + r >> 1 ;
    if(x <= mid) return query(ls[L] , ls[R] , l , mid , x) ;
    return sum[ls[R]] - sum[ls[L]] + query(rs[L] , rs[R] , mid + 1 , r , x) ;
}
void build(int u) {
    upd(rt[fa[u]] , rt[u] , 1 , q , val[u]) ; go(u) build(v) ;
}
int Lca(int x , int y) {
    while(top[x] != top[y]) {
        if(d[top[x]] < d[top[y]]) swap(x , y) ;
        x = fa[top[x]] ;
    }
    return d[x] < d[y] ? x : y ;
}
signed main() {
    n = read() ;
    rep(i , 1 , n) { fa[i] = read() ; if(! fa[i]) root = i ; else G.add(fa[i] , i) ; }
    q = read() ;
    rep(i , 1 , n) val[i] = q ;
    rep(i , 1 , q) {
        int op = read() ;
        if(op == 1) X[i] = read() , Y[i] = read() , c[i] = read() ;
        else val[read()] = i ;
    }
    dfs(root) ; dfs(root , root) ; build(root) ;
    rep(i , 1 , q) {
        if(! X[i]) continue ;
        int lca = Lca(X[i] , Y[i]) ;
        print(d[X[i]] + d[Y[i]] - (d[lca] << 1) + 1 , ' ') ;
        if(i - c[i] - 1 <= 0) { print(0) ; continue ; }
        print(query(rt[lca] , rt[X[i]] , 1 , q , i - c[i] - 1) + query(rt[lca] , rt[Y[i]] , 1 , q , i - c[i] - 1) + (val[lca] <= i - c[i] - 1)) ;
    }
    return 0 ;
}

你可能感兴趣的:([SCOI2015]情报传递[树剖+主席树])