社区讨论

编译失败求条

P3384【模板】重链剖分 / 树链剖分参与者 4已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mm2u0zul
此快照首次捕获于
2026/02/26 10:16
2 周前
此快照最后确认于
2026/02/27 15:00
2 周前
查看原帖
编译器里可以编译
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
int nms[100010];
int P;
vector<int> link[100010];
struct Node{
	int hs;
	vector<int> sons;
	int fa;
	int dep;
	int newp;
	int sz;
}nodes[100010];
int gt(int nn,int fa,int dep)
{
	nodes[nn].fa=fa;
	nodes[nn].dep=dep;
	int sumh=1,maxh=0;
	for(unsigned i=0;i<link[nn].size();i++)
	{
		if(link[nn][i]==fa) continue;
		nodes[nn].sons.push_back(link[nn][i]);
		int tmp=gt(link[nn][i],nn,dep+1);
		if(tmp>maxh)
		{
			maxh=tmp;
			nodes[nn].hs=link[nn][i];
		}
		sumh+=tmp;
	}
	nodes[nn].sz=sumh;
	return sumh;
}

int newtoold[100010];
int oldtonew[100010];
int nhead[100010];
int np=1;
void resetn(int nn,int head)
{
	int nnp=np++;
	nhead[nnp]=head;
	newtoold[nnp]=nn;
	oldtonew[nn]=nnp;
	if(nodes[nn].hs!=0) resetn(nodes[nn].hs,head);
	for(unsigned i=0;i<nodes[nn].sons.size();i++)
	{
		if(nodes[nn].sons[i]==nodes[nn].hs) continue;
		resetn(nodes[nn].sons[i],np);
	}
}

struct Lt{
	int l,r;
	int sum;
	int s1,s2;
	int lzadd;
}lts[400010];
int ltp=1;
int gett(int l,int r)
{
	int nltp=ltp++;
	lts[nltp].l=l;
	lts[nltp].r=r;
	if(l==r)
	{
		lts[nltp].sum=nms[newtoold[l]];
	}
	else
	{
		int mid=(l+r)/2;
		lts[nltp].s1=gett(l,mid);
		lts[nltp].s2=gett(mid+1,r);
		lts[nltp].sum=
			lts[lts[nltp].s1].sum+
			lts[lts[nltp].s2].sum;
	}
	return nltp;
}

void down(int p)
{
	if(lts[p].lzadd!=0&&lts[p].l!=lts[p].r)
	{
		lts[lts[p].s1].sum+=lts[p].lzadd;
		lts[lts[p].s2].sum+=lts[p].lzadd;
		lts[lts[p].s1].lzadd+=lts[p].lzadd;
		lts[lts[p].s2].lzadd+=lts[p].lzadd;
	}
	lts[p].lzadd=0;
}

void addtr(int p,int l,int r,int n)
{
	if(lts[p].l==l&&lts[p].r==r) 
	{
		lts[p].sum+=n;
		lts[p].lzadd+=n;
	}
	else
	{
		down(p);
		int mid=(lts[p].l+lts[p].r)/2;
		if(r<=mid)
		{
			addtr(lts[p].s1,l,r,n);
			lts[p].sum=lts[lts[p].s1].sum+lts[lts[p].s2].sum;
		}
		else if(l>=mid+1)
		{
			addtr(lts[p].s2,l,r,n);
			lts[p].sum=lts[lts[p].s1].sum+lts[lts[p].s2].sum;
		}
		else
		{
			addtr(lts[p].s1,l,mid,n);
			addtr(lts[p].s2,mid+1,r,n);
			lts[p].sum=lts[lts[p].s1].sum+lts[lts[p].s2].sum;
		}
	}
}

int cntt(int p,int l,int r)
{
	if(lts[p].l==l&&lts[p].r==r)
	{
		return lts[p].sum;
	}
	else
	{
		down(p);
		int mid=(lts[p].l+lts[p].r)/2;
		if(r<=mid)
		{
			return cntt(lts[p].s1,l,r);
		}
		else if(l>=mid+1)
		{
			return cntt(lts[p].s2,l,r);
		}
		else
		{
			return cntt(lts[p].s1,l,mid)
				+cntt(lts[p].s2,mid+1,r);
		}
	}
}

void addl(int n1,int n2,int n)
{
	int h1=nhead[n1],h2=nhead[n2];
	while(h1!=h2)
	{
		int* upn;
		int* uph;
		if(nodes[newtoold[h1]].dep>nodes[newtoold[h2]].dep)
		{
			upn=&n1;
			uph=&h1;
		}
		else
		{
			upn=&n2;
			uph=&h2;
		}
		addtr(1,*uph,*upn,n);
		*upn=oldtonew[nodes[newtoold[*uph]].fa];
		*uph=nhead[*upn];	
	}
	if(n1<=n2) addtr(1,n1,n2,n);
	else addtr(1,n2,n1,n);
}
int cntl(int n1,int n2)
{
	int ans=0;
	int h1=nhead[n1],h2=nhead[n2];
	while(h1!=h2)
	{
		int* upn;
		int* uph;
		if(nodes[newtoold[h1]].dep>nodes[newtoold[h2]].dep)
		{
			upn=&n1;
			uph=&h1;
		}
		else
		{
			upn=&n2;
			uph=&h2;
		}
		ans+=cntt(1,*uph,*upn);
		ans%=P;
		*upn=oldtonew[nodes[newtoold[*uph]].fa];
		*uph=nhead[*upn];	
	}
	if(n1<=n2) ans+=cntt(1,n1,n2);
	else ans+=cntt(1,n2,n1);
	return ans;
}

