QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87351#5439. Meet in the MiddleExplodingKonjacWA 42ms21616kbC++176.6kb2023-03-12 18:00:022023-03-12 18:00:03

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-12 18:00:03]
  • 评测
  • 测评结果:WA
  • 用时:42ms
  • 内存:21616kb
  • [2023-03-12 18:00:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
//#define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<22;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
};

class FastOutput: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
		{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 protected:
	inline void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE)flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	inline void __write(T x)
	{
		static char stk[64],*top;top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	inline void setTarget(FILE *f) { this->flush(),target=f; }
	~FastOutput(){ flush(); }
#else
	inline void setTarget(FILE *f) { target=f; }
#endif
	template<typename ...T>
	inline void writesp(const T &...x)
		{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	inline void writeln(const T &...x)
		{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	inline FastOutput &operator <<(char x)
		{ return __putc(x),*this; }
	inline FastOutput &operator <<(const char *s)
		{ for(;*s;__putc(*(s++)));return *this; }
	inline FastOutput &operator <<(const string &s)
		{ return (*this)<<s.c_str(); }
	template<typename T,typename=typename enable_if<is_integral<T>::value>::type>
	inline FastOutput &operator <<(const T &x)
		{ return __write(x),*this; }
}qout;

class FastInput: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
		{ buf[fread(buf,1,BUFSIZE,target)]='\0',buf_p=0; }
#endif
 protected:
	inline char __getc()
	{
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		return buf[buf_p++];
#else
		return getc(target);
#endif
	}
 public:
#ifdef OPENIOBUF
	FastInput(FILE *f=stdin): FastIOBase(f){ buf_p=BUFSIZE; }
	inline void setTarget(FILE *f) { this->flush(),target=f; }
#else
	FastInput(FILE *f=stdin): FastIOBase(f){}
	inline void setTarget(FILE *f) { target=f; }
#endif
	inline char getchar() { return __getc(); }
	template<typename ...T>
	inline void read(T &...x)
		{ initializer_list<int>{(this->operator>>(x),0)...}; }
	inline FastInput &operator >>(char &x)
		{ while(isspace(x=__getc()));return *this; }
	template<typename T,typename=typename enable_if<is_integral<T>::value>::type>
	inline FastInput &operator >>(T &x)
	{
		static char ch,sym;x=sym=0;
		while(isspace(ch=__getc()));
		if(ch=='-') sym=1,ch=__getc();
		for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return sym?x=-x:x,*this;
	}
	inline FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		for(;!isspace(*s) && *s && ~*s;*(++s)=__getc());
		return *s='\0',*this;
	}
	inline FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1],*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && *p && ~*p;p++);
			*p='\0',s.append(str_buf);
			if(p!=buf_end) break;
		}
		return *this;
	}
}qin;

} // namespace FastIO
using namespace FastIO;

using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif

int n,q;
vector<pair<int,int>> g1[100005],g2[100005];
LL d1[100005],d2[100005];
int dfn,lb[100005],rb[100005],ord[100005];
int nn,dep[100005],pos[100005],eul[200005],f[200005][20];

void dfs1(int u,int fa=0)
{
	ord[++dfn]=u,lb[u]=dfn;
	for(auto &[v,w]: g1[u])
	{
		if(v==fa) continue;
		d1[v]=d1[u]+w;
		dfs1(v,u);
	}
	rb[u]=dfn;
}
void dfs2(int u,int fa=0)
{
	dep[u]=dep[fa]+1;
	eul[pos[u]=++nn]=u;
	for(auto &[v,w]: g2[u])
	{
		if(v==fa) continue;
		d2[v]=d2[u]+w;
		dfs2(v,u),eul[++nn]=u;
	}
}
inline bool cmpd(int x,int y) { return dep[x]<dep[y]; }
void prework()
{
	for(int i=1;i<=nn;i++) f[i][0]=eul[i];
	for(int j=1;(1<<j)<=nn;j++)
		for(int i=1;i+(1<<j)-1<=nn;i++)
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1],cmpd);
}
inline int lca(int u,int v)
{
	tie(u,v)=minmax(pos[u],pos[v]);
	int s=__lg(v-u+1);
	return min(f[u][s],f[v-(1<<s)+1][s],cmpd);
}
inline LL dis(int u,int v)
{
	int l=lca(u,v);
	return d2[u]+d2[v]-2*d2[l];
}

