QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#331858#6852. Escape The MazeNana7AC ✓2048ms182512kbC++142.8kb2024-02-18 20:38:062024-02-18 20:38:06

Judging History

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

  • [2024-02-18 20:38:06]
  • 评测
  • 测评结果:AC
  • 用时:2048ms
  • 内存:182512kb
  • [2024-02-18 20:38:06]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define I inline
#define ll long long
#define kid tr[k].id
#define lson tr[k].son[0]
#define rson tr[k].son[1]
#define mid (l+r>>1)
#define pii pair<int,ll> 
#define inf 2e15
using namespace std;


const int N = 100010;
const int V = 100010*32;
struct line {
	ll k,b;
	ll f(ll x) {
		return x*k+b;
	}
}L[N];
struct node {
	int id;
	int son[2];
}tr[V<<2];
int dcnt,d[V<<2];
int ncnt;
int n,rt[N];
ll LL,dis[N],f[N];
vector<pii> q[N];

I void del(int k) {
	lson=rson=kid=0;
	d[++dcnt]=k;
}
I int newnode() {
	if(dcnt) return d[dcnt--];
	return ++ncnt;
}
void insert(ll l,ll r,int &k,int id) {
	if(!k) k=newnode();
	if(!kid) {
		kid=id;
		return ;
	}
	if(l==r) {
		if(!kid||L[kid].f(l)>L[id].f(l)) kid=id;
		return ;
	}
	if(L[id].f(mid)<L[kid].f(mid)) swap(id,kid);
	if(L[id].f(l)<L[kid].f(l)) insert(l,mid,lson,id);
	if(L[id].f(r)<L[kid].f(r)) insert(mid+1,r,rson,id);
}
int merge(ll l,ll r,int x,int y) {
	if(!x||!y) return x+y;
	if(tr[y].id) insert(l,r,x,tr[y].id);
	tr[x].son[0]=merge(l,mid,tr[x].son[0],tr[y].son[0]);
	tr[x].son[1]=merge(mid+1,r,tr[x].son[1],tr[y].son[1]);
	del(y);
	return x;
}
ll query(ll l,ll r,ll x,int k) {
	if(!k) return inf;
	if(l==r) return L[kid].f(x);
	if(x<=mid) return min(L[kid].f(x),query(l,mid,x,lson));
	else return min(L[kid].f(x),query(mid+1,r,x,rson));
} 
void dfs(int x,int f) {
	for(int i=0;i<q[x].size();++i) {
		int u=q[x][i].first,v=q[x][i].second;
		if(u==f) continue;
		dis[u]=dis[x]+v;
		dfs(u,x);
	}
}
void dfs1(int x,int fa) {
	bool leaf=1;
	for(int i=0;i<q[x].size();++i) {
		int u=q[x][i].first;
		if(u==fa) continue;
		dfs1(u,x); leaf=0;
		rt[x]=merge(-1e9,1e9,rt[x],rt[u]);
	}
	if(leaf) f[x]=0;
	else f[x]=query(-1e9,1e9,dis[x],rt[x])+LL*LL+dis[x]*dis[x]+2*dis[x]*LL;
	//cout<<x<<' '<<query(-1e9,1e9,dis[x],rt[x])<<endl;
	L[x].k=-2*dis[x];
	L[x].b=f[x]+dis[x]*dis[x]-2*LL*dis[x];
	insert(-1e9,1e9,rt[x],x);
}
I void clear() {
	dcnt=ncnt=0;
	for(int i=1;i<=n;++i) rt[i]=++ncnt,dis[i]=f[i]=0;
	memset(L,0,sizeof(L));
	memset(tr,0,sizeof(tr));
}
I int read() {
	int ret=0,w=1; char ch;
	while((ch=getchar())>'9'||ch<'0'&&ch!='-'); if(ch=='-') w=-1; else ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9') ret=ret*10+ch-'0';
	return ret*w;
}
signed main() 
{
	int T; cin>>T;
	while(T--) {
		cin>>n>>LL;
		memset(q,0,sizeof(q));
		for(int i=1;i<n;++i) {
			int x,y,z; x=read(); y=read(); z=read(); 
			q[x].push_back({y,z});
			q[y].push_back({x,z});
		}
		int Q; cin>>Q;
		for(int i=1;i<=Q;++i) {
			int x; cin>>x;
			clear();	
			dfs(x,0);//for(int j=1;j<=n;++j) cout<<dis[j]<<' '; cout<<endl;
			dfs1(x,0);
			cout<<f[x]<<endl;
		}
	}
}
/*
1
10 3
1 2 7
2 7 6
2 4 -2
4 6 4
1 3 1
3 5 5
5 8 -3
5 10 0
5 9 2
1 6
*/

详细

Test #1:

score: 100
Accepted
time: 2048ms
memory: 182512kb

input:

5
6 -23131
2 1 -65179
3 2 -4529
4 3 869
5 4 -43646
6 3 72319
6
1
2
3
4
5
6
100000 -89702
2 1 90843
3 2 -51373
4 3 -37208
5 4 50738
6 4 -56753
7 2 -22185
8 6 -2522
9 5 6214
10 6 51682
11 2 97227
12 11 -72521
13 3 20844
14 9 -63596
15 1 -811
16 1 -63314
17 14 33366
18 11 -13974
19 5 82109
20 10 -35290...

output:

662650564
584430625
385965316
420865225
2352464929
662650564
0
169
1
36
169
1
205
324
576
1
25
4
2401
441
400
10201
361
1600
36
5184
4
1
4611360649
177795556
0
0
1
834747664
1387786009
0
4356
6561
410
361
1849
100
6889
16
1296
81

result:

ok 46 lines