Sunday, April 21, 2024

# Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined), problem: (G) Can Bash Save the Day? Solution in C/C++

#include<bits/stdc++.h>
#define N 200100
using namespace std;
int sz[N];
vector<int> g[N],w[N];
int loc[N][20],dep[N],tloc[20],td;
long long len[N][20];
bool del[N];
void build(int u, int p, int ly, long long d){
sz[u]=1;
len[u][ly]=d;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(v==p||del[v]) continue;
build(v,u,ly,d+w[u][i]);
sz[u]+=sz[v];
}
}
void decom(int u){
build(u,0,td,0);
int tsz=sz[u],p=-1;
bool flag=true;
while(flag){
flag=false;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(del[v]||v==p) continue;
if(sz[v]+sz[v]>tsz){
p=u;
u=v;
flag=true;
break;
}
}
}
build(u,0,td,0);
tloc[td++]=u;
dep[u]=td;
for(int i=0;i<td;i++){
loc[u][i]=tloc[i];
}
del[u]=true;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(del[v]) continue;
decom(v);
}
td–;
}
int a[N];
vector<long long> sum[N];
vector<int> ts[N];
int pos[N][20];
int main(){
int n,q,u,v,x,t,l,r;
long long ans=0,msk=(1LL<<30)-1;
scanf(“%d%d”,&n,&q);
for(int i=0;i<n;i++){
scanf(“%d”,&a[i]);
}
for(int i=1;i<n;i++){
scanf(“%d%d%d”,&u,&v,&x);
g[u].push_back(v);
g[v].push_back(u);
w[u].push_back(x);
w[v].push_back(x);
}
decom(1);
for(int i=1;i<=n;i++){
sum[i].push_back(0);
ts[i].push_back(-1);
}
for(int i=0;i<n;i++){
u=a[i];
for(int j=0;j<dep[u];j++){
sum[loc[u][j]].push_back(sum[loc[u][j]].back()+len[u][j]-(j>0?len[u][j-1]:0));
pos[u][j]=ts[loc[u][j]].size();
ts[loc[u][j]].push_back(i);
}
}
while(q–){
scanf(“%d”,&t);
if(t==1){
scanf(“%d%d%d”,&l,&r,&u);
l=msk&ans^l;
r=msk&ans^r;
u=msk&ans^u;
l–,r–;
//printf(“actual: %d %d %d\n”,l,r,u);
ans=0;
for(int i=0;i<dep[u];i++){
v=loc[u][i];
int pr=upper_bound(ts[v].begin(),ts[v].end(),r)-ts[v].begin()-1,pl=lower_bound(ts[v].begin(),ts[v].end(),l)-ts[v].begin()-1;
if(pl==pr) break;
ans+=sum[v][pr]-sum[v][pl]+(pr-pl)*(len[u][i]-(i>0?len[u][i-1]:0));
}
printf(“%I64d\n”,ans);
}
else{
scanf(“%d”,&x);
x=msk&ans^x;
//printf(“actual: %d\n”,x);
u=a[x],v=a[x-1];
for(int i=0;i<max(dep[u],dep[v]);i++){
if(i<min(dep[u],dep[v])&&loc[u][i]==loc[v][i]){
sum[loc[u][i]][pos[v][i]]=sum[loc[u][i]][pos[v][i]+1]-sum[loc[u][i]][pos[v][i]]+sum[loc[u][i]][pos[v][i]-1];
swap(pos[u][i],pos[v][i]);
}
else{
if(i<dep[u]){
ts[loc[u][i]][pos[u][i]]–;
}
if(i<dep[v]){
ts[loc[v][i]][pos[v][i]]++;
}
}
}
swap(a[x],a[x-1]);
}
}
return 0;
}

## Celebrating Black History Month:...

As February unfolds, so does the annual celebration of...

## The Path to Self-Mastery:...

Embarking on a journey of self-mastery and breaking free...

## Wizards of Waverly Place...

In a spellbinding announcement, Disney has officially revealed that...

## Jim Irsay’s Reported ‘Suspected...

In a shocking turn of events last month, Jim...

Coachella Valley Music and Arts Festival, one of the...

## 2024 Taiwan Election: Pivotal...

As Taiwan gears up for its 2024 presidential election,...

### Celebrating Black History Month: Past, Future

As February unfolds, so does the annual celebration of Black History Month, a time to reflect on the profound contributions, resilience, and rich cultural...

### The Path to Self-Mastery: Lessons from Book ‘The Mountain Is You’

Embarking on a journey of self-mastery and breaking free from self-sabotage is a transformative process that requires dedication and conscious effort. Brianna Wiest's insightful...

### Wizards of Waverly Place Cast Reunites for Enchanting Revival

In a spellbinding announcement, Disney has officially revealed that the beloved fantasy series "Wizards of Waverly Place" is set for a magical comeback, featuring...

### Jim Irsay’s Reported ‘Suspected Overdose’: A Closer Look

In a shocking turn of events last month, Jim Irsay, the owner of the Indianapolis Colts, was reportedly found unresponsive at his home in...

### Coachella 2024: Iconic headliners, unforgettable musical experience!

Coachella Valley Music and Arts Festival, one of the most iconic and eagerly anticipated music festivals globally, has just dropped its highly anticipated lineup...

### 2024 Taiwan Election: Pivotal Moment in Political Landscape

As Taiwan gears up for its 2024 presidential election, the political landscape is buzzing with anticipation and fervor. With the island nation situated at...

### Michael Strahan’s Daughter’s Medulloblastoma Diagnosis

In a recent and heartbreaking revelation, Michael Strahan, former NFL star and television personality, shared the devastating news of his daughter Isabella's diagnosis with...

### Michigan vs. Washington: The 2024 National Championship Clash

In a highly anticipated matchup, the 2024 National Championship will witness a clash of football titans as the Michigan Wolverines square off against the...

### Jason Kelce: Unmasking the Unconventional NFL Icon

In the world of professional football, where conformity often takes center stage, one player stands out as a beacon of individuality, both on and...