signed main()
{
	int n,m,r;
	cin>>n>>m>>r>>P;
	for(int i=1;i<=n;i++) cin>>nms[i];
	int t1,t2;
	for(int i=1;i<n;i++)
	{
		cin>>t1>>t2;
		link[t1].push_back(t2);
		link[t2].push_back(t1);
	}
	gt(r,0,0);
	resetn(r,1);
	gett(1,n);
	int t3,t4;
	for(int i=1;i<=m;i++)
	{
		cin>>t1;
		if(t1==1)
		{
			cin>>t2>>t3>>t4;
			addl(oldtonew[t2],oldtonew[t3],t4);
		}
		if(t1==2)
		{
			cin>>t2>>t3;
			cout<<cntl(oldtonew[t2],oldtonew[t3])%P<<endl;
		}
		if(t1==3)
		{
			cin>>t2>>t3;
			addtr(1,oldtonew[t2],oldtonew[t2]+nodes[t2].sz-1,t3);
		}
		if(t1==4)
		{
			cin>>t2;
			cout<<cntt(1,oldtonew[t2],oldtonew[t2]+nodes[t2].sz-1)%P<<endl;
		}
	}
	return 0;
}
这些是洛谷的提示,不知道有什么问题 /tmp/compiler_jcl0otvg/src:6:24: 错误:‘std::vector link [100010]’ redeclared as different kind of entity 6 | vector link[100010]; | ^ In file included from /nix/store/79624djlfdc0a6anji2rwqd9p9ycqi8h-glibc-2.34-210-dev/include/bits/sigstksz.h:24, from /nix/store/79624djlfdc0a6anji2rwqd9p9ycqi8h-glibc-2.34-210-dev/include/signal.h:328, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/csignal:42, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:43, from /tmp/compiler_jcl0otvg/src:1: /nix/store/79624djlfdc0a6anji2rwqd9p9ycqi8h-glibc-2.34-210-dev/include/unistd.h:819:12: 附注:previous declaration ‘int link(const char*, const char*)’ 819 | extern int link (const char __from, const char __to) | ^~~~ /tmp/compiler_jcl0otvg/src: 在函数‘long long int gt(long long int, long long int, long long int)’中: /tmp/compiler_jcl0otvg/src:20:28: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 20 | for(unsigned i=0;i<link[nn].size();i++) | ^ /tmp/compiler_jcl0otvg/src:20:30: 错误:对成员‘size’的请求出现在‘(link + ((sizetype)nn))’中,而后者具有非类类型‘int(const char, const char*) noexcept’ {aka ‘int(const char*, const char*)’} 20 | for(unsigned i=0;i<link[nn].size();i++) | ^~~~ /tmp/compiler_jcl0otvg/src:22:13: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 22 | if(link[nn][i]==fa) continue; | ^ /tmp/compiler_jcl0otvg/src:22:16: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 22 | if(link[nn][i]==fa) continue; | ^ /tmp/compiler_jcl0otvg/src:22:19: 错误:ISO C++ 不允许比较指针和整数的值 [-fpermissive] 22 | if(link[nn][i]==fa) continue; | ^~ /tmp/compiler_jcl0otvg/src:23:35: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 23 | nodes[nn].sons.push_back(link[nn][i]); | ^ /tmp/compiler_jcl0otvg/src:23:38: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 23 | nodes[nn].sons.push_back(link[nn][i]); | ^ /tmp/compiler_jcl0otvg/src:23:39: 错误:no matching function for call to ‘push_back(int (&)(const char*, const char*) noexcept)’ 23 | nodes[nn].sons.push_back(link[nn][i]); | ^ In file included from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/vector:67, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/queue:61, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:86, from /tmp/compiler_jcl0otvg/src:1: /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1184:7: 附注:candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = long long int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = long long int]’ 1184 | push_back(const value_type& __x) | ^~~~~~~~~ /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1184:7: 附注: conversion of argument 1 would be ill-formed: /tmp/compiler_jcl0otvg/src:23:38: 错误:invalid conversion from ‘int ()(const char, const char*) noexcept’ {aka ‘int ()(const char, const char*)’} to ‘std::vector::value_type’ {aka ‘long long int’} [-fpermissive] 23 | nodes[nn].sons.push_back(link[nn][i]); | ~~~~~~~~~~^ | | | int ()(const char, const char*) noexcept {aka int ()(const char, const char*)} In file included from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/vector:67, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/queue:61, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:86, from /tmp/compiler_jcl0otvg/src:1: /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1200:7: 附注:candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = long long int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = long long int]’ 1200 | push_back(value_type&& __x) | ^~~~~~~~~ /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1200:7: 附注: conversion of argument 1 would be ill-formed: /tmp/compiler_jcl0otvg/src:23:38: 错误:invalid conversion from ‘int ()(const char, const char*) noexcept’ {aka ‘int ()(const char, const char*)’} to ‘std::vector::value_type’ {aka ‘long long int’} [-fpermissive] 23 | nodes[nn].sons.push_back(link[nn][i]); | ~~~~~~~~~~^ | | | int ()(const char, const char*) noexcept {aka int ()(const char, const char*)} /tmp/compiler_jcl0otvg/src:24:21: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 24 | int tmp=gt(link[nn][i],nn,dep+1); | ^ /tmp/compiler_jcl0otvg/src:24:24: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 24 | int tmp=gt(link[nn][i],nn,dep+1); | ^ /tmp/compiler_jcl0otvg/src:24:24: 错误:invalid conversion from ‘int ()(const char, const char*) noexcept’ {aka ‘int ()(const char, const char*)’} to ‘long long int’ [-fpermissive] 24 | int tmp=gt(link[nn][i],nn,dep+1); | ~~~~~~~~~~^ | | | int ()(const char, const char*) noexcept {aka int ()(const char, const char*)} /tmp/compiler_jcl0otvg/src:15:12: 附注: 初始化‘long long int gt(long long int, long long int, long long int)’的实参 1 15 | int gt(int nn,int fa,int dep) | ^ /tmp/compiler_jcl0otvg/src:28:24: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 28 | nodes[nn].hs=link[nn][i]; | ^ /tmp/compiler_jcl0otvg/src:28:27: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 28 | nodes[nn].hs=link[nn][i]; | ^ /tmp/compiler_jcl0otvg/src:28:27: 错误:invalid conversion from ‘int ()(const char, const char*) noexcept’ {aka ‘int ()(const char, const char*)’} to ‘long long int’ [-fpermissive] 28 | nodes[nn].hs=link[nn][i]; | ~~~~~~~~~~^ | | | int ()(const char, const char*) noexcept {aka int ()(const char, const char*)} /tmp/compiler_jcl0otvg/src: 在函数‘int main()’中: /tmp/compiler_jcl0otvg/src:211:10: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 211 | link[t1].push_back(t2); | ^ /tmp/compiler_jcl0otvg/src:211:12: 错误:对成员‘push_back’的请求出现在‘(link + ((sizetype)t1))’中,而后者具有非类类型‘int(const char, const char*) noexcept’ {aka ‘int(const char*, const char*)’} 211 | link[t1].push_back(t2); | ^~~~~~~~~ /tmp/compiler_jcl0otvg/src:212:10: 警告:在算术表达式中使用了函数指针 [-Wpointer-arith] 212 | link[t2].push_back(t1); | ^ /tmp/compiler_jcl0otvg/src:212:12: 错误:对成员‘push_back’的请求出现在‘(link + ((sizetype)t2))’中,而后者具有非类类型‘int(const char, const char*) noexcept’ {aka ‘int(const char*, const char*)’} 212 | link[t2].push_back(t1); | ^~~~~~~~~

回复

4 条回复,欢迎继续交流。

正在加载回复...