QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#279645#4410. 隆huolinglingCompile Error//C++143.4kb2023-12-08 22:38:052023-12-08 22:38:05

Judging History

你现在查看的是最新测评结果

  • [2023-12-08 22:38:05]
  • 评测
  • [2023-12-08 22:38:05]
  • 提交

answer

#include<algorithm>
#include<cstdio>
#include<iostream> 
#define N 500100
using namespace std;
int head[N],ver[N<<1],net[N<<1],tot;
int dep[N],fa[N],siz[N],son[N],top[N];
int seg[N],rev[N],sum[N<<2],maxn[N<<2];
int n,m,s,num[N];char op[10];
inline void add(int x,int y){ver[++tot]=y;net[tot]=head[x];head[x]=tot;}

void dfs1(int x,int f)
{
    siz[x]=1;
    dep[x]=dep[f]+1;
    fa[x]=f;
    for(int k=head[x];k;k=net[k])
    {
        int y;
		y=ver[k];
		if(y==f)continue;
		dfs1(y,x);
        if(siz[son[x]]<siz[y])son[x]=y;
        siz[x]+=siz[y];
    }
}

void dfs2(int x,int f)
{
    if(son[x]){
	seg[son[x]]=++seg[0];rev[seg[0]]=son[x];//分配线段树节点
    top[son[x]]=top[x];dfs2(son[x],x);	
	}   
    for(int k=head[x],y;y=ver[k],k;k=net[k])
    {
        if(y==fa[x]||y==son[x])continue;
        seg[y]=++seg[0];rev[seg[0]]=y;
        top[y]=y;dfs2(y,x);
    }
}
void build(int p,int l,int r)//不会线段树的可以去逛我的blog
{
    if(l==r){sum[p]=maxn[p]=num[rev[l]];return;}
    int mid=(l+r)>>1;
    build(p<<1,l,mid);build(p<<1|1,mid+1,r);
    sum[p]=sum[p<<1]+sum[p<<1|1];
    maxn[p]=max(maxn[p<<1],maxn[p<<1|1]);
}
void change(int p,int l,int r,int x,int val)
{
    if(l==r){sum[p]=maxn[p]=val;return;}
    int mid=(l+r)>>1;
    if(x<=mid)change(p<<1,l,mid,x,val);
    else change(p<<1|1,mid+1,r,x,val);
    sum[p]=sum[p<<1]+sum[p<<1|1];
    maxn[p]=max(maxn[p<<1],maxn[p<<1|1]);
}
int ask_sum(int p,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)return sum[p];
    int mid=(l+r)>>1,ans=0;
    if(x<=mid)ans=ask_sum(p<<1,l,mid,x,y);
    if(y>mid)ans+=ask_sum(p<<1|1,mid+1,r,x,y);
    return ans;
}
int ask_max(int p,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)return maxn[p];
    int mid=(l+r)>>1,ans=-30000;
    if(x<=mid)ans=ask_max(p<<1,l,mid,x,y);
    if(y>mid)ans=max(ans,ask_max(p<<1|1,mid+1,r,x,y));
    return ans;
}

int query_max(int x,int y)//查询部分
{
    int fx=top[x],fy=top[y],ans=-30000;//记得初始化
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])swap(x,y),swap(fx,fy);
        ans=max(ans,ask_max(1,1,seg[0],seg[fx],seg[x]));//fx~x的这条链
        x=fa[fx],fx=top[x];
    }
    if(dep[x]>dep[y])swap(x,y);
    ans=max(ans,ask_max(1,1,seg[0],seg[x],seg[y]));//x~y的这条链
    return ans;
}
int query_sum(int x,int y)
{
    int fx=top[x],fy=top[y],ans=0;
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])swap(x,y),swap(fx,fy);
        ans+=ask_sum(1,1,seg[0],seg[fx],seg[x]);//同上
        x=fa[fx],fx=top[x];
    }
    if(dep[x]>dep[y])swap(x,y);
    ans+=ask_sum(1,1,seg[0],seg[x],seg[y]);
    return ans;
}
int lca(int x,int y){
	int fx=top[x],fy=top[y];
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])swap(x,y),swap(fx,fy);
       // ans+=ask_sum(1,1,seg[0],seg[fx],seg[x]);//同上
        x=fa[fx],fx=top[x];
    }
    if(dep[x]>dep[y])
       return y;
    else
       return x; 	
}
signed main()
{
    scanf("%d%d%d",&n,&m,&s);
    int x,y;
    for(int i=1;i<n;++i)
    scanf("%d%d",&x,&y),add(x,y),add(y,x);
    //for(int i=1;i<=n;++i)scanf("%d",num+i);
    //dep[1]=rev[1]=seg[0]=seg[1]=top[1]=1;//记得对根节点初始化!!!dep[1]也可以默认为0 
    dep[s]=seg[0]=seg[s]=1;//对根节点初始化!!!
    top[s]=s;
    rev[1]=s;
    
    dfs1(s,0);dfs2(s,0);
	//build(1,1,n);
	//scanf("%d",&m);

	
    while(m--)
    {
        scanf("%d%d",&x,&y);
        cout<<lca(x,y)<<"\n";
    }
}

詳細信息

answer.code: In function ‘int main()’:
answer.code:116:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  116 |     scanf("%d%d%d",&n,&m,&s);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
answer.code:119:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  119 |     scanf("%d%d",&x,&y),add(x,y),add(y,x);
      |     ~~~~~^~~~~~~~~~~~~~
answer.code:133:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  133 |         scanf("%d%d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cctYbp59.o: in function `main':
answer.code:(.text.startup+0x0): multiple definition of `main'; /tmp/ccMuIBua.o:implementer.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccMuIBua.o: in function `main':
implementer.cpp:(.text.startup+0x2f4): undefined reference to `init(int, int, int, std::vector<int, std::allocator<int> >, std::vector<infoset, std::allocator<infoset> >)'
/usr/bin/ld: implementer.cpp:(.text.startup+0x33b): undefined reference to `ask(int, int)'
/usr/bin/ld: implementer.cpp:(.text.startup+0x3f3): undefined reference to `modify(int, int)'
collect2: error: ld returned 1 exit status