struct TreeNode{ array<LL,2> u,w;LL mx,tag; }t[400005];
#define LC (i<<1)
#define RC (i<<1|1)
inline void pushTag(int i,LL x)
{
	t[i].tag+=x;
	t[i].mx+=(x<<1);
	t[i].w[0]+=x,t[i].w[1]+=x;
}
inline void pushdown(int i)
{
	if(!t[i].tag) return;
	pushTag(LC,t[i].tag);
	pushTag(RC,t[i].tag);
	t[i].tag=0;
}
inline void pushup(int i)
{
	if(t[LC].mx>t[RC].mx) t[i].u=t[LC].u,t[i].w=t[LC].w,t[i].mx=t[LC].mx;
	else t[i].u=t[RC].u,t[i].w=t[RC].w,t[i].mx=t[RC].mx;
	for(int k1: {0,1}) for(int k2: {0,1})
	{
		int tmp=dis(t[LC].u[k1],t[RC].u[k2])+t[LC].w[k1]+t[RC].w[k2];
		if(tmp<=t[i].mx) continue;
		t[i].mx=tmp;
		t[i].u={t[LC].u[k1],t[RC].u[k2]};
		t[i].w={t[LC].w[k1],t[RC].w[k2]};
	}
}
void build(int l,int r,int i=1)
{
	if(l==r)
	{
		int x=ord[l];
		t[i].u={x,x},t[i].mx=d1[x]<<1;
		t[i].w={d1[x],d1[x]};
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,LC),build(mid+1,r,RC);
	pushup(i);
}
void modify(int lq,int rq,LL x,int i=1,int l=1,int r=n)
{
	if(l>=lq && r<=rq) return pushTag(i,x);
	int mid=(l+r)>>1;
	pushdown(i);
	if(mid>=lq) modify(lq,rq,x,LC,l,mid);
	if(mid<rq) modify(lq,rq,x,RC,mid+1,r);
	pushup(i);
}

vector<pair<int,int>> ask[100005];
LL ans[500005];
void solve(int u,int fa=0)
{
	for(auto &[v,id]: ask[u])
		ans[id]=max(dis(t[1].u[0],v)+t[1].w[0],
					dis(t[1].u[1],v)+t[1].w[1]);
	for(auto &[v,w]: g1[u])
	{
		if(v==fa) continue;
		modify(lb[v],rb[v],-w);
		if(lb[v]>1) modify(1,lb[v]-1,w);
		if(rb[v]<n) modify(rb[v]+1,n,w);
		solve(v,u);
		modify(lb[v],rb[v],w);
		if(lb[v]>1) modify(1,lb[v]-1,-w);
		if(rb[v]<n) modify(rb[v]+1,n,-w);
	}
}
int main()
{
	qin>>n>>q;
	for(int i=1,u,v,w;i<n;i++)
	{
		qin>>u>>v>>w;
		g1[u].emplace_back(v,w);
		g1[v].emplace_back(u,w);
	}
	for(int i=1,u,v,w;i<n;i++)
	{
		qin>>u>>v>>w;
		g2[u].emplace_back(v,w);
		g2[v].emplace_back(u,w);
	}
	dfs1(1),dfs2(1);
	prework(),build(1,n);
	for(int i=1,u,v;i<=q;i++)
	{
		qin>>u>>v;
		ask[u].emplace_back(v,i);
	}
	solve(1);
	for(int i=1;i<=q;i++) qout<<ans[i]<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 20008kb

input:

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

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 4ms
memory: 16864kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 2ms
memory: 17556kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 42ms
memory: 21616kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

399548131845
537786791830
425520940358
426520155719
383793378250
435296678541
367399012173
484851503610
555328574124
715122450120
378790217673
646011045570
690958458520
709874036595
308123232687
409592755604
556469362593
393969914729
390941510555
638058436814
599229887199
578008990616
602564678827
4...

result:

wrong answer 1st numbers differ - expected: '647838384844', found: '399548131845'