QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#185115#6737. NeighbourhoodbulijiojiodibuliduoTL 8131ms245772kbC++2.9kb2023-09-21 17:20:132023-09-21 17:20:13

Judging History

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

  • [2023-09-21 17:20:13]
  • 评测
  • 测评结果:TL
  • 用时:8131ms
  • 内存:245772kb
  • [2023-09-21 17:20:13]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;
vector<array<int,2>> e[N];
int wt[N];

int q[N],f[N],vis[N],sz[N],ms[N];
int find(int u) {
	int t=1;q[0]=u;f[u]=-1;
	rep(i,0,t) {
		u=q[i];
		rep(j,0,e[u].size()) {
			int v=e[u][j][0];
			if (!vis[v]&&v!=f[u]) f[q[t++]=v]=u;
		}
		ms[q[i]]=0;
		sz[q[i]]=1;
	}
	for (int i=t-1;i>=0;i--) {
		ms[q[i]]=max(ms[q[i]],t-sz[q[i]]);
		if (ms[q[i]]*2<=t) return q[i];
		sz[f[q[i]]]+=sz[q[i]];
		ms[f[q[i]]]=max(ms[f[q[i]]],sz[q[i]]);
	}
	return 0;
}

struct dst {
	vector<ll> a;
	int bsz,n;
	void init(vector<ll> dp) {
		a=dp;
	}
	void add(int l,int r,ll x) {
		rep(i,l,r+1) a[i]+=x;
	}
	int query(int l,int r,ll d) {
		int ans=0;
		rep(i,l,r+1) ans+=a[i]<=d;
		return ans;
	}
	ll queryp(int x) {
		return a[x];
	}
}ds[N];
int n,Q;
vector<array<int,3>> ps[N];
vector<array<int,4>> cs[N];

void solve(int x) {
	x=find(x);
	vis[x]=1;
	int tot=0;
	static int l[N],r[N],br[N];
	VI pv;
	vector<ll> dp;
	function<void(int,int,int,ll)> dfs=[&](int u,int f,int b,ll dep) {
		l[u]=tot++;
		dp.pb(dep);
		pv.pb(u);
		br[u]=b;
		for (auto [v,id]:e[u]) if (v!=f&&!vis[v]) {
			dfs(v,u,b==-1?v:b,dep+wt[id]);
			ps[id].pb({x,l[v],r[v]});
		}
		r[u]=tot-1;
	};
	dfs(x,-1,-1,0);
	ds[x].init(dp);
	for (auto u:pv) {
		if (br[u]==-1) {
			cs[u].pb({x,0,tot-1,l[u]});
		} else {
			if (l[br[u]]-1>=0) {
				cs[u].pb({x,0,l[br[u]]-1,l[u]});
			}
			if (r[br[u]]+1<tot) {
				cs[u].pb({x,r[br[u]]+1,tot-1,l[u]});
			}
		}
	}
	for (auto [v,id]:e[x]) if (!vis[v]) {
		solve(v);
	}
}

int main() {
	scanf("%d%d",&n,&Q);
	//Q=min(Q,100000);
	rep(i,1,n) {
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		e[u].pb({v,i});
		e[v].pb({u,i});
		wt[i]=w;
	}
	solve(1);
	rep(i,0,Q) {
		int op;
		scanf("%d",&op);
		if (op==1) {
			int id,c;
			scanf("%d%d",&id,&c);
			c-=wt[id]; wt[id]+=c;
			for (auto [x,l,r]:ps[id]) {
				ds[x].add(l,r,c);
			}
		} else {
			int x; ll d;
			scanf("%d%lld",&x,&d);
			int ans=0;
			for (auto [u,l,r,pos]:cs[x]) {
				ll v=ds[u].queryp(pos);
				ans+=ds[u].query(l,r,d-v);
			}
			printf("%d\n",ans);
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 8131ms
memory: 245772kb

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: -100
Time Limit Exceeded

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: