#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC taraget("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
#define mp make_pair
//#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
using namespace std;
const int mod=1e9+7;
const int modp=1999999973;
const int modulo=998244353;
int n,q,cant[20],parinte[105],lca[105][17],tin[105],tout[105];
int aib[21][21][105],depth[105],first[105],last[105];
vector<int>nod[105],euler;
void DFS(int s,int par,int lvl){
euler.push_back(s);
parinte[s]=par;
depth[s]=lvl;
for(auto it:nod[s]){
if(it!=par){
DFS(it,s,lvl+1);
euler.push_back(s);
}
}
}
void DFStimp(int s,int par,int &timp){
tin[s]=timp;
++timp;
for(auto it:nod[s]){
if(it!=par){
DFStimp(it,s,timp);
}
}
tout[s]=timp;
++timp;
}
int lowestca(int x,int y){
if(x==y) return x;
else{
if(tin[x]<tin[y] && tout[x]>tout[y]) return x;
if(tin[y]<tin[x] && tout[y]>tout[x]) return y;
else{
while(true){
bool b=true;
for(int i=0;i<=15;i++){
int x1=lca[x][i];
int y1=lca[x][i+1];
if(tin[y1]<tin[y] && tout[y1]>tout[y]){
if(tin[x1]>tin[y] || tout[x1]<tout[y]){
x=x1;
b=false;
break;
}
}
}
if(b==true) return lca[x][0];
}
}
}
}
void update(int pos,int val,int arr[]){
while(pos<=euler.size()){
arr[pos]+=val;
pos+=(pos&-pos);
}
return;
}
int sum(int pos,int arr[]){
int sum=0;
while(pos){
sum+=arr[pos];
pos-=(pos&-pos);
}
return sum;
}
void querytip12(int x,int y,int tip){
int lc = lowestca(x,y);
int distanta = depth[x] + depth[y] - 2*depth[lc] + 1;
vector<int>path;
int curr = x;
path.push_back(curr);
while(curr!=lc){
curr = parinte[curr];
path.push_back(curr);
}
curr=y;
vector<int>tz;
tz.push_back(curr);
while(curr!=lc){
curr=parinte[curr];
tz.push_back(curr);
}
tz.pop_back();
if(tz.size()){
for(int i=tz.size()-1;i>=0;i--) path.push_back(tz[i]);
}
for(int i=0;i<=19;i++){
int nod=path[i%path.size()];
if(tip==1){
update(first[nod],1,aib[path.size()][i]);
update(last[nod]+1,-1,aib[path.size()][i]);
}
else{
update(first[nod],-1,aib[path.size()][i]);
update(last[nod]+1,1,aib[path.size()][i]);
}
}
}
int query3(int x,int y,int t1,int t2){
for(int i=0;i<=19;i++) cant[i]=0;
while(t1%20!=0 && t1<t2){
cant[t1%20]++;
t1++;
}
while(t2%20!=19 && t2>t1){
cant[t2%20]++;
t2--;
}
if(t1==t2) cant[t1%20]++;
else{
int c=(t2-t1+1);
for(int i=0;i<=19;i++) cant[i]+=(c/20);
}
int ans=0;
int lc=lowestca(x,y);
for(int i=0;i<=19;i++){
for(int j=1;j<=20;j++){
int cnt=cant[i]*(sum(first[x],aib[j][i])+sum(first[y],aib[j][i])-sum(first[lc],aib[j][i])-sum(first[parinte[lc]],aib[j][i]));
ans+=cnt;
}
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
srand(chrono::steady_clock::now().time_since_epoch().count());
// ifstream cin("input.in");
cin >> n;
for(int i=1;i<n;i++){
int x,y;
cin >> x >> y;
nod[x].push_back(y);
nod[y].push_back(x);
}
DFS(1,-1,0);
int cnt=0;
for(auto it:euler){
++cnt;
if(first[it]==0){
first[it]=cnt;
last[it]=cnt;
}
else last[it]=cnt;
}
for(int i=1;i<=n;i++) lca[i][0]=parinte[i];
for(int j=1;j<=16;j++){
for(int i=1;i<=n;i++){
lca[i][j]=lca[lca[i][j-1]][j-1];
}
}
int timp=0;
tin[0]=1e18;
tout[0]=1e18;
DFStimp(1,-1,timp);
cin >> q;
for(int i=1;i<=q;i++){
int x,y;
cin >> x >> y;
querytip12(x,y,1);
}
cin >> q;
int type=0;
for(int i=1;i<=q;i++){
cin >> type;
if(type!=3){
int x,y;
cin >> x >> y;
if(type==1) querytip12(x,y,1);
if(type==2) querytip12(x,y,2);
}
else{
int x,y,z,t;
cin >> x >> y >> z >> t;
cout << query3(x,y,z,t) << '\n';
}
}
}
Compilation message
traffickers.cpp: In function 'void update(int, int, int*)':
traffickers.cpp:77:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pos<=euler.size()){
~~~^~~~~~~~~~~~~~
traffickers.cpp: In function 'void querytip12(int, int, int)':
traffickers.cpp:95:9: warning: unused variable 'distanta' [-Wunused-variable]
int distanta = depth[x] + depth[y] - 2*depth[lc] + 1;
^~~~~~~~
traffickers.cpp: In function 'int32_t main()':
traffickers.cpp:181:12: warning: overflow in implicit constant conversion [-Woverflow]
tin[0]=1e18;
^~~~
traffickers.cpp:182:13: warning: overflow in implicit constant conversion [-Woverflow]
tout[0]=1e18;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
755 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
5 ms |
896 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
5 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
6 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
6 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
7 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |