QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#327702 | #3307. Query on a Tree 17 | WRuperD | WA | 9ms | 8420kb | C++14 | 3.6kb | 2024-02-15 12:21:45 | 2024-02-15 12:21:47 |
Judging History
answer
// Problem: I. Query On A Tree 17
// URL: https://codeforces.com/gym/102759/problem/I
// Writer: WRuperD
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int long long
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 2e5 + 10;
int n;
vector <int> g[MAX];
int fa[MAX][30];
int siz[MAX], son[MAX];
void dfs(int u, int father){
siz[u] = 1;
fa[u][0] = father;
for(int v : g[u]){
if(v == father) continue;
dfs(v, u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
}
int dfn[MAX], top[MAX], dfx[MAX], clk;
int dep[MAX];
void dfs2(int u, int topu){
dep[u] = dep[fa[u][0]] + 1;
top[u] = topu, dfn[u] = ++clk, dfx[clk] = u;
if(son[u]) dfs2(son[u], topu);
for(int v : g[u]){
if(v == fa[u][0] or v == son[u]) continue;
dfs2(v, v);
}
}
int s[MAX << 1], tag[MAX << 1];
void pushup(int x){
s[x] = s[x << 1] + s[x << 1 | 1];
}
void pushdown(int x, int l, int r){
int mid = (l + r) >> 1;
s[x << 1] += tag[x] * (mid - l + 1), s[x << 1 | 1] += (r - mid) * tag[x];
tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x];
tag[x] = 0;
return ;
}
void build(int l, int r, int x){
tag[x] = 0;
if(l == r){
s[x] = 1;
return ;
}
int mid = (l + r) >> 1;
build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
pushup(x);
}
void add(int l, int r, int dl, int dr, int x){
if(dl <= l and r <= dr){
s[x] += r - l + 1;
tag[x]++;
return ;
}
pushdown(x, l, r);
int mid = (l + r) >> 1;
if(dl <= mid) add(l, mid, dl, dr, x << 1);
if(dr > mid) add(mid + 1, r, dl, dr, x << 1 | 1);
pushup(x);
}
int query(int l, int r, int dl, int dr, int x){
if(dl <= l and r <= dr) return s[x];
pushdown(x, l, r);
int mid = (l + r) >> 1, ans = 0;
if(dl <= mid) ans += query(l, mid, dl, dr, x << 1);
if(dr > mid) ans += query(mid + 1, r, dl, dr, x << 1 | 1);
pushup(x);
return ans;
}
int search(int l, int r, int val, int x){
if(l == r) return dfx[l];
int mid = (l + r) >> 1;
if(s[x << 1] < val) return search(mid + 1, r, val - s[x << 1], x << 1 | 1);
return search(l, mid, val, x << 1);
}
void Add(int u, int v){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]]) swap(u, v);
add(1, n, dfn[top[u]], dfn[u], 1);
u = fa[top[u]][0];
}
if(dfn[u] > dfn[v]) swap(u, v);
add(1, n, dfn[u], dfn[v], 1);
}
void solve(){
n = read();
for(int i = 1; i < n; i++){
int u = read(), v = read();
g[u].pb(v), g[v].pb(u);
}
dfs(1, 1), dfs2(1, 1);
// build(1, n, 1);
for(int i = 1; i <= 29; i++){
for(int j = 1; j <= n; j++){
fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
}
int q = read();
for(int i = 1; i <= q; i++){
int op = read();
if(op == 1){
int x = read();
add(1, n, dfn[x], dfn[x] + siz[x] - 1, 1);
}else{
int u = read(), v = read();
Add(u, v);
}
// write(s[1]), endl;
int x = search(1, n, (s[1]) / 2 + 1, 1);
for(int i = 29; i >= 0; i--){
while (x != fa[x][i] and query(1, n, dfn[fa[x][i]], dfn[fa[x][i]] + siz[fa[x][i]] - 1, 1) <= s[1] / 2){
x = fa[x][i];
}
}
// write(x), endl;
if(query(1, n, dfn[x], dfn[x] + siz[x] - 1, 1) <= s[1] / 2){
x = fa[x][0];
}
write(x), endl;
}
}
signed main(){
int t = 1;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8300kb
input:
7 1 6 1 7 7 3 3 2 7 5 5 4 4 1 2 1 4 1 6 2 6 7
output:
2 7 7 1
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 8308kb
input:
15 1 2 1 3 2 4 3 7 6 2 3 8 4 9 2 5 5 11 7 13 10 4 11 15 12 5 14 7 11 2 6 11 1 6 2 10 9 1 9 2 2 6 1 4 2 7 6 1 2 2 8 13 1 10 2 8 15
output:
2 2 2 2 2 2 2 2 2 2 2
result:
ok 11 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 8236kb
input:
2 1 2 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 2ms
memory: 8420kb
input:
2 1 2 1 2 1 2
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 5ms
memory: 8300kb
input:
100 1 2 58 2 2 87 63 87 65 63 65 19 6 87 58 21 87 8 87 74 91 6 95 65 2 61 87 59 3 61 37 87 67 87 23 2 63 9 87 46 40 67 70 2 12 58 46 75 75 36 28 3 12 33 72 87 39 6 75 52 85 70 1 76 26 40 47 28 2 49 41 65 66 28 51 37 15 47 86 51 8 60 97 19 48 58 72 90 33 83 97 54 36 5 23 14 78 52 90 7 99 2 48 82 48 6...
output:
72 3 3 87 87 2 2 2 2 87 87 87 87 87 2 87 87 87 87 87 87 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87...
result:
ok 10000 lines
Test #6:
score: 0
Accepted
time: 4ms
memory: 8344kb
input:
100 27 1 27 35 96 1 54 35 25 54 81 35 69 25 27 18 69 83 27 99 70 83 81 61 1 77 73 54 35 68 61 89 20 89 99 95 37 20 63 95 95 38 7 83 63 78 86 35 89 13 71 77 70 14 95 34 13 62 24 95 77 98 99 33 25 100 36 63 35 8 65 54 66 83 40 8 15 81 78 59 15 4 62 28 97 14 15 58 69 3 89 44 47 100 52 73 12 95 53 38 39...
output:
46 35 46 83 83 83 83 35 35 35 25 25 25 35 25 25 25 25 25 54 54 35 35 35 54 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 ...
result:
ok 10000 lines
Test #7:
score: 0
Accepted
time: 3ms
memory: 8312kb
input:
100 1 19 19 45 58 45 58 72 36 45 45 94 94 13 90 58 64 90 90 23 45 12 90 52 27 36 19 42 72 35 23 32 67 36 1 18 54 36 33 67 10 1 55 90 54 65 92 72 53 42 65 24 9 45 81 42 85 35 8 81 10 44 85 68 23 30 69 12 43 69 23 25 12 88 85 99 91 9 24 100 48 81 60 94 33 41 52 51 17 19 51 82 30 49 32 28 52 7 82 79 82...
output:
33 45 45 45 45 45 58 58 58 58 58 45 58 45 58 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 ...
result:
ok 10000 lines
Test #8:
score: -100
Wrong Answer
time: 9ms
memory: 8312kb
input:
100 67 1 34 67 34 72 26 72 63 26 26 14 63 24 63 49 42 14 45 34 14 71 71 87 71 7 14 22 64 72 90 7 14 58 87 99 33 87 24 70 65 64 42 30 33 74 99 62 3 71 60 90 84 90 40 70 47 45 84 69 89 7 57 3 99 59 66 65 58 76 37 14 6 37 72 54 38 24 15 99 51 84 93 76 59 8 89 53 75 51 97 99 20 8 28 53 95 15 11 53 95 13...
output:
37 37 14 14 14 14 71 14 14 14 14 14 14 14 14 14 14 14 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 14 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 ...
result:
wrong answer 1st lines differ - expected: '21', found: '37'