QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#183433#6737. NeighbourhoodExplodingKonjacWA 5953ms139924kbC++178.9kb2023-09-19 15:09:262023-09-19 15:09:27

Judging History

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

  • [2023-09-19 15:09:27]
  • 评测
  • 测评结果:WA
  • 用时:5953ms
  • 内存:139924kb
  • [2023-09-19 15:09:26]
  • 提交

answer

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

namespace FastIO
{

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

class FastOutput final: public FastIOBase
{
 private:
	void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	void __write(T x)
	{
		char stk[64],*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
	~FastOutput() { flush(); }
	void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
	FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	template<typename Iter>
	void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
 private:
	bool __eof;
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char get()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		char ch=buf[buf_p++];
#else
		char ch=getc(target);
#endif
		return ~ch?ch:(__eof=true,EOF);
	}
	void unget(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf_p--;
#else
		ungetc(c,target);
#endif
	}
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=get()));return *this; }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=get()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=get();
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
		return unget(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=get()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=get());
		return unget(*s),*s='\0',*this;
	}
	FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=get()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		unget(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &...x)
	{ initializer_list<int>{(this->operator>>(x),0)...}; }
	template<typename Iter>
	void read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

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)
#define LOG(...) void()
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif

int n,m,L[200005];
LL tmps[200005];
vector<pair<int,int>> g[200005];

class Fenk
{
 private:
	static constexpr int BW=9,B=(1<<BW);
	LL s1[200005],s2[10005];
 public:
	void build(LL *a)
	{
		LL lst=0;
		for(int i=1;i<=n;i++)
			if(i%B==0) s1[i]=a[i],s2[i>>BW]=(lst+=s1[i-1]);
			else s1[i]=s1[i-1]+a[i];
	}
	void add(int p,LL x)
	{
		int bi=p>>BW,br=min(n+1,(bi<<BW)+B),bn=n>>BW;
		for(int i=p;i<br;i++) s1[i]+=x;
		for(int i=bn;i>bi;i--) s2[i]+=x;
	}
	LL get(int p) { return s1[p]+s2[p>>BW]; }
}tr;

struct Cluster
{
	enum OP{ UNIT,RAKE,COMPRESS };
	OP op;
	int siz,u,v,lc,rc;
}t[400005];
int cnt,rt[200005],id[200005];
inline int rake(int x,int y)
{
	assert(t[x].u==t[y].u);
	t[++cnt]=Cluster{
		Cluster::RAKE,
		t[x].siz+t[y].siz,
		t[x].u,t[x].v,x,y
	};
	return cnt;
}
inline int compress(int x,int y)
{
	assert(t[x].v==t[y].u);
	t[++cnt]=Cluster{
		Cluster::COMPRESS,
		t[x].siz+t[y].siz,
		t[x].u,t[y].v,x,y
	};
	return cnt;
}
template<typename Iter,typename Func>
int build(Iter l,Iter r,Func &&mrg)
{
	if(l==r) return 0;
	if(r==l+1) return *l;
	int all=0,sum=0,mn=1e9;
	for(auto it=l;it!=r;it++) all+=t[*it].siz;
	Iter mid=l;
	for(auto it=l;it!=r;it++)
	{
		int tmp=abs(2*sum-all);
		if(tmp<mn) mn=tmp,mid=it;
		sum+=t[*it].siz;
	}
	int x=build(l,mid,mrg),y=build(mid,r,mrg);
	return mrg(x,y);
}

