QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#391405 | #7245. Frank Sinatra | Siilhouette | WA | 0ms | 26224kb | C++14 | 2.9kb | 2024-04-16 16:11:53 | 2024-04-16 16:11:53 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define bel(x) ((x-1)/(block)+1)
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
const int N=400010;
int n,m,k,block,ans,a[N],c[N],realans[N],vis[N];
struct Query{
int l,r,id;
inline bool operator <(const Query &that){
return bel(l)==bel(that.l)?bel(l)&1?
r<that.r : r>that.r : l<that.l;
}
}q[N];
inline int g()
{
int res=0,fix=1;
char ch;
while(!isdigit(ch=getchar()))
fix=(ch=='-')?-1:fix;
do res=res*10+(ch^'0');
while(isdigit(ch=getchar()));
return fix*res;
}
inline void out(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)out(x/10);
putchar(x%10+'0');
}
struct Graph{
int tot,head[N],tim,st[N],ed[N],suiv[N<<1],ver[N<<1],edge[N],fa[N],siz[N],eu[N],son[N],top[N],d[N];
inline void lnk(int x,int y,int z)
{
ver[++tot]=y;
edge[tot]=z;
suiv[tot]=head[x];
head[x]=tot;
}
inline void dfs1(int x,int maxson=-1)
{
d[x]=d[fa[x]]+1;
eu[st[x]=++tim]=x;siz[x]=1;
for(int i=head[x];i;i=suiv[i])
{
int y=ver[i],z=edge[i];
if(y==fa[x])continue;
fa[y]=x;a[y]=z;
dfs1(y);siz[x]+=siz[y];
if(siz[y]>maxson)
maxson=siz[y],son[x]=y;
}
eu[ed[x]=++tim]=x;
}
inline void dfs2(int x,int acro)
{
top[x]=acro;
if(son[x])dfs2(son[x],acro);
for(int i=head[x];i;i=suiv[i])
{
int y=ver[i];
if(y==fa[x]||y==son[x])continue;
dfs2(y,y);
}
}
inline int lca(int x,int y)
{
while(top[x]^top[y])
{
if(d[top[x]]<d[top[y]])swap(x,y);
x=fa[top[x]];
}
if(d[x]>d[y])swap(x,y);
return x;
}
}e;
inline void update(int x,int val)
{
if(e.eu[x]==1)return;
int y=a[e.eu[x]];
if(!vis[e.eu[x]])
{
c[y]++;
if(c[y]==1&&ans==y)
while(c[ans])ans++;
}
else
{
c[y]--;
if(!c[y]&&y<ans)ans=y;
}
vis[e.eu[x]]^=1;
/*
if(val==1)
{
c[a[x]]++;
if(c[a[x]]==1&&ans==a[x])
while(c[ans])ans++;
}
else
{
c[a[x]]--;
if(!c[a[x]]&&a[x]<ans)ans=a[x];
}
*/
}
int main()
{
n=g(),m=g();
for(int i=1,x,y,z;i<n;i++)
x=g(),y=g(),z=g(),e.lnk(x,y,z),e.lnk(y,x,z);
e.dfs1(1);
e.dfs2(1,1);
block=(int)sqrt(n<<1)+1;
for(int i=1,x,y,l;i<=m;i++)
{
x=g(),y=g(),l=e.lca(x,y);
//cout<<"lca "<<l<<endl;
if(e.st[x]>e.st[y])swap(x,y);
if(l==x)q[i].l=e.st[x],q[i].r=e.st[y];
else q[i].l=e.ed[x],q[i].r=e.st[y];
q[i].id=i;
}
sort(q+1,q+1+m);
int l=1,r=0;
for(int i=1;i<=m;i++)
{
while(l<q[i].l)update(l++,-1);
while(l>q[i].l)update(--l,1);
while(r<q[i].r)update(++r,1);
while(r>q[i].r)update(r--,-1);
realans[q[i].id]=ans;
}
for(int i=1;i<=m;i++)
out(realans[i]),putchar('\n');
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26224kb
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: -100
Wrong Answer
time: 0ms
memory: 24212kb
input:
2 4 1 2 0 1 1 1 2 2 1 2 2
output:
0 1 1 1
result:
wrong answer 4th numbers differ - expected: '0', found: '1'