Sponsors

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;
}

8 killed, 95 injured...

Tragic Escalation: US-Israeli Strikes on Iran’s B1 Bridge Claim...

Singapore Airlines Direct Rtn...

Experience Luxury for Less: Singapore Airlines Announces Direct Return...

UWEC’s Bullert named scholar...

Ava Bullert: A Legacy of Academic and Athletic Excellence...

First Nations rehabilitation programs...

Improving First Nations Rehabilitation Programs: A Path Forward In Australia,...

CyberPower PC – 7800X3D,...

Score Big with the CyberPower PC RTX 5070 Ti...

North Korea’s Kim Jong...

Kim Jong Un Oversees Advanced Rocket Engine and Tank...

8 killed, 95 injured as US-Israeli strikes hit Iran’s B1 Bridge

Tragic Escalation: US-Israeli Strikes on Iran’s B1 Bridge Claim Eight Lives The geopolitical landscape of the Middle East has taken a somber and dangerous turn...

Singapore Airlines Direct Rtn to Singapore Ex PER $595, DRW $613, BNE $822 MEL $838, SYD $846, CNS $859 & More @ BTF

Experience Luxury for Less: Singapore Airlines Announces Direct Return Flights from Australia Travelers looking for a premium experience at an affordable price are in luck....

UWEC’s Bullert named scholar athlete of the year

Ava Bullert: A Legacy of Academic and Athletic Excellence at UW-Eau Claire In the highly competitive world of collegiate athletics, finding a player who perfectly...

First Nations rehabilitation programs aren’t keeping people out of prison. Here’s what would help

Improving First Nations Rehabilitation Programs: A Path Forward In Australia, the incarceration rates of First Nations people remain a critical issue that demands urgent systemic...

CyberPower PC – 7800X3D, RTX 5070 ti, 32GB, 2TB SSD, B850-VC Pro, Corsair RM850 watt, 2Year Premium Warranty +free Mech Keyboard and Resident Evil...

Score Big with the CyberPower PC RTX 5070 Ti Gaming Rig Deal Finding a high-end gaming PC that balances cutting-edge components with a reasonable price...

North Korea’s Kim Jong Un inspects solid-fuel rocket engine, new battle tank as Pyongyang steps up military development

Kim Jong Un Oversees Advanced Rocket Engine and Tank DevelopmentsIn a significant display of military modernization, North Korean leader Kim Jong Un has personally...

Aurangabad horror: Uncle locks 3 children inside room, slits their throats, attempts suicide

Shocking Incident in Aurangabad: Uncle Kills Three Children over Family Feud A horrific incident has sent shockwaves through the Aurangabad district of Bihar, where a...

ついに明かされる”ペニーワイズ”の起源―。「IT/イット ウェルカム・トゥ・デリー “それ”が見えたら、終わり。<シーズン1>」Blu-ray BOX 6/3(水)発売決定!

The Nightmare Returns: IT: Welcome to Derry Season 1 Blu-ray Release Details Fans of Stephen King’s legendary horror saga are preparing to return to the...

Will too much AI weaken troops’ judgement?

The Pentagon's AI Integration: A Double-Edged Sword for Military PersonnelThe United States Department of Defense is moving at an unprecedented pace to integrate Large...