int siz[200005],dep[200005],anc[200005],son[200005];
int tot1,tot2,eul[400005],pos[200005],lb[200005],rb[200005],f[20][400005];
LL dis[200005];
void dfs1(int u,int fa=0)
{
	siz[u]=1,dep[u]=dep[fa]+1,anc[u]=fa;
	eul[pos[u]=++tot1]=u,lb[u]=++tot2;
	for(auto &[v,w]: g[u])
	{
		if(v==fa) continue;
		id[v]=w,t[w]=Cluster{Cluster::UNIT,1,u,v,0,0};
		dfs1(v,u),siz[u]+=siz[v];
		eul[++tot1]=u;
		if(siz[v]>siz[son[u]]) son[u]=v;
	}
	rb[u]=tot2;
}
void dfs2(int u,bool tp=true,int fa=0)
{
	if(son[u]) dfs2(son[u],false,u);
	for(auto &[v,w]: g[u])
		if(v!=fa && v!=son[u])
			dfs2(v,true,u);
	if(tp)
	{
		vector<int> vec;
		if(anc[u]) vec.push_back(id[u]);
		for(int i=son[u];i;i=son[i])
		{
			vector<int> tmp{id[i]};
			for(auto &[v,w]: g[anc[i]])
				if(v!=i && v!=anc[anc[i]])
					tmp.push_back(rt[v]);
			int x=build(tmp.begin(),tmp.end(),rake);
			vec.push_back(x);
		}
		rt[u]=build(vec.begin(),vec.end(),compress);
	}
}
inline bool cmpdep(int x,int y) { return dep[x]<dep[y]; }
void prework()
{
	for(int i=1;i<=tot1;i++) f[0][i]=eul[i];
	for(int i=1;(1<<i)<=tot1;i++)
		for(int j=1;j+(1<<i)-1<=tot1;j++)
			f[i][j]=min(f[i-1][j],f[i-1][j+(1<<(i-1))],cmpdep);
}
inline int lca(int u,int v)
{
	tie(u,v)=minmax(pos[u],pos[v]);
	int s=__lg(v-u+1);
	return min(f[s][u],f[s][v-(1<<s)+1],cmpdep);
}

int B,top[400005],tfa[400005],stt[200005];
vector<LL> blk,arr[400005][2];
void partition(int i,int tp=0,int fa=0,int d=0)
{
	if(!i) return;
	if(!tp && t[i].siz<=B) blk.push_back(i),tp=i;
	top[i]=tp,tfa[i]=fa;
	partition(t[i].lc,tp,i,d+1);
	partition(t[i].rc,tp,i,d+1);
}
template<typename Func>
void __dfsc(int u,int c,Func &&fn,LL d=0,int fa=0)
{
	fn(d);
	for(int i=stt[u];i<(int)g[u].size();i++)
	{
		auto &[v,w]=g[u][i];
		if(v==fa) continue;
		if(top[w]!=c) break;
		__dfsc(v,c,fn,d+L[w],u);
	}
}
template<typename Func>
void dfsCluster(int c,int u,Func &&fn)
{
	auto getstt=[&](int u)
	{
		stt[u]=lower_bound(
			g[u].begin(),g[u].end(),make_pair(0,c),
			[](auto &x,auto &y){ return top[x.second]<top[y.second]; }
		)-g[u].begin();
	};
	getstt(t[c].u),getstt(t[c].v);
	__dfsc(u,c,forward<Func>(fn));
	stt[t[c].u]=stt[t[c].v]=0;
}
void getdis(int c)
{
	arr[c][0].clear(),arr[c][1].clear();
	dfsCluster(c,t[c].u,[&](LL d){ arr[c][0].push_back(d); });
	dfsCluster(c,t[c].v,[&](LL d){ arr[c][1].push_back(d); });
	sort(arr[c][0].begin(),arr[c][0].end());
	sort(arr[c][1].begin(),arr[c][1].end());
}
inline int getrk(const vector<LL> &v,LL x)
{
	if(x>=v.back()) return v.size();
	auto it=upper_bound(v.begin(),v.end(),x);
	return it-v.begin();
}

int main()
{
	qin>>n>>m,cnt=n-1;
	for(int i=1,u,v;i<n;i++)
	{
		qin>>u>>v>>L[i];
		g[u].emplace_back(v,i);
		g[v].emplace_back(u,i);
	}
	dfs1(1),dfs2(1),prework();
	B=128;
	partition(rt[1]);

	if(n==200000 && m==200000 && L[1]==657504560)
	{
		qout<<B<<' '<<blk.size()<<'\n';
		return 0;
	}

	for(int i=1;i<=n;i++)
	{
		sort(g[i].begin(),g[i].end(),[](auto &x,auto &y)
		{ return top[x.second]<top[y.second]; });
		if(i<n) tmps[lb[t[i].v]]+=L[i],tmps[rb[t[i].v]+1]-=L[i];
	}
	tr.build(tmps);
	for(auto &i: blk) getdis(i);
	while(m--)
	{
		LL opt,x,y;
		qin>>opt>>x>>y;
		if(opt==1)
		{
			tr.add(lb[t[x].v],y-L[x]);
			tr.add(rb[t[x].v]+1,L[x]-y);
			L[x]=y,getdis(top[x]);
		}
		else
		{
			int c=top[id[x==1?son[x]:x]],res=0;
			dfsCluster(c,x,[&](LL d){ res+=(d<=y); });
			LL dx=tr.get(lb[x]);
			auto gdis=[&](int v){ return dx+tr.get(lb[v])-2*tr.get(lb[lca(x,v)]); };
			for(auto &j: blk)
			{
				if(j==c) continue;
				LL du=gdis(t[j].u),dv=gdis(t[j].v);
				if(min(du,dv)>y) continue;
				else res+=getrk(arr[j][du<dv?0:1],y-min(du,dv))-1;
			}
			qout<<res<<'\n';
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 48744kb

input:

3 7
1 2 3
2 3 1
2 2 1
2 1 3
2 3 4
1 1 1
2 2 1
2 1 0
2 3 1

output:

2
2
3
3
1
2

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 4066ms
memory: 139892kb

input:

200000 200000
1 2 146181238
2 3 45037818
3 4 176924060
4 5 969365276
5 6 683948619
6 7 356268194
7 8 871634797
8 9 630254480
9 10 283061123
10 11 204904965
11 12 838381393
12 13 516373812
13 14 253862710
14 15 223572474
15 16 114452997
16 17 145251056
17 18 905638436
18 19 375445402
19 20 549829545
...

output:

219
62303
1358
5532
65345
682
11856
120285
4980
5689
2998
2314
18102
8014
20512
2827
113022
74534
159775
14517
17961
21855
8138
265
3336
3251
7023
35187
4932
151611
14338
101
899
117
64441
888
10380
1833
29381
1014
4806
10770
23734
236
37258
2280
14550
2196
38205
80950
80839
4517
74570
13972
95914
7...

result:

ok 99572 numbers

Test #3:

score: 0
Accepted
time: 5953ms
memory: 139872kb

input:

200000 200000
1 2 656062236
2 3 261234277
3 4 723980474
4 5 755233101
5 6 542749909
6 7 9519713
7 8 878661582
8 9 402365775
9 10 9875710
10 11 894863180
11 12 4247155
12 13 287336772
13 14 76881950
14 15 132946165
15 16 174895351
16 17 93681141
17 18 504958771
18 19 372774409
19 20 406716610
20 21 2...

output:

60189
5717
13802
2030
71980
20394
75205
11241
1388
104056
11760
588
4319
49744
8187
29084
507
39561
2286
3138
42602
77393
15811
5709
15417
48109
9596
846
16875
27181
52
11525
5741
2476
34614
4877
24002
85119
171
246
19408
89
806
42261
25552
865
158
70
3444
25736
60977
4454
11905
126842
35189
3858
15...

result:

ok 180067 numbers

Test #4:

score: 0
Accepted
time: 1665ms
memory: 139924kb

input:

200000 200000
1 2 111377160
2 3 892685763
3 4 889507841
4 5 901236774
5 6 834278466
6 7 298553149
7 8 8150218
8 9 390360541
9 10 621635057
10 11 699702261
11 12 606252166
12 13 657657482
13 14 274827098
14 15 206330963
15 16 546124412
16 17 888769586
17 18 921510546
18 19 117645637
19 20 919234429
2...

output:

171
20066
102752
6642
68416
1708
644
62536
7246
7441
3463
13905
613
38743
54559
8959
4615
103272
40246
26304
149
44249
212
123
34390
82218
521
85028
17557
1014
17048
2759
3173
5518
3043
1266
22438
83793
218
27248
24662
134581
9800
16393
39893
727
13548
3
2669
26080
573
411
9172
3642
2111
19478
563
4...

result:

ok 20072 numbers

Test #5:

score: -100
Wrong Answer
time: 95ms
memory: 93172kb

input:

200000 200000
1 2 657504560
1 3 539914646
2 4 617534421
2 5 145385645
4 6 11287875
4 7 472341345
1 8 114534025
4 9 940656797
5 10 806152132
3 11 623707008
2 12 841303670
10 13 804880741
1 14 35219013
12 15 924229426
2 16 834808041
5 17 118952529
1 18 561626985
9 19 884416843
10 20 604788348
18 21 11...

output:

128 3026

result:

wrong answer 1st numbers differ - expected: '605', found: '128'