题目链接

  上午跟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 ;
}

最新文章

  1. OpenCASCADE Job - dimue
  2. 不使用session,借助redis实现验证码
  3. 【转】ubuntu vpn自动切换路由
  4. 【代码笔记】iOS-看图听声音
  5. c++ this *this
  6. linux笔记:文件系统管理-fdisk分区
  7. Python socket编程之二:【struct.pack】&amp;【struct.unpack】
  8. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
  9. 新手浅谈C#关于abstract和interface
  10. 引用外部CSS的link和import方式的分析与比较
  11. SharePoint中的权限体系
  12. 使用Xcode插件,让iOS开发更加便捷
  13. 英语口语练习系列-C17-Love story
  14. React Fiber源码分析 第一篇
  15. Fast Failure Detection and Recovery in SDN with Stateful Data Plane
  16. 【技术】正則表達式—匹配电话号码,网址链接,Email地址
  17. pta 习题集 5-14 求n以内最大的k个素数以及它们的和
  18. 使用jenkins SVN MSBuil配置.net mvc网站进行持续集成
  19. 数据库路由中间件MyCat - 源代码篇(4)
  20. Nginx加状态监控

热门文章

  1. 关于JavaScript的变量和函数提升
  2. jquery绑定事件的系统参数传递方法
  3. codeforce Gym 100500H ICPC Quest (简单dp)
  4. CoreData介绍
  5. js原型,原型链的理解
  6. 广播监听USB插入与拔出
  7. Lemonade Trade
  8. _variant_t的使用
  9. java利用SuffixFileFilter统计目录下特定后缀名文件的数目
  10. Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static