This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#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)
const ll MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const ll MX = 1e9 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define debug(x) cerr << #x << " : " << x << endl;
#define debugs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define debugv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
vector<ll> create_path(vector<ll> adj[], ll n, ll s, ll e)
{
vector<bool> visited(n+1, false);
queue<ll> q;
vector<ll> parent(n+1, -1);
bool flag = true;
visited[s] = true;
q.push(s);
while(!q.empty()&&flag)
{
ll u = q.front();
q.pop();
for(auto v: adj[u])
{
if(visited[v])
continue;
visited[v] = true;
parent[v] = u;
q.push(v);
if(v==e)
flag = false;
}
}
vector<ll> path;
ll x = e;
while(x!=-1)
{
path.pb(x);
x = parent[x];
}
reverse(all(path));
return path;
return parent;
}
int main()
{
boost;
IO();
ll n;
cin >> n;
vector<ll> adj[n+1];
ll u, v;
FOR(i, 0, n-1)
{
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
ll k;
cin >> k;
vector<pair<ll, ll>> traffickers;
FOR(i, 0, k)
{
cin >> u >> v;
traffickers.pb(mp(u, v));
}
vector<vector<ll>> path(k);
FOR(i, 0, k)
{
path[i] = create_path(adj, n, traffickers[i].fi, traffickers[i].se);
}
ll q;
cin >> q;
while(q--)
{
ll c, s, e, t1, t2;
cin >> c;
if(c==1)
{
cin >> s >> e;
traffickers.pb(mp(s, e));
path.pb(create_path(adj, n, s, e));
k++;
}
if(c==2)
{
cin >> s >> e;
auto ind = find(all(traffickers), mp(s, e));
traffickers.erase(ind);
int indd = ind - traffickers.begin();
path.erase(path.begin() + indd);
k--;
}
if(c==3)
{
cin >> s >> e >> t1 >> t2;
vector<ll> path_marchandise = create_path(adj, n, s, e);
sort(all(path_marchandise));
ll res = 0;
FOR(i, 0, k)
{
vector<ll> p = path[i];
ll len = (ll)p.size();
ll ptr = 0;
ptr = t1%len;
ll timer = t1;
while(timer<=t2)
{
if(binary_search(all(path_marchandise), p[ptr]))
res++;
ptr++, timer++;
if(ptr==len)
ptr = 0;
}
}
cout << res << endl;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |