답안 #433969

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433969 2021-06-20T12:53:36 Z AmineWeslati Traffickers (RMI18_traffickers) C++14
15 / 100
3500 ms 11004 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)

typedef string str; 

typedef pair<int,int>pi;
#define fi first
#define se second
typedef vector<pi>vpi;
#define eb emplace_back

#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)


void ckmin(int &x, int y){x=min(x,y);}
//------------------------------------

#define int ll

const int MX=1e5;

int N; 
vi adj[MX];
multiset<pi> vec[MX];

vi par(MX);
void dfs(int u, int p, int dest){
	//if(u==dest) return; 
	for(int v: adj[u]) if(v!=p){
		par[v]=u;
		dfs(v,u,dest);
	}

}

void add(int u, int v){
	dfs(u,u,v);

	int cur=v,n=0;

	vi cy; 
	while(1){
		n++;
		cy.pb(cur);
		if(cur==u) break;
		cur=par[cur];
	}
	reverse(all(cy));

	FOR(i,0,n){
		vec[cy[i]].insert({n,i});
	}
}

void del(int u, int v){
	dfs(u,u,v);

	int cur=v,n=0;

	vi cy; 
	while(1){
		n++;
		cy.pb(cur);
		if(cur==u) break;
		cur=par[cur];
	}
	reverse(all(cy));

	FOR(i,0,n){
		vec[cy[i]].erase(vec[cy[i]].find({n,i}));
	}
}

int32_t main(){

	cin>>N; 
	FOR(i,0,N-1){
		int u,v; cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	int K; cin>>K;
	FOR(i,0,K){
		int u,v; cin>>u>>v;
		add(u,v);
	}

	/*FOR(i,1,N+1){
		for(auto x: vec[i]) cout << x.fi << ' ' << x.se << endl;
		cout << endl;
	}*/

	int Q; cin>>Q;
	while(Q--){
		int ty; cin>>ty;
		if(ty==1){
			int u,v; cin>>u>>v;
			add(u,v);
		}
		else if(ty==2){
			int u,v; cin>>u>>v;
			del(u,v);
		}
		else{
			int u,v,t,tt; cin>>u>>v>>t>>tt; 
			dfs(u,u,v);

			int cur=v,ans=0; 
			while(1){
				for(auto it: vec[cur]){
					int a=it.fi,b=it.se;
					/*int l=(t-b+a-1)/a,r=(tt-b)/a;
					if(l<0) l=0;
					ans+=max(0ll,r-l+1);*/

					FOR(x,t,tt+1) if(x%a==b) ans++;
				}

				if(cur==u) break;
				cur=par[cur];
			}
			cout << ans << endl;
		}
		
	}



}

/*
5
1 2
2 3
2 4
1 5
1
4 1
3
3 3 5 0 3
1 2 5
3 4 5 1 5
1 3 5
3 4 5 5 10
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7680 KB Output is correct
2 Correct 88 ms 8012 KB Output is correct
3 Correct 48 ms 7976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3560 ms 10684 KB Time limit exceeded
2 Execution timed out 3565 ms 10612 KB Time limit exceeded
3 Execution timed out 3580 ms 10620 KB Time limit exceeded
4 Execution timed out 3552 ms 10712 KB Time limit exceeded
5 Execution timed out 3533 ms 10648 KB Time limit exceeded
6 Execution timed out 3596 ms 10568 KB Time limit exceeded
7 Execution timed out 3595 ms 10608 KB Time limit exceeded
8 Execution timed out 3600 ms 10664 KB Time limit exceeded
9 Execution timed out 3574 ms 11000 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3571 ms 10512 KB Time limit exceeded
2 Execution timed out 3568 ms 10600 KB Time limit exceeded
3 Execution timed out 3533 ms 10840 KB Time limit exceeded
4 Execution timed out 3537 ms 10152 KB Time limit exceeded
5 Execution timed out 3541 ms 10004 KB Time limit exceeded
6 Execution timed out 3541 ms 10696 KB Time limit exceeded
7 Execution timed out 3568 ms 11004 KB Time limit exceeded
8 Execution timed out 3572 ms 10860 KB Time limit exceeded