QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#185097#6737. NeighbourhoodbulijiojiodibuliduoTL 0ms30700kbC++2.6kb2023-09-21 17:05:462023-09-21 17:05:46

Judging History

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

  • [2023-09-21 17:05:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:30700kb
  • [2023-09-21 17:05:46]
  • 提交

answer

#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;
	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,5>> 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) {
		cs[u].pb({x,0,tot-1,l[u],1});
		if (br[u]!=-1) cs[u].pb({x,l[br[u]],r[br[u]],l[u],-1});
	}
	for (auto [v,id]:e[x]) if (!vis[v]) {
		solve(v);
	}
}

int main() {
	scanf("%d%d",&n,&Q);
	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,sgn]:cs[x]) {
				ll v=ds[u].queryp(pos);
				ans+=sgn*ds[u].query(l,r,d-v);
			}
			printf("%d\n",ans);
		}
	}
}

详细

Test #1:

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

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

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: