[NOI2015]软件包管理器 树链剖分
在水一道
题意 如上
思路
1.版子
2.线段树区间覆盖
pushup
void pushup(int o){
t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
}
pushdown
void pushdown(int o){
if(t[o].lazy!=-1){
t[o<<1].lazy=t[o<<1|1].lazy=t[o].lazy;
t[o<<1].sum=(t[o<<1].r-t[o<<1].l+1)*t[o<<1].lazy;
t[o<<1|1].sum=(t[o<<1|1].r-t[o<<1|1].l+1)*t[o<<1|1].lazy;
t[o].lazy=-1;
}
}
3.更新前t[1].sum 与更新后的t[1].sum差的绝度值即为答案
完整代码
还是很简单的,一遍过
#pragma GCC optimize(3,"Ofast","inline") //G++
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
//======================================
namespace FastIO{
char print_f[105];void read() {}void print() {putchar('\n');}
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth){x = 0;char ch = getchar();ll f = 1;while (!isdigit(ch)){if (ch == '-')f *= -1;ch = getchar();}while (isdigit(ch)){x = x * 10 + ch - 48;ch = getchar();}x *= f;read(oth...);}
template <typename T, typename... T2>
inline void print(T x, T2... oth){ll p3=-1;if(x<0) putchar('-'),x=-x;do{print_f[++p3] = x%10 + 48;}while(x/=10);while(p3>=0) putchar(print_f[p3--]);putchar(' ');print(oth...);}} // namespace FastIO
using FastIO::print;
using FastIO::read;
//======================================
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn = 1e6+5;
struct node{
int l,r,sum,lazy;
}t[maxn<<2];
void pushup(int o){
t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
}
void pushdown(int o){
if(t[o].lazy!=-1){
t[o<<1].lazy=t[o<<1|1].lazy=t[o].lazy;
t[o<<1].sum=(t[o<<1].r-t[o<<1].l+1)*t[o<<1].lazy;
t[o<<1|1].sum=(t[o<<1|1].r-t[o<<1|1].l+1)*t[o<<1|1].lazy;
t[o].lazy=-1;
}
}
vector<int>edge[maxn];
void add(int x,int y){
edge[x].push_back(y);
edge[y].push_back(x);
}
int a[maxn];
int siz[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],dfn[maxn];
void dfs1(int u,int fr){
deep[u]=deep[fr]+1,siz[u]=1,fa[u]=fr;
for(auto v:edge[u]){
if(v==fr) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int fr){
top[u]=fr;
static int cnt=0;
dfn[u]=++cnt;
if(son[u]) dfs2(son[u],fr);
for(auto v:edge[u]){
if(v==son[u]||v==fa[u]) continue;
dfs2(v,v);
}
}
void build(int l,int r,int o=1){
t[o].l=l,t[o].r=r,t[o].sum=0,t[o].lazy=-1;
if(l==r) return ;
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o<<1|1);
}
void update(int l,int r,int val,int o=1){
if(t[o].l==l&&t[o].r==r){
t[o].sum=(t[o].r-t[o].l+1)*val;
t[o].lazy=val;
return ;
}
pushdown(o);
int mid=(t[o].l+t[o].r)>>1;
if(r<=mid) update(l,r,val,o<<1);
else if(l>mid) update(l,r,val,o<<1|1);
else update(l,mid,val,o<<1),update(mid+1,r,val,o<<1|1);
pushup(o);
}
void update2(int x,int y,int val){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]]) swap(x,y);
update(dfn[top[x]],dfn[x],val);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
update(dfn[x],dfn[y],val);
}
char s[105];
int main() {
#ifndef ONLINE_JUDGE
freopen("H:\\code\\in.in", "r", stdin);
freopen("H:\\code\\out.out", "w", stdout);
clock_t c1 = clock();
#endif
//**************************************
ios;
int n;
cin>>n;
for(int i=2;i<=n;i++){
int x;
cin>>x;
x++;
add(i,x);
}
dfs1(1,0);
dfs2(1,1);
build(1,n);
int _;
cin>>_;
while(_--){
cin>>(s+1);
int u;
cin>>u;
u++;
int t1=t[1].sum;
if(s[1]=='i'){
update2(u,1,1);
int t2=t[1].sum;
cout<<abs(t1-t2)<<"\n";
}
else{
update(dfn[u],dfn[u]+siz[u]-1,0);
int t2=t[1].sum;
cout<<abs(t1-t2)<<"\n";
}
}
//**************************************
#ifndef ONLINE_JUDGE
cerr << "Time:" << clock() - c1 << "ms" << endl;
#endif
return 0;
}