QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#331636 | #6852. Escape The Maze | Nana7 | WA | 982ms | 40024kb | C++14 | 2.7kb | 2024-02-18 16:12:24 | 2024-02-18 16:12:24 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define I inline
#define int 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,int>
#define inf 2e9
using namespace std;
const int N = 100010;
const int V = 100010*32*2;
struct line {
int k,b;
int f(int x) {
return x*k+b;
}
}L[N];
struct node {
int id;
int son[2];
}tr[N<<2];
int dcnt,d[N];
int ncnt;
int n,LL,dis[N],rt[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(int l,int 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(int l,int r,int x,int y) {
if(!x||!y) return x+y;
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(l,mid,tr[x].son[1],tr[y].son[1]);
del(y);
return x;
}
int query(int l,int r,int 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(mid+1,r,x,rson));
else return min(L[kid].f(x),query(l,mid,x,lson));
}
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(-5e9,5e9,rt[x],rt[u]);
}
if(leaf) f[x]=0;
else f[x]=query(-5e9,5e9,dis[x],rt[x])+LL*LL+dis[x]*dis[x]+2*dis[x]*LL;
L[x].k=-2*dis[x];
L[x].b=f[x]+dis[x]*dis[x]-2*LL*dis[x];
insert(-5e9,5e9,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;
}
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 982ms
memory: 40024kb
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 441 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 8281 361 1849 100 6889 16 1296 81
result:
wrong answer 13th lines differ - expected: '205', found: '441'