QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#480966 | #7245. Frank Sinatra | Hadtsti | WA | 2ms | 10004kb | C++14 | 1.5kb | 2024-07-16 19:51:56 | 2024-07-16 19:51:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int T,ID[100010],n,q,x,y,z,fa[100010],a[100010],num,dfn[200010],rk[200010],cnt[100010],s[310],ll[310],rr[310],ans[100010];
bool fg[100010];
vector<pair<int,int> >E[100010];
struct query
{
int l,r,id;
friend bool operator <(const query &a,const query &b)
{
return ID[a.l]^ID[a.l]?ID[a.l]<ID[b.l]:(ID[a.l]&1?a.r<b.r:a.r>b.r);
}
}Q[100010];
void dfs(int x)
{
dfn[x]=++num;
rk[num]=x;
for(pair<int,int>i:E[x])
{
int y=i.first,z=i.second;
if(y!=fa[x])
{
fa[y]=x;
a[y]=z+1;
dfs(y);
}
}
rk[++num]=x;
}
inline void update(int x)
{
if(a[x]>n)
return;
if(fg[x]^=1)
{
if(!(cnt[a[x]]++))
s[ID[a[x]]]++;
}
else
if(!(--cnt[a[x]]))
s[ID[a[x]]]--;
}
inline int calc()
{
for(int i=1;;i++)
if(s[i]<T)
{
for(int j=(i-1)*T+1;j<=i*T;j++)
if(!cnt[j])
return j-1;
}
}
int main()
{
scanf("%d%d",&n,&q);
T=sqrt(n);
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
E[x].push_back({y,z});
E[y].push_back({x,z});
}
for(int i=1;i<=2*n;i++)
ID[i]=(i-1)/T+1;
dfs(1);
a[1]=n+1;
for(int i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
if(dfn[x]>dfn[y])
swap(x,y);
Q[i]={x,y,i};
}
sort(Q+1,Q+q+1);
for(int i=1,L=1,R=1;i<=q;i++)
{
while(R<dfn[Q[i].r])
update(rk[++R]);
while(L>dfn[Q[i].l])
update(rk[L--]);
while(R>dfn[Q[i].r])
update(rk[R--]);
while(L<dfn[Q[i].l])
update(rk[++L]);
ans[Q[i].id]=calc();
}
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9940kb
input:
7 6 2 1 1 3 1 2 1 4 0 4 5 1 5 6 3 5 7 4 1 3 4 1 2 4 2 5 3 5 3 7
output:
0 1 2 2 3 3
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 10004kb
input:
2 4 1 2 0 1 1 1 2 2 1 2 2
output:
0 1 1 0
result:
ok 4 number(s): "0 1 1 0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 7948kb
input:
10 100 3 7 1 3 1 0 6 1 1 1 9 0 4 1 1 5 8 1 10 6 0 1 2 1 5 3 1 6 1 4 10 6 5 3 3 5 8 9 2 1 3 8 4 8 5 10 10 5 2 7 10 2 10 8 9 5 3 9 4 6 2 1 8 4 7 3 9 2 5 3 7 10 7 2 2 6 6 6 7 1 9 7 8 6 8 7 3 5 10 5 1 1 2 10 8 8 7 4 2 2 3 7 6 2 9 5 4 10 3 2 4 10 6 3 6 7 4 5 6 10 4 8 2 1 4 9 10 7 9 3 5 9 8 7 2 1 1 7 1 7 ...
output:
0 2 2 0 0 2 1 2 0 0 2 2 2 2 0 2 0 2 2 1 2 0 2 0 0 2 1 0 2 0 2 2 0 2 0 0 2 2 2 2 2 0 1 2 2 2 2 2 0 2 2 0 2 2 0 2 0 2 0 2 2 2 2 2 1 2 2 1 2 0 0 2 2 0 0 2 2 2 2 0 2 0 2 0 2 0 0 0 0 1 2 0 2 0 1 2 2 2 2 2
result:
ok 100 numbers
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 8604kb
input:
10 100 8 1 1 9 1 1 4 1 0 1 3 1 1 6 0 1 2 0 5 1 1 7 1 1 10 1 1 3 7 3 9 7 9 1 4 2 4 5 9 10 7 9 3 7 4 8 10 6 10 1 8 9 4 10 10 10 2 5 6 7 6 8 4 7 3 7 8 3 5 10 5 1 10 8 9 1 1 8 3 10 6 5 10 8 1 6 6 8 2 6 4 5 4 8 8 1 5 5 3 8 7 1 2 4 6 7 1 2 8 2 2 5 5 7 2 5 7 1 7 6 1 2 3 5 8 3 2 10 1 1 3 7 10 7 5 1 9 6 3 5 ...
output:
0 0 0 1 1 0 0 0 2 0 2 0 2 0 2 2 2 2 0 0 0 0 0 0 0 0 2 0 0 0 2 1 2 0 0 0 0 1 1 0 2 0 0 2 0 0 1 2 0 2 0 0 0 0 0 2 0 2 1 2 2 2 0 2 0 2 2 2 0 2 0 2 1 0 0 1 2 0 0 2 0 2 0 2 0 0 2 0 0 1 0 1 0 2 1 2 2 2 2 0
result:
wrong answer 74th numbers differ - expected: '2', found: '0'