//#include "gap.h"
//#include "mushrooms.h"
//#include "island.h"
//#include "joitour.h"
#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "ftree"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r)
{
return uniform_int_distribution<int>(l,r)(rd);
}
void phongbeo();
const int inf = 1e16;
const ll mod2 = 1e9+7;
const ll base=67;
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,lim,w,l,r ;
int kk;
int t;
int el = 19;/*
main()
{
if(fopen(task2".inp","r"))
{
fin(task2);
fou(task2);
}
if(fopen(task".inp","r"))
{
fin(task);
fou(task);
}
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
/// cin>>s1;
//int t;cin>>t; while(t--)
phongbeo();
// checktime
}*/
int in[200001],heavy[200001],ou[200001],head[200001];
vector<int> v[200001];
int color[200001];
int par[200001];
bool kiki[200001];
int dfs(int x,int y)
{
int sz=1,mxz=-1;
par[x]=y;
for(auto f:v[x])
if(f^y)
{
int s=dfs(f,x);
sz+=s;
if(s>mxz)
{
mxz=s;
heavy[x]=f;
}
}
return sz;
}
void des(int x,int y)
{
in[x]=++dem;
head[x]=y;
if(heavy[x]!=0)
{
des(heavy[x],y);
for(auto f:v[x])
if(in[f]==0)
des(f,f);
}
ou[x]=dem;
}
bool check(int x,int y)
{
if(in[x]<=in[y]&&in[y]<=ou[x])return 1;
return 0;
}
struct ic
{
int a,b,c;
};
struct segtree
{
ll t[800001][3];
ll total[800001][3];
bool flip[800001];
int onbit[800001];
void mer(int x,int y,int z)
{
for(int f=0; f<3; f++)
t[x][f]=t[y][f]+t[z][f];
onbit[x]=onbit[y]+onbit[z];
}
void flip_bit(int node,int l,int r,int l1)
{
if(l>l1||r<l1) return;
if(l==r)
{
if(!flip[node])
{
t[node][0]=total[node][0];
t[node][1]=total[node][1];
onbit[node]=1;
}
else
{
t[node][0]=t[node][1]=t[node][2]=onbit[node]=0;
}
flip[node]=1-flip[node];
t[node][2]=t[node][0]*t[node][1];
return;
}
int mid=l+r>>1;
for(int gg=0; gg<2; gg++)
if(total[node][gg]!=0)ff(node,gg);
flip_bit(node<<1,l,mid,l1);
flip_bit(node<<1|1,mid+1,r,l1);
mer(node,node<<1,node<<1|1);
}
void apply(int node,int f,int cost)
{
t[node][2]+=t[node][1-f]*cost;
t[node][f]+=cost*onbit[node];
total[node][f]+=cost;
}
void ff(int x,int y)
{
for(int i=x*2; i<=x*2+1; i++)
apply(i,y,total[x][y]);
total[x][y]=0;
}
void upd(int node,int l,int r,int l1,int r1,int f,int cost)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
apply(node,f,cost);
return;
}
int mid=l+r>>1;
for(int gg=0; gg<2; gg++)
if(total[node][gg]!=0)ff(node,gg);
upd(node<<1,l,mid,l1,r1,f,cost);
upd(node<<1|1,mid+1,r,l1,r1,f,cost);
mer(node,node<<1,node<<1|1);
}
ll get(int node,int l,int r,int l1,int r1,int f)
{
if(l>r1||r<l1) return 0;
if(l>=l1&&r<=r1) return total[node][f];
int mid=l+r>>1;
for(int gg=0; gg<2; gg++)
if(total[node][gg]!=0)ff(node,gg);
return get(node<<1,l,mid,l1,r1,f)+get(node<<1|1,mid+1,r,l1,r1,f);
}
} st[2];
struct hahasegtree
{
ll t[800001][3];
ll total[800001][3];
int lazy[800001][2];
bool flip[800001];
void mer(int x,int y,int z)
{
for(int f=0; f<3; f++)
t[x][f]=t[y][f]+t[z][f];
}
void flip_bit(int node,int l,int r,int l1)
{
if(l>l1||r<l1) return;
if(l==r)
{
if(!flip[node])
{
t[node][0]=total[node][0];
t[node][1]=total[node][1];
t[node][2]=total[node][2];
}
else
{
t[node][0]=t[node][1]=t[node][2]=0;
}
flip[node]=1-flip[node];
return;
}
int mid=l+r>>1;
for(int gg=0; gg<2; gg++)
if(lazy[node][gg]!=0)ff(node,gg);
flip_bit(node<<1,l,mid,l1);
flip_bit(node<<1|1,mid+1,r,l1);
mer(node,node<<1,node<<1|1);
}
void upd(int node,int l,int r,int l1,int f,int x,int cost)
{
if(l>l1||r<l1) return;
if(l==r)
{
total[node][f]+=x;
total[node][2]+=cost;
if(flip[node])
{
t[node][0]=total[node][0];
t[node][1]=total[node][1];
t[node][2]=total[node][2];
}
return;
}
for(int gg=0; gg<2; gg++)
if(lazy[node][gg]!=0)ff(node,gg);
int mid=l+r>>1;
upd(node<<1,l,mid,l1,f,x,cost);
upd(node<<1|1,mid+1,r,l1,f,x,cost);
mer(node,node<<1,node<<1|1);
}
void ff(int x,int y)
{
for(int i=x*2; i<=x*2+1; i++)
{
total[i][2]+=total[i][y]*lazy[x][y];
t[i][2]+=t[i][y]*lazy[x][y];
lazy[i][y]+=lazy[x][y];
}
lazy[x][y]=0;
}
void banana(int node,int l,int r,int l1,int r1,int f,int x)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
total[node][2]+=total[node][f]*x;
t[node][2]+=t[node][f]*x;
lazy[node][f]+=x;
return;
}
int mid=l+r>>1;
for(int gg=0; gg<2; gg++)
if(lazy[node][gg]!=0)ff(node,gg);
banana(node<<1,l,mid,l1,r1,f,x);
banana(node<<1|1,mid+1,r,l1,r1,f,x);
mer(node,node<<1,node<<1|1);
///if(l1==1&&r1==1) cout<<t[node<<1][2]<<" "<<l<<" "<<r<<" "<<t[node<<1|1][2],down
}
} sthaha;
void son_of_x(int x,int y,int f,int cost)
{
s3=st[1-f].get(1,1,n,in[x],in[x],0)-st[1-f].get(1,1,n,in[y],in[y],0);
if(kiki[x])
if(color[x]!=1)
{
if(f==1&&color[x]!=2)s3--;
if(f==0&&color[x]!=0)s3--;
}
sthaha.upd(1,1,n,in[x],f,cost,s3*cost);
}
void upd_hld(int x,int y,int f,int cost)
{
if(in[y]>=in[head[x]])
{
st[1-f].upd(1,1,n,in[y],in[x],1,-cost);
st[f].upd(1,1,n,in[y],in[x],0,cost);
sthaha.banana(1,1,n,in[y],in[x]-1,1-f,cost);
return;
}
st[1-f].upd(1,1,n,in[head[x]],in[x],1,-cost);
st[f].upd(1,1,n,in[head[x]],in[x],0,cost);
sthaha.banana(1,1,n,in[head[x]],in[x]-1,1-f,cost);
son_of_x(par[head[x]],head[x],f,cost);
upd_hld(par[head[x]],y,f,cost);
}
void upd(int x,int y,int cost)
{
if(y==1)
{
st[0].flip_bit(1,1,n,in[x]);
st[1].flip_bit(1,1,n,in[x]);
sthaha.flip_bit(1,1,n,in[x]);
}
else
{
if(y==2) y=1;
st[1-y].upd(1,1,n,1,n,1,cost);
upd_hld(x,1,y,cost);
}
}
void init(int _n,vector<int> _a,vector<int> _u,vector<int> _v,int Q)
{
n=_n;
for(int i=1; i<=n; i++)color[i]=_a[i-1];
for(int i=0; i<n-1; i++)
{
l=_u[i];
r=_v[i];
/// cout<<l<<" "<<r,down
v[l+1].pb(r+1);
v[r+1].pb(l+1);
}
dfs(1,0);
des(1,1);
for(int i=1; i<=n; i++)
{// cout<<in[i]<<" "<<color[i],down
kiki[i]=1;
upd(i,color[i],1);
}
}
void change(int l,int r)
{
upd(l+1,color[l+1],-1);
color[l+1]=r;
upd(l+1,color[l+1],1);
}
ll num_tours(){
return st[0].t[1][2]+st[1].t[1][2]+sthaha.t[1][2];
}
컴파일 시 표준 에러 (stderr) 메시지
joitour.cpp:29:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+16' to '2147483647' [-Woverflow]
29 | const int inf = 1e16;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |