QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72514#5148. Tree DistanceJinTianHaoTL 2ms16256kbC++172.9kb2023-01-16 14:05:492023-01-16 14:05:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-16 14:05:56]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:16256kb
  • [2023-01-16 14:05:49]
  • 提交

answer

#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
#define lowbit(x) (x&(-x))
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n,q,rt,all,len,tot;
int h[200005],e[400005],w[400005],ne[400005],idx;
int siz[200005],maxp[200005],id[200005];
long long dist[200005],bit[200005],ans[1000005];
bool vis[200005];
struct oper
{
	int l,r;
	long long v;
	bool operator<(const oper &o) const
	{
		return (l^o.l?l>o.l:v>o.v);
	}
}opr[2000005];
void modify(int x,long long y)
{
	while(x<=n)
	{
		bit[x]=min(bit[x],y);
		x+=lowbit(x);
	}
}
long long query(int x)
{
	long long res=1ll*inf*inf;
	while(x)
	{
		res=min(res,bit[x]);
		x-=lowbit(x);
	}
	return res;
}
void add(int a,int b,int c)
{
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void getrt(int x,int fa)
{
	siz[x]=1;maxp[x]=0;
	for(int i=h[x];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa||vis[j]) continue;
		getrt(j,x);siz[x]+=siz[j];
		maxp[x]=max(maxp[x],siz[j]);
	}
	maxp[x]=max(maxp[x],all-siz[x]);
	if(maxp[rt]>maxp[x]) rt=x;
}
void dfs(int x,int fa)
{
	id[++len]=x;
	for(int i=h[x];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa||vis[j]) continue;
		dist[j]=dist[x]+w[i];dfs(j,x);
	}
}
void solve(int x)
{
	dist[x]=0;id[len=1]=x;
	for(int i=h[x];~i;i=ne[i])
	{
		int j=e[i];
		if(vis[j]) continue;
		dist[j]=w[i];dfs(j,x);
	}
	sort(id+1,id+len+1,[](int x,int y){return dist[x]<dist[y];});
	set<int> st;
	for(int i=1;i<=len;++i)
	{
		auto it=st.lower_bound(id[i]);
		if(it!=st.end()) opr[++tot]={id[i],*it,dist[id[i]]+dist[*it]};
		if(it!=st.begin()) --it,opr[++tot]={*it,id[i],dist[*it]+dist[id[i]]};
		st.insert(id[i]);
	}
}
void divide(int x)
{
	vis[x]=1;
	solve(x);
	for(int i=h[x];~i;i=ne[i])
	{
		int j=e[i];
		if(vis[j]) continue;
		maxp[rt=0]=all=siz[j];
		getrt(j,0);getrt(rt,0);
		divide(rt);
	}
}
signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	read(n);memset(h,-1,sizeof(h));
	for(int i=1;i<n;++i)
	{
		int a,b,c;
		read(a);read(b);read(c);
		add(a,b,c);add(b,a,c);
	}
	maxp[rt=0]=all=n;
	getrt(1,0);getrt(rt,0);
	divide(rt);
	read(q);
	for(int i=1,l,r;i<=q;++i)
	{
		read(l);read(r);
		opr[++tot]={l,r,-i};
	}
	sort(opr+1,opr+tot+1);memset(bit,0x3f,sizeof(bit));
	for(int i=1;i<=tot;++i)
		if(opr[i].v>0) modify(opr[i].r,opr[i].v);
		else ans[-opr[i].v]=query(opr[i].r);
	for(int i=1;i<=q;++i)
		writeln(ans[i]==1ll*inf*inf?-1:ans[i]);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 16256kb

input:

5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Time Limit Exceeded

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:


result: