UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204796#3620. ALJ071009835ms61404kbC++113.2kb2024-06-09 12:38:262024-06-09 13:17:43

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define pb emplace_back
#define sz(a) int(a.size())

const int N = 3e5 + 5;
int n, m;
int siz[N], top[N], hson[N], dep[N], fa[N];
int pot[N];
int dfn[N], ed[N], cntd, seq[N];
vector<int> G[N];
int p;
struct {
  LL tr[N];
  void add(int x, LL y) {
    for (; x <= n; x += x & -x) {
      tr[x] += y;
    }
  }
  LL ask(int x) {
    LL y = 0;
    for (; x; x -= x & -x) {
      y += tr[x];
    }
    return y;
  }
  LL ask(int x, int y) {
    return ask(y) - ask(x - 1);
  }
} A, B;
void add(int x, int y, LL c) {
  A.add(x, c), A.add(y + 1, -c);
  B.add(x, c * x), B.add(y + 1, -c * (y + 1));
}
LL ask(int l, int r) {
  return A.ask(l, r) * (r + 1) - B.ask(l, r) + A.ask(1, l - 1) * (r - l + 1);
}
void dfs1(int u, int F) {
  siz[u] = 1;
  for (auto v : G[u]) {
    if (v != F) {
      dep[v] = dep[u] + 1, fa[v] = u;
      dfs1(v, u), siz[u] += siz[v];
      if (siz[hson[u]] < siz[v]) {
        hson[u] = v;
      }
    }
  }
}
void dfs2(int u, int tp) {
  top[u] = tp, seq[dfn[u] = ++cntd] = u;
  pot[tp] = cntd;
  if (hson[u]) {
    dfs2(hson[u], tp);
  }
  for (auto v : G[u]) {
    if (v != hson[u] && v != fa[u]) {
      dfs2(v, v);
    }
  }
  ed[u] = cntd;
}
int lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) {
      swap(u, v);
    }
    u = fa[top[u]];
  }
  return dep[u] < dep[v] ? u : v;
}
int getweight(int u) {
  int L = dfn[u], R = pot[top[u]];
  while (L < R) {
    int mid = ((L + R + 1) >> 1);
    if (siz[seq[mid]] <= siz[u] - siz[seq[mid]]) {
      R = mid - 1;
    }else {
      L = mid;
    }
  }
  return seq[L];
}
void addlink(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) {
      swap(x, y);
    }
    add(dfn[top[x]], dfn[x], z);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) {
    swap(x, y);
  }
  add(dfn[x], dfn[y], z);
}
LL sum;
vector<int> to[N], rec;
int qry() {
  int u = 1;
  LL sum = ask(1, n);
  for (; ; ) {
    int L = dfn[top[u]], R = pot[top[u]];
    while (L < R) {
      int mid = ((L + R + 1) >> 1), u = seq[mid];
      LL s = ask(dfn[u], ed[u]);
      if (s <= sum - s) {
        R = mid - 1;
      }else {
        L = mid;
      }
    }
    u = seq[L];
    for (auto v : to[u]) {
      LL s = ask(dfn[v], ed[v]);
      if (s > sum - s) {
        u = v;
        goto End;
      }
    }
    return u;
    End:;
  }
  return u;
}
void ad(int x) {
  while (top[x] != 1) {
    x = top[x];
    to[fa[x]].pb(x);
    x = fa[x];
    rec.pb(x);
  }
}
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v, G[u].pb(v), G[v].pb(u);
  }
  dfs1(1, 0), dfs2(1, 1);
  p = 1;
  while (m--) {
    int op, x, y, z, pos;
    cin >> op;
    ad(p);
    if (op == 1) {
      cin >> x >> z;
      add(dfn[x], ed[x], z);
      ad(getweight(x));
    }else {
      cin >> x >> y >> z;
      addlink(x, y, z), ad(x), ad(y);
    }
    p = qry();
    cout << p << '\n';
    for (auto i : rec) {
      vector<int>().swap(to[i]);
    }
    vector<int>().swap(rec);
  }
  return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 8ms
memory: 15632kb

input:

1631 1695
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19...

output:

1303
721
688
701
682
737
588
628
713
769
774
780
862
866
871
886
909
929
937
943
941
946
945
954
949...

result:

ok 1695 lines

Test #2:

score: 0
Accepted
time: 9ms
memory: 15536kb

input:

1724 1881
2 1
3 2
4 2
5 2
6 1
7 6
8 2
9 8
10 6
11 10
12 1
13 1
14 12
15 7
16 3
17 14
18 13
19 4
20 8...

output:

910
910
910
910
910
910
910
910
910
910
910
910
826
826
826
826
826
826
1006
1006
1006
1006
1006
100...

result:

ok 1881 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 15536kb

input:

