【Luogu】P2146软件包管理器(树链剖分)
2024-10-15 09:52:32
上午跟rqy学了一道超难的概率题,准备颓一会,于是水了这么一道水题。
话说这题真的是模板啊。数据范围正好,描述特别贴近(都不给你绕弯子的),连图都给你画出来,就差题目描述加一句“树链剖分模板”了qwq
怕当年考noi的选手想不到这是树剖么qwq
然后一般来讲省选及以上难度的题都是有套路的,也就是说你秒出的算法都过不了,比如轮状病毒肯定秒出高斯消元解行列式,然后就过不了了
emm但是这个为什么是真的模板啊qwq
我交之前还想着“没事,肯定被卡。我只看看我树剖暴力能拿几分”
然后就A了啊喂。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#define maxn 300050
#define left (rt<<1)
#define right (rt<<1|1)
#define mid ((l+r)>>1)
#define lson l,mid,left
#define rson mid+1,r,right
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
}
int n;
int deep[maxn];
int top[maxn];
int father[maxn];
int dfn[maxn],ID;
int back[maxn];
int size[maxn];
int son[maxn]; struct Edge{
int next,to;
}edge[maxn*];
int head[maxn],num;
inline void add(int from,int to){
edge[++num]=(Edge){head[from],to};
head[from]=num;
} void findfs(int x,int fa){
deep[x]=deep[fa]+;
father[x]=fa;
size[x]=;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
findfs(to,x);
size[x]+=size[to];
if(son[x]==||size[son[x]]<size[to]) son[x]=to;
}
} void unidfs(int x,int Top){
dfn[x]=++ID; back[ID]=;
top[x]=Top;
if(!son[x]) return;
unidfs(son[x],Top);
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==father[x]||to==son[x]) continue;
unidfs(to,to);
}
} int tree[maxn*],tag[maxn*]; inline void pushup(int rt){
tree[rt]=tree[left]+tree[right];
} void pushdown(int rt,int m){
if(tag[rt]==-) return;
tag[left]=tag[rt];
tag[right]=tag[rt];
tree[left]=tag[rt]*(m-(m>>));
tree[right]=tag[rt]*(m>>);
tag[rt]=-;
return;
} void update(int from,int to,int num,int l,int r,int rt){
if(from<=l&&to>=r){
tree[rt]=num*(r-l+);
tag[rt]=num;
return;
}
pushdown(rt,r-l+);
if(from<=mid) update(from,to,num,lson);
if(to>mid) update(from,to,num,rson);
pushup(rt);
return;
} void updatesum(int from,int to,int num){
while(top[from]!=top[to]){
if(deep[top[from]]<deep[top[to]]) swap(from,to);
update(dfn[top[from]],dfn[from],num,,n,);
from=father[top[from]];
}
if(deep[from]>deep[to]) swap(from,to);
update(dfn[from],dfn[to],num,,n,);
return;
} int query(int from,int to,int l,int r,int rt){
if(from<=l&&to>=r) return tree[rt];
int ans=;
pushdown(rt,r-l+);
if(from<=mid) ans+=query(from,to,lson);
if(to>mid) ans+=query(from,to,rson);
return ans;
} int querysum(int from,int to){
int ans=;
while(top[from]!=top[to]){
if(deep[top[from]]<deep[top[to]]) swap(from,to);
ans+=query(dfn[top[from]],dfn[from],,n,);
from=father[top[from]];
}
if(deep[from]>deep[to]) swap(from,to);
ans+=query(dfn[from],dfn[to],,n,);
return ans;
} void updatetree(int from,int num){
update(dfn[from],dfn[from]+size[from]-,num,,n,);
} int querytree(int from){
return query(dfn[from],dfn[from]+size[from]-,,n,);
} int main(){
memset(tag,-,sizeof(tag));
n=read();
for(int i=;i<n;++i){
int x=read();
add(x+,i+);
add(i+,x+);
}
findfs(,);
unidfs(,);
int m=read();
for(int i=;i<=m;++i){
char ch[];int x;
scanf("%s%d",ch,&x);
x++;
if(ch[]=='i'){
int last=querysum(,x);
updatesum(,x,);
int now=querysum(,x);
printf("%d\n",now-last);
}
else{
int last=querytree(x);
updatetree(x,);
int now=querytree(x);
printf("%d\n",last-now);
}
}
return ;
}
最新文章
- OpenCASCADE Job - dimue
- 不使用session,借助redis实现验证码
- 【转】ubuntu vpn自动切换路由
- 【代码笔记】iOS-看图听声音
- c++ this *this
- linux笔记:文件系统管理-fdisk分区
- Python socket编程之二:【struct.pack】&;【struct.unpack】
- linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
- 新手浅谈C#关于abstract和interface
- 引用外部CSS的link和import方式的分析与比较
- SharePoint中的权限体系
- 使用Xcode插件,让iOS开发更加便捷
- 英语口语练习系列-C17-Love story
- React Fiber源码分析 第一篇
- Fast Failure Detection and Recovery in SDN with Stateful Data Plane
- 【技术】正則表達式—匹配电话号码,网址链接,Email地址
- pta 习题集 5-14 求n以内最大的k个素数以及它们的和
- 使用jenkins SVN MSBuil配置.net mvc网站进行持续集成
- 数据库路由中间件MyCat - 源代码篇(4)
- Nginx加状态监控