답안 #999228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999228 2024-06-15T08:26:31 Z vjudge1 Sumtree (INOI20_sumtree) C++17
50 / 100
3000 ms 66640 KB
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9+7;
const int N = 4e5+10;
const int MAXNC=5e5+10;
const int LG = 20;
vector<int> g[N];
int tin[N], tout[N], tim=0;
int up[N][LG], tags[N];
long long fact[MAXNC];
int depth[N], sz[N], used[N], tagsms[N];

namespace BIT {
  int fen[N];
  void add(int at, int vl) {
    at++;
    while(at < N) {
      fen[at] += vl;
      at+=at&(-at);
    }
  }
  int query(int at) {
    at++;
    int res=0;
    while(at) {
      res+=fen[at];
      at-=at&(-at);
    }
    return res;
  }
};
namespace BIT2 {
  int fen[N];
  void add(int at, int vl) {
    at++;
    while(at < N) {
      fen[at] += vl;
      at+=at&(-at);
    }
  }
  int query(int at) {
    at++;
    int res=0;
    while(at) {
      res+=fen[at];
      at-=at&(-at);
    }
    return res;
  }
};
namespace BIT3 {
  int fen[N];
  void add(int at, int vl) {
    at++;
    while(at < N) {
      fen[at] += vl;
      at+=at&(-at);
    }
  }
  int query(int at) {
    at++;
    int res=0;
    while(at) {
      res+=fen[at];
      at-=at&(-at);
    }
    return res;
  }
};
void change(int x, int vl) {
  BIT::add(tin[x], vl);
  BIT::add(tout[x], -vl);
}
int query(int anc, int ch) {
  return BIT::query(tin[ch])-BIT::query(tin[anc]);
}
int query2(int l, int r) {
  if(l>r)return 0;
  if(l == 0)return BIT2::query(r);
  return BIT2::query(r)-BIT2::query(l-1);
}

int query3(int l, int r) {
  if(l>r)return 0;
  if(l == 0)return BIT3::query(r);
  return BIT3::query(r)-BIT3::query(l-1);
}

long long binpow(long long a, long long b) {
  long long res = 1;
  while(b) {
    if(b&1) {
      res=(res*a)%mod;
    }
    a=(a*a)%mod;
    b>>=1;
  }
  return res;
}
long long ncr(long long nn, long long rr) {
  if(rr > nn)return 0;
  if(rr < 0)return 0;
  long long res = fact[nn];
  res = (res*binpow(fact[rr], mod-2))%mod;
  res = (res*binpow(fact[nn-rr], mod-2))%mod;
  return res;
}
void dfs(int at, int p) {
  sz[at]=1;
  up[at][0] = p;
  for(int i = 1;i<LG;i++)up[at][i] = up[up[at][i-1]][i-1];
  tin[at] = tim++;
  for(int to:g[at]) {
    if(to == p)continue;
    depth[to]=depth[at]+1;
    dfs(to, at);
    sz[at]+=sz[to];
  }
  tout[at] = tim++;
}

int findroot(int at) {
  for(int i = LG-1;i>=0;i--) {
    if(up[at][i] != 0 and query(up[at][i], at) == depth[at]-depth[up[at][i]]) {
      at = up[at][i];
    }
  }
  return at;
}

