QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#419798 | #8716. 树 | ucup-team2580# | WA | 4ms | 17960kb | C++23 | 2.0kb | 2024-05-24 11:24:23 | 2024-05-24 11:24:23 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int n,m,q;
int b[200200];
vector<int> G[200200];
int depth[200200],anc[18][200200];
void dfs(int u,int fa)
{
depth[u]=depth[fa]+1;
anc[0][u]=fa;
for(auto v:G[u])
if(v!=fa)
dfs(v,u);
}
inline int lca(int u,int v)
{
for(int i=17;i>=0;i--)
{
if(depth[anc[i][u]]>=depth[v])
u=anc[i][u];
if(depth[anc[i][v]]>=depth[u])
v=anc[i][v];
}
if(u==v) return u;
for(int i=17;i>=0;i--)
if(anc[i][u]!=anc[i][v])
{
u=anc[i][u];
v=anc[i][v];
}
return anc[0][u];
}
inline int dist(int x,int y)
{
return depth[x]+depth[y]-depth[lca(x,y)]*2;
}
inline bool onChain(int x,int y,int z)
{
return dist(x,y)+dist(y,z)==dist(x,z);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
dfs(1,0);
for(int i=1;i<18;i++)
for(int j=1;j<=n;j++)
anc[i][j]=anc[i-1][anc[i-1][j]];
for(int i=1;i<=n;i++)
cin>>b[i];
int ans=n;
for(int i=2;i<n;i++)
ans-=onChain(b[i-1],b[i],b[i+1]);
while(q--)
{
int u,v;
cin>>u>>v;
for(int i=max(2,u-1);i<=min(n-1,u+1);i++)
ans+=onChain(b[i-1],b[i],b[i+1]);
b[u]=v;
for(int i=max(2,u-1);i<=min(n-1,u+1);i++)
ans-=onChain(b[i-1],b[i],b[i+1]);
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 16044kb
input:
5 5 3 2 1 3 2 1 4 5 1 1 5 4 2 3 1 3 5 3 3 3
output:
4 4 5
result:
ok 3 number(s): "4 4 5"
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 17960kb
input:
30 200 200 10 24 10 13 10 26 13 29 27 26 17 24 27 21 17 15 13 5 13 30 27 3 18 21 9 21 2 24 10 4 11 5 2 8 10 23 1 18 21 25 4 20 12 23 22 27 28 27 18 7 13 6 14 30 10 19 16 21 14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...
output:
27 27 26 26 26 26 26 26 27 26 25 25 26 26 26 26 27 27 27 27 27 26 25 25 25 25 26 27 27 27 26 25 26 27 26 26 25 24 25 25 25 25 27 28 27 27 27 28 27 27 27 27 27 28 26 25 26 25 24 24 24 24 23 23 24 24 24 24 25 23 23 23 23 22 23 23 24 26 25 23 23 23 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '27'