裸的树链剖分。

对于安装 查询和维护到根路径

对于卸载 查询和维护子树信息

一开始线段树add[]标记要全赋值为-1


 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; typedef long long LL; #define N 200010 int id;
int fa[N],siz[N],top[N],son[N];
int pos[N],r[N];
LL sum[N<<],add[N<<]; struct Node
{
int to,next;
}e[N];
int head[N];
int cnt; int a; char s; int n,q; int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} void link(int x,int y)
{
e[++cnt]=(Node){y,head[x]};
head[x]=cnt;
} void dfs(int x)
{
siz[x]=;
for (int i=head[x],mx=;i;i=e[i].next)
if (e[i].to!=fa[x])
{
fa[e[i].to]=x;
dfs(e[i].to);
siz[x]+=siz[e[i].to];
if (siz[e[i].to]>mx)
mx=siz[e[i].to],son[x]=e[i].to;
}
} void dfs2(int x,int cha)
{
top[x]=cha;
pos[x]=++id;
if (son[x])
dfs2(son[x],cha);
for (int i=head[x];i;i=e[i].next)
if(e[i].to!=fa[x] && e[i].to!=son[x])
dfs2(e[i].to,e[i].to);
r[x]=id;
} void pushup(int now)
{
sum[now]=sum[now<<]+sum[now<<|];
} void pushdown(int nowl,int nowr,int now,int mid)
{
if (add[now]!=-)
{
LL t=add[now];
add[now]=-;
add[now<<]=t;
add[now<<|]=t;
sum[now<<]=t*(mid-nowl+);
sum[now<<|]=t*(nowr-mid);
}
} void update(int nowl,int nowr,int now,int s,int t,LL d)
{
if (nowl>=s && nowr<=t)
{
add[now]=d;
sum[now]=(nowr-nowl+)*d;
return ;
}
int mid=(nowl+nowr)>>;
pushdown(nowl,nowr,now,mid);
if (s<=mid)
update(nowl,mid,now<<,s,t,d);
if (t>mid)
update(mid+,nowr,now<<|,s,t,d);
pushup(now);
} int query(int nowl,int nowr,int now,int s,int t)
{
if (nowl>=s && nowr<=t)
return sum[now];
int mid=(nowl+nowr)>>;
int ans=;
pushdown(nowl,nowr,now,mid);
if (s<=mid)
ans+=query(nowl,mid,now<<,s,t);
if (t>mid)
ans+=query(mid+,nowr,now<<|,s,t);
return ans;
} int work1(int x)
{
int ans,res=;
while (x)
{
ans=query(,n,,pos[top[x]],pos[x]);
res+=pos[x]-pos[top[x]]+-ans;
update(,n,,pos[top[x]],pos[x],);
if (ans)
break;
x=fa[top[x]];
}
return res;
} int work2(int x)
{
int res;
res=query(,n,,pos[x],r[x]);
update(,n,,pos[x],r[x],);
return res;
} int main()
{
n=read();
for (int i=;i<n;i++)
{
a=read();
link(a+,i+);
}
dfs();
dfs2(,);
memset(add,-,sizeof(add));
q=read();
while (q--)
{
scanf("%c",&s);
a=read();
if (s=='i')
printf("%d\n",work1(a+));
else
printf("%d\n",work2(a+));
}
return ;
}

【bzoj4196】[Noi2015]软件包管理器的更多相关文章

  1. [UOJ#128][BZOJ4196][Noi2015]软件包管理器

    [UOJ#128][BZOJ4196][Noi2015]软件包管理器 试题描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管 ...

  2. [BZOJ4196][NOI2015]软件包管理器(树链剖分)

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2166  Solved: 1253[Submit][Sta ...

  3. [BZOJ4196][NOI2015]软件包管理器

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1040  Solved: 603[Submit][Stat ...

  4. [bzoj4196][Noi2015]软件包管理器_树链剖分_线段树

    软件包管理器 bzoj-4196 Noi-2015 题目大意:Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件 ...

  5. BZOJ4196[Noi2015]软件包管理器——树链剖分+线段树

    题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个 ...

  6. BZOJ4196 [Noi2015]软件包管理器 【树剖】

    题目 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个软件 ...

  7. BZOJ4196: [Noi2015]软件包管理器(树链剖分)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  8. bzoj4196 [Noi2015]软件包管理器 树链剖分+线段树

    先把树剖分了(又是dfs1.dfs2),然后区间求和.区间覆盖即可 难得的1A好(shui)题 ——写了那么多题,终于有一道是1A的了,加上上一次连续交了几遍A的程序,我的状态莫名好看啊233 总结: ...

  9. BZOJ4196——noi2015软件包管理器

    1.题目大意:讲道理,就是让你有两个修改一个是把一个点到根的路径上的点权值全部变成1,另一个是把一个子树全部变成0 然后让你输出每次修改,改变的哪些节点的值 2.分析:就是一个树剖,树剖是满足dfs序 ...

  10. bzoj4196 [Noi2015]软件包管理器——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4196 树链剖分. 代码如下: #include<iostream> #inclu ...

随机推荐

  1. IIS添加更改默认页面

    服务器中打开IIS管理器,选择默认文档,双击即可进入编辑:

  2. Unexpected token d in JSON at position 669 while parsing near '...ct-mode":"^6.0.2"}

    问题 在安装 babel 的时候,遇到问题 Unexpected token d in JSON at position 669 while parsing near '...ct-mode" ...

  3. 诊断:MRP0: Background Media Recovery terminated with error 1111

    表现: 灾备环境,无法继续应用日志. 日志: MRP0: Background Media Recovery terminated with error 1111 Fri Jan 18 15:55:2 ...

  4. ubuntu 16.4 安装mysql-python

    sudo apt-get install python-pip python-dev libmysqlclient-dev pip install MySQL-python

  5. 杭电 1789 Doing Homework again (贪心 求最少扣分)

    Description zichen has just come back school from the 30th ACM/ ICPC. Now he has a lot of homework t ...

  6. 【Codeforces 1031C】Cram Time

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果找到最大的n使得1+2+...+n<=a+b 然后第一天输出1,2.3,4....t1 这里1+2+..+t1<=a 这还远远 ...

  7. STM32F407 跑马灯 库函数版 个人笔记

    原理图: MCU在开发板原理图的第二页,LED在开发板原理图的第三页 由图可知,PF9 ,PF10 若输出低电平则灯亮,高电平则灯灭 选推挽输出 代码步骤 使能IO口时钟. 调用函数RCC_AHB1P ...

  8. ROS 笔记 程序包/节点/topic

    官方教程: wiki.ros.org/cn/ROS/tutorials 程序包打创建于编译 创建程序包 在工作空间的src底下,输入如下命令: $ catkin_create_pkg 要创建的包名 依 ...

  9. POJ1094 / ZOJ1060

    #include <cstdio> #include <cstring> #include <stack> #include <iostream> us ...

  10. CodeForces788B 欧拉路

    B. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...