[Bzoj4196] [NOI2015] 软件包管理器 [树链剖分,线段树]
题解摘要:树链剖分后用线段树区间查询修改,对于安装软件,将改点到根的路径全部变为1,对于卸载软件,将子树清空。注意边界,编号是从0开始的,容易漏掉树根。
第一次写树剖~
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime> using namespace std; struct Edge
{
int to,next;
}e[]; int ind,n,Pre[],f[],Sum[],Out_dfn[];
int p[],cnt,hv[],Head[],Dfn[]; void Add_edge(const int & x,const int & y)
{
e[++cnt].to=y;
e[cnt].next=p[x];
p[x]=cnt;
return ;
} int Init_Dfs(const int& S)
{
int num=,i,Max_=;
for(i=p[S];i;i=e[i].next)
{
int temp=Init_Dfs(e[i].to);
if(temp>Max_)Max_=temp,hv[S]=e[i].to;
num+=temp;
}
return num;
} void Dfs(const int& S,const int& fa)
{
int i;
if(!Head[S])Head[S]=Head[fa];Dfn[S]=++ind; if(hv[S])Dfs(hv[S],S);
for(i=p[S];i;i=e[i].next)
{
if(e[i].to!=hv[S])
{
Head[e[i].to]=e[i].to;
Pre[e[i].to]=S;
Dfs(e[i].to,S);
}
}
Out_dfn[S]=ind;
return ;
} inline void push_down(const int & num,const int & l,const int & r)
{
if(f[num])
{
if(l!=r)
{
f[num<<]=f[num<<|]=f[num];
}
if(f[num]==)Sum[num]=r-l+;
if(f[num]==-)Sum[num]=;
f[num]=;
}
return ;
} void push_up(const int & num,const int & l,const int & r)
{
if(l!=r)
{
int mid=l+((r-l)>>);
push_down(num<<,l,mid);
push_down(num<<|,mid+,r);
Sum[num]=Sum[num<<]+Sum[num<<|];
}
return ;
} void Change(const int & l,const int & r,const int & num,
const int & s,const int & t,const int &d)
{
if(s<=l && r<=t)
{
f[num]=d;
push_down(num,l,r);
return ;
}
int mid=l+((r-l)>>);
push_down(num,l,r);
if(s<=mid)Change(l,mid,num<<,s,t,d);
if(t>mid) Change(mid+,r,num<<|,s,t,d);
push_up(num,l,r);
return ;
} int Query(const int & l,const int & r,const int & num,
const int & s,const int & t)
{
if(s<=l && r<=t)
{
push_down(num,l,r);
return Sum[num];
} int mid=l+((r-l)>>),temp=;
push_down(num,l,r);
if(s<=mid)temp+=Query(l,mid,num<<,s,t);
if(t>mid) temp+=Query(mid+,r,num<<|,s,t);
push_up(num,l,r);
return temp;
} int Get_sum_install(const int& S)
{
int x=S,temp=,sum=; while(x)
{
sum+=Query(,n,,Dfn[Head[x]],Dfn[x]);
Change(,n,,Dfn[Head[x]],Dfn[x],);
temp+=Dfn[x]-Dfn[Head[x]]+;
x=Pre[Head[x]];
} return temp-sum;
} int Get_sum_remove(const int & S)
{
int x=S,sum=; sum=Query(,n,,Dfn[x],Out_dfn[x]);
Change(,n,,Dfn[x],Out_dfn[x],-); return sum;
} int main()
{ int i,x,q;
char op[]; scanf("%d",&n);
for(i=;i<=n;++i)
{
scanf("%d",&x);x++;
Add_edge(x,i);
} Init_Dfs(); Head[]=;
Dfs(,);
scanf("%d",&q);
for(i=;i<=q;++i)
{
scanf("%s%d",op,&x);x++;
if(op[]=='i')
printf("%d\n",Get_sum_install(x));
else
printf("%d\n",Get_sum_remove(x));
} return ;
}
不知道为什么,BZOJ TLE了,但是UOJ AC(求大神指教)??
[Bzoj4196] [NOI2015] 软件包管理器 [树链剖分,线段树]的更多相关文章
- bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2852 Solved: 1668[Submit][Sta ...
- [UOJ#128][BZOJ4196][Noi2015]软件包管理器
[UOJ#128][BZOJ4196][Noi2015]软件包管理器 试题描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
随机推荐
- ubuntu/linuxmint下java环境变量设置
1.root权限下使用vi或gedit打开/etc目录下的profile文件,末尾加入环境变量. 1)命令: sudo gedit /etc/profile 2)环境变量个人案例: export JA ...
- Finding Nemo(搜索)
http://poj.org/problem?id=2049 题意:有一个迷宫,迷宫中有墙.门和空地.有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个 ...
- [Swift通天遁地]五、高级扩展-(14)扩展String快速计算字符串中的各种数学表达式
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 基于Spark Streaming预测股票走势的例子(一)
最近学习Spark Streaming,不知道是不是我搜索的姿势不对,总找不到具体的.完整的例子,一怒之下就决定自己写一个出来.下面以预测股票走势为例,总结了用Spark Streaming开发的具体 ...
- Sql批量插入方法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- MyEclipse找不到install new software
Window->Preferences->Capabilities-> classic update(勾选即可) 勾选后会出现software updates,下面按照help-&g ...
- Mac OS安装octave出现的问题-'error:terminal type set to 'unknown'的解决'
学习Machine learning需要使用Octave语言,毕竟Andrew Ng (恩达.吴)力荐.本机系统Mac OS X EI Capitan, 其实什么系统都无所谓了,安装原理都是一样的. ...
- Mybatis的Dao向mapper传多个参数(三种解决方案)
第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...
- VMWare 安装Ubuntu 16.04
1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...
- SQl基本操作——视图
视图适合频繁查询的表:将一个查询结果作为虚拟表提供给开发人员.安全性高,视图只能查询不能修改,它是一张虚拟表.查询方便,逻辑清晰,但是性能低,一般情况下不如自己写sql语句. --创建视图 creat ...