1935 1603
2 1
3 2
4 3
5 1
6 1
7 2
8 6
9 1
10 1
11 8
12 11
13 10
14 4
15 2
16 9
17 8
18 14
19 14
20 7...

output:

14
2
10
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
...

result:

ok 1603 lines

Test #4:

score: 0
Accepted
time: 10ms
memory: 15536kb

input:

1782 1942
2 1
3 1
4 2
5 4
6 1
7 1
8 1
9 7
10 4
11 5
12 10
13 4
14 7
15 14
16 15
17 3
18 5
19 16
20 1...

output:

14
7
7
7
4
4
4
4
4
1
1
7
1
4
4
4
4
4
4
2
1
1
4
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

result:

ok 1942 lines

Subtask #2:

score: 20
Accepted

Test #5:

score: 20
Accepted
time: 819ms
memory: 59360kb

input:

281495 252939
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

213578
210748
135169
135026
144651
132400
134957
124141
118451
118781
137612
154321
170613
166113
17...

result:

ok 252939 lines

Test #6:

score: 0
Accepted
time: 681ms
memory: 58164kb

input:

273746 287587
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

189163
189918
190785
185592
183371
184857
188261
186057
186098
184608
193692
178813
178667
179002
17...

result:

ok 287587 lines

Test #7:

score: 0
Accepted
time: 455ms
memory: 54076kb

input:

247712 269003
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

176247
185709
162271
169277
173592
171721
153484
155588
162688
163154
161122
160221
154947
154853
14...

result:

ok 269003 lines

Test #8:

score: 0
Accepted
time: 857ms
memory: 61404kb

input:

294554 246011
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

225125
191067
199412
203257
197830
189818
184048
184192
183424
180181
180167
185337
187207
185668
18...

result:

ok 246011 lines

Subtask #3:

score: 30
Accepted

Test #9:

score: 30
Accepted
time: 517ms
memory: 60156kb

input:

286640 284270
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

257488
195036
211947
235498
241668
232198
232766
228649
228553
208960
201281
202100
202317
203208
20...

result:

ok 284270 lines

Test #10:

score: 0
Accepted
time: 604ms
memory: 36896kb

input:

260280 269761
2 1
3 1
4 2
5 4
6 5
7 2
8 3
9 6
10 7
11 1
12 7
13 3
14 9
15 3
16 11
17 13
18 2
19 14
2...

output:

180457
180457
171997
177266
159775
159174
159174
159174
232429
232429
146084
146084
146084
146084
14...

result:

ok 269761 lines

Test #11:

score: 0
Accepted
time: 956ms
memory: 40276kb

input:

299939 243207
2 1
3 1
4 2
5 1
6 5
7 5
8 1
9 3
10 8
11 1
12 9
13 8
14 11
15 5
16 13
17 11
18 14
19 16...

output:

289736
289736
8
5
1
1
1
1
1
1
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
...

result:

ok 243207 lines

Test #12:

score: 0
Accepted
time: 557ms
memory: 37064kb

input:

260657 248813
2 1
3 1
4 1
5 4
6 5
7 1
8 5
9 5
10 5
11 1
12 9
13 11
14 6
15 12
16 10
17 12
18 16
19 1...

output:

192653
115832
172896
11
5
190178
32264
32264
9
9
9
9
12
12
12
12
3992
3992
3992
3992
3992
3992
3992
...

result:

ok 248813 lines

Subtask #4:

score: 40
Accepted

Test #13:

score: 40
Accepted
time: 530ms
memory: 53084kb

input:

241289 290298
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

198749
185755
175529
192379
192636
183414
183546
173058
171195
154762
156345
166238
164292
163728
15...

result:

ok 290298 lines

Test #14:

score: 0
Accepted
time: 1327ms
memory: 39820kb

input:

295796 290103
2 1
3 1
4 2
5 1
6 1
7 6
8 4
9 8
10 2
11 5
12 4
13 4
14 3
15 12
16 2
17 10
18 1
19 14
2...

output:

70561
125326
125326
133092
125326
125326
125326
125326
125326
125326
125326
133092
133092
133092
125...

result:

ok 290103 lines

Test #15:

score: 0
Accepted
time: 1245ms
memory: 37720kb

input:

268550 291961
2 1
3 2
4 1
5 1
6 3
7 3
8 3
9 3
10 2
11 4
12 6
13 12
14 12
15 3
16 1
17 14
18 6
19 1
2...

output:

223475
1
2
2
2
4435
4435
4435
4435
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
...

result:

ok 291961 lines

Test #16:

score: 0
Accepted
time: 1256ms
memory: 39260kb

input:

287030 275087
2 1
3 1
4 1
5 4
6 3
7 6
8 6
9 3
10 5
11 9
12 8
13 1
14 4
15 10
16 1
17 5
18 11
19 12
2...

output:

18
9
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
712...

result:

ok 275087 lines

Extra Test:

score: 0
Extra Test Passed