struct ANS {
  long long mult = 1;
  long long zero = 0;
  void add(long long vl) {
    if(vl == 0)
      zero++;
    else
      mult = (mult*vl)%mod;
  }
  void del(long long vl) {
    if(vl == 0) {
      zero--;
    }
    else {
      mult = (mult*binpow(vl, mod-2))%mod;
    }
  }
  long long get() {
    if(zero>0)return 0;
    return mult;
  }

};
int main () {
  cin.tie(0)->sync_with_stdio(0);
  fact[0] = 1;
  for(int i = 1;i<MAXNC;i++)fact[i] = (fact[i-1]*i)%mod;
  int n, r;
  cin >> n >> r;
  for(int i = 1;i<n;i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  tags[1] = r;
  used[1] = 1;
  for(int i = 2;i<=n;i++)change(i, 1);
//  for(int i = 1;i<=n;i++) {
//    BIT2::add(tin[i], 1);
//  }
//  BIT3::add(tin[1], r);
//  BIT3::add(tout[1], -r);
  ANS ans;
  ans.add(ncr(n+r-1, r));
  cout << ans.get() << "\n";
  int q;
  cin >> q;
  while(q--) {
    int t;
    cin >> t;
    if(t == 1) {
      int u, v;
      cin >> u >> v;
      tags[u]=v;
      int rr = findroot(u);
      {
        long long tmp = tags[rr] - tagsms[rr];
        ans.del(ncr(sz[rr]+tmp-1, tmp)); 
      }
      int par = up[u][0];
      while(1) {
        sz[par]-=sz[u];
        tagsms[par]-=tagsms[u];
        tagsms[par]+=v;
        if(par==rr)break;
        par = up[par][0];
      }
      {
        long long tmp = tags[u] - tagsms[u];
        ans.add(ncr(sz[u]+tmp-1, tmp)); 
      }
      {
        long long tmp = tags[rr]-tagsms[rr];
        ans.add(ncr(sz[rr]+tmp-1, tmp)); 
      }
      change(u, -1);
    }
    else {
      int u;
      cin >> u;
      int v = tags[u];
      change(u, 1);
      int rr = findroot(u);
      {
        long long tmp = tags[rr] - tagsms[rr];
        ans.del(ncr(sz[rr]+tmp-1, tmp)); 
      }
      int par = up[u][0];
      while(1) {
        sz[par]+=sz[u];
        tagsms[par]+=tagsms[u];
        tagsms[par]-=v;
        if(par==rr)break;
        par = up[par][0];
      }
      {
        long long tmp = tags[u] - tagsms[u];
        ans.del(ncr(sz[u]+tmp-1, tmp)); 
      }
      {
        long long tmp = tags[rr]-tagsms[rr];
        ans.add(ncr(sz[rr]+tmp-1, tmp)); 
      }
    }
    cout << ans.get() << "\n";
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 58456 KB Output is correct
2 Correct 98 ms 58452 KB Output is correct
3 Correct 92 ms 58452 KB Output is correct
4 Correct 90 ms 58452 KB Output is correct
5 Correct 70 ms 54608 KB Output is correct
6 Correct 6 ms 25948 KB Output is correct
7 Correct 6 ms 25692 KB Output is correct
8 Correct 7 ms 25656 KB Output is correct
9 Correct 87 ms 50744 KB Output is correct
10 Correct 79 ms 50560 KB Output is correct
11 Correct 84 ms 50688 KB Output is correct
12 Correct 81 ms 50252 KB Output is correct
13 Correct 95 ms 55120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23644 KB Output is correct
2 Correct 5 ms 23644 KB Output is correct
3 Correct 6 ms 23644 KB Output is correct
4 Correct 6 ms 23644 KB Output is correct
5 Correct 7 ms 23480 KB Output is correct
6 Correct 9 ms 25692 KB Output is correct
7 Correct 10 ms 25660 KB Output is correct
8 Correct 9 ms 25688 KB Output is correct
9 Correct 10 ms 25692 KB Output is correct
10 Correct 11 ms 25948 KB Output is correct
11 Correct 11 ms 25948 KB Output is correct
12 Correct 8 ms 25692 KB Output is correct
13 Correct 10 ms 25960 KB Output is correct
14 Correct 11 ms 25948 KB Output is correct
15 Correct 11 ms 26204 KB Output is correct
16 Correct 11 ms 25692 KB Output is correct
17 Correct 10 ms 25940 KB Output is correct
18 Correct 9 ms 23800 KB Output is correct
19 Correct 10 ms 25908 KB Output is correct
20 Correct 8 ms 25692 KB Output is correct
21 Correct 7 ms 25692 KB Output is correct
22 Correct 6 ms 23640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 57920 KB Output is correct
2 Correct 137 ms 58292 KB Output is correct
3 Correct 109 ms 58636 KB Output is correct
4 Correct 173 ms 59216 KB Output is correct
5 Correct 258 ms 56912 KB Output is correct
6 Correct 8 ms 25948 KB Output is correct
7 Correct 7 ms 25692 KB Output is correct
8 Correct 8 ms 25692 KB Output is correct
9 Correct 249 ms 52692 KB Output is correct
10 Correct 210 ms 52308 KB Output is correct
11 Correct 195 ms 52308 KB Output is correct
12 Correct 157 ms 52052 KB Output is correct
13 Execution timed out 3043 ms 66640 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 501 ms 54868 KB Output is correct
2 Correct 453 ms 54612 KB Output is correct
3 Correct 418 ms 54612 KB Output is correct
4 Correct 446 ms 54864 KB Output is correct
5 Correct 433 ms 54216 KB Output is correct
6 Correct 400 ms 54612 KB Output is correct
7 Correct 342 ms 40788 KB Output is correct
8 Correct 391 ms 40784 KB Output is correct
9 Correct 476 ms 54692 KB Output is correct
10 Correct 421 ms 54872 KB Output is correct
11 Correct 419 ms 54908 KB Output is correct
12 Correct 374 ms 41016 KB Output is correct
13 Correct 218 ms 39508 KB Output is correct
14 Correct 291 ms 39452 KB Output is correct
15 Correct 258 ms 39504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 58456 KB Output is correct
2 Correct 98 ms 58452 KB Output is correct
3 Correct 92 ms 58452 KB Output is correct
4 Correct 90 ms 58452 KB Output is correct
5 Correct 70 ms 54608 KB Output is correct
6 Correct 6 ms 25948 KB Output is correct
7 Correct 6 ms 25692 KB Output is correct
8 Correct 7 ms 25656 KB Output is correct
9 Correct 87 ms 50744 KB Output is correct
10 Correct 79 ms 50560 KB Output is correct
11 Correct 84 ms 50688 KB Output is correct
12 Correct 81 ms 50252 KB Output is correct
13 Correct 95 ms 55120 KB Output is correct
14 Correct 5 ms 23644 KB Output is correct
15 Correct 5 ms 23644 KB Output is correct
16 Correct 6 ms 23644 KB Output is correct
17 Correct 6 ms 23644 KB Output is correct
18 Correct 7 ms 23480 KB Output is correct
19 Correct 9 ms 25692 KB Output is correct
20 Correct 10 ms 25660 KB Output is correct
21 Correct 9 ms 25688 KB Output is correct
22 Correct 10 ms 25692 KB Output is correct
23 Correct 11 ms 25948 KB Output is correct
24 Correct 11 ms 25948 KB Output is correct
25 Correct 8 ms 25692 KB Output is correct
26 Correct 10 ms 25960 KB Output is correct
27 Correct 11 ms 25948 KB Output is correct
28 Correct 11 ms 26204 KB Output is correct
29 Correct 11 ms 25692 KB Output is correct
30 Correct 10 ms 25940 KB Output is correct
31 Correct 9 ms 23800 KB Output is correct
32 Correct 10 ms 25908 KB Output is correct
33 Correct 8 ms 25692 KB Output is correct
34 Correct 7 ms 25692 KB Output is correct
35 Correct 6 ms 23640 KB Output is correct
36 Correct 110 ms 57920 KB Output is correct
37 Correct 137 ms 58292 KB Output is correct
38 Correct 109 ms 58636 KB Output is correct
39 Correct 173 ms 59216 KB Output is correct
40 Correct 258 ms 56912 KB Output is correct
41 Correct 8 ms 25948 KB Output is correct
42 Correct 7 ms 25692 KB Output is correct
43 Correct 8 ms 25692 KB Output is correct
44 Correct 249 ms 52692 KB Output is correct
45 Correct 210 ms 52308 KB Output is correct
46 Correct 195 ms 52308 KB Output is correct
47 Correct 157 ms 52052 KB Output is correct
48 Execution timed out 3043 ms 66640 KB Time limit exceeded
49 Halted 0 ms 0 KB -