QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72291#5148. Tree Distancepaul2008TL 1ms27736kbC++142.8kb2023-01-15 12:34:282023-01-15 12:35:05

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-15 12:35:05]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:27736kb
  • [2023-01-15 12:34:28]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
using namespace std;

const int N=2e5+5;

#define IT set<int>::iterator

int GC=0,Max_sz=0;
bool del[N];
vector <int> g[N],w[N];

int get_sz(int x,int fa)
{
	if(del[x])
		return 0;

	int ans=1;
	for(int i=0;i<g[x].size();i++)
	{
		int to=g[x][i];
		if(to!=fa)
			ans += get_sz(to,x);
	}
	return ans;
}

int dfs(int x,int fa,int tot) //返回子树sz
{
	if(del[x])
		return 0;

	int sum=1,Max=0;
	for(int i=0;i<g[x].size();i++)
	{
		int to=g[x][i];
		if(to!=fa)
		{
			int sz=dfs(to,x,tot);
			sum += sz;
			Max=max(Max,sz);
		}
	}
	Max=max(Max,tot-sum);

	if(Max<Max_sz)
		Max_sz=Max,GC=x;
	return sum;
}

int get_GC(int x)
{
	GC=0,Max_sz=0x3f3f3f3f;
	dfs(x,-1,get_sz(x,-1));
	return GC;
}

int cnt=0;
pair<long long,int> dist[N];
vector <pair <int,long long> > upd[N];
vector <pair <int,int> > que[N];
long long val[N],ans[N];

void get_dist(int x,int fa,long long nowdist)
{
	if(del[x])
		return;

	dist[++cnt]=make_pair(nowdist,x),val[x]=nowdist;
	for(int i=0;i<g[x].size();i++)
	{
		int to=g[x][i];
		if(to!=fa)
			get_dist(to,x,nowdist+w[x][i]);
	}
}

void getans(int x)
{
	if(del[x])
		return;

	x=get_GC(x),cnt=0;
	get_dist(x,-1,0);
	del[x]=true;

	sort(dist+1,dist+cnt+1);

	set <int> s;
	for(int i=1;i<=cnt;i++)
	{
		IT it=s.lower_bound(dist[i].second);
		if(it!=s.begin())
		{
			--it;
			int x=*it,y=dist[i].second;
			if(x>y)
				swap(x,y);
			upd[y].push_back(make_pair(x,val[x]+val[y]));
		}

		it=s.upper_bound(dist[i].second);
		if(it!=s.end())
		{
			int x=*it,y=dist[i].second;
			if(x>y)
				swap(x,y);
			upd[y].push_back(make_pair(x,val[x]+val[y]));
		}
		s.insert(dist[i].second);
	}

	for(int i=0;i<g[x].size();i++)
		getans(g[x][i]);
}

long long c[N];
int n;

void update(int x,long long y)
{
	for(int i=x;i;i-=i&-i)
		c[i]=min(c[i],y);
}

long long query(int x)
{
	long long ans=0x3f3f3f3f3f3f3f3f;
	for(int i=x;i<=n;i+=i&-i)
		ans=min(ans,c[i]);
	return ans;
}

int main()
{
	cin >> n;
	for(int i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%d %d %d",&x,&y,&z);
		g[x].push_back(y);
		g[y].push_back(x);
		w[x].push_back(z);
		w[y].push_back(z);
	}

	getans(1);

	int T;
	cin >> T;
	for(int i=1;i<=T;i++)
	{
		int l,r;
		scanf("%d %d",&l,&r);
		que[r].push_back(make_pair(l,i));
	}

	memset(c,0x3f,sizeof(c));
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<upd[i].size();j++)
			update(upd[i][j].first,upd[i][j].second);

		for(int j=0;j<que[i].size();j++)
			ans[que[i][j].second]=query(que[i][j].first);
	}

	for(int i=1;i<=T;i++)
	{
		if(ans[i]==0x3f3f3f3f3f3f3f3f)
			printf("-1\n");
		else
			printf("%lld\n",ans[i]);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 27736kb

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: