WISCO信息组NOIP模拟赛-部落冲突
传送门
首先肯定考虑树剖,这里没有要求区间加,所以可以用树状数组维护,不会卡常的
这里是边权,可以转化为点权:让每条边连接的较深的节点的点权等于边权即可,然后计算的时候减去lca
- #include<cstdio>
- #include<cstdlib>
- #include<algorithm>
- #include<cstring>
- #define MAXN 300005
- #define LOG 20
- using namespace std;
- int read(){
- int x=;char ch=getchar();
- while(ch<''||ch>''){ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+(ch^);ch=getchar();}
- return x;
- }
- int n,T;
- int a[MAXN],dat[MAXN];
- int dep[MAXN],size[MAXN],gs[MAXN],fa[][MAXN];
- int top[MAXN],tree[MAXN],pre[MAXN],tot;
- int first[MAXN],nxt[MAXN<<],to[MAXN<<],cnt;
- int id[MAXN],tmp;
- void add(int x,int y){
- nxt[++cnt]=first[x];first[x]=cnt;to[cnt]=y;
- nxt[++cnt]=first[y];first[y]=cnt;to[cnt]=x;
- }
- int lca(int x,int y){
- if(dep[x]<dep[y]){
- swap(x,y);
- }
- for(int k=dep[x]-dep[y],p=;k;k>>=,p++){
- if(k&){
- x=fa[p][x];
- }
- }
- if(x==y){
- return x;
- }
- for(int k=LOG-;k>=;k--){
- if(fa[k][x]!=fa[k][y]){
- x=fa[k][x],y=fa[k][y];
- }
- }
- return fa[][x];
- }
- void change(int k,int x){
- while(k<=n){
- dat[k]+=x;
- k+=(k&-k);
- }
- }
- int query(int k){
- int ret=;
- while(k>=){
- ret+=dat[k];
- k-=(k&-k);
- }
- return ret;
- }
- void dfs1(int x){
- size[x]=;
- for(int e=first[x];e;e=nxt[e]){
- int y=to[e];
- if(y==fa[][x]){
- continue;
- }
- fa[][y]=x;
- dep[y]=dep[x]+;
- dfs1(y);
- size[x]+=size[y];
- if(size[y]>size[gs[x]]){
- gs[x]=y;
- }
- }
- }
- void dfs2(int x,int t){
- top[x]=t;
- tree[x]=(++tot);
- pre[tot]=x;
- if(!gs[x]){
- return;
- }
- dfs2(gs[x],t);
- for(int e=first[x];e;e=nxt[e]){
- int y=to[e];
- if(y==fa[][x]||y==gs[x]){
- continue;
- }
- dfs2(y,y);
- }
- }
- int ask(int x,int y){
- int f1=top[x],f2=top[y];
- if(dep[f1]<dep[f2]){
- swap(x,y),swap(f1,f2);
- }
- int ret=-a[lca(x,y)];
- while(f1!=f2){
- ret+=query(tree[x])-query(tree[f1]-);
- x=fa[][f1]; f1=top[x];
- if(dep[f1]<dep[f2]){
- swap(x,y),swap(f1,f2);
- }
- }
- if(dep[x]<dep[y]){
- swap(x,y);
- }
- ret+=query(tree[x])-query(tree[y]-);
- return (ret<=);
- }
- void init(){
- n=read();T=read();
- for(int i=;i<n;i++){
- int x=read(),y=read();
- add(x,y);
- }
- dfs1();
- dfs2(,);
- for(int k=;k<LOG;k++){
- for(int i=;i<=n;i++){
- fa[k][i]=fa[k-][fa[k-][i]];
- }
- }
- }
- void solve(){
- char ch[];
- while(T--){
- scanf("%s",ch);
- int x=read();
- if('Q'==ch[]){
- int y=read();
- if(ask(x,y)){
- printf("Yes\n");
- }
- else{
- printf("No\n");
- }
- }
- else if('C'==ch[]){
- int y=read();
- if(dep[x]<dep[y]){
- swap(x,y);
- }
- change(tree[x],);
- a[x]++;
- id[++tmp]=x;
- }
- else{
- x=id[x];
- change(tree[x],-);
- a[x]--;
- }
- }
- }
- int main()
- {
- init();
- solve();
- return ;
- }
树剖AC
也可以是树上差分,用树状数组+dfs序,本质上是差不多的
- #include<cstdio>
- #include<cstdlib>
- #include<algorithm>
- #include<cstring>
- #define MAXN 300005
- #define LOG 20
- using namespace std;
- int read(){
- int x=;char ch=getchar();
- while(ch<''||ch>''){ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+(ch^);ch=getchar();}
- return x;
- }
- int n,T;
- int first[MAXN],nxt[MAXN<<],to[MAXN<<],cnt;
- int pin[MAXN],pout[MAXN],tot;
- int dat[MAXN<<];
- int fa[LOG][MAXN],dep[MAXN];
- int id[MAXN],tmp;
- void change(int k,int x){
- while(k<=(n<<)){
- dat[k]+=x;
- k+=(k&-k);
- }
- }
- int query(int k){
- int ret=;
- while(k>=){
- ret+=dat[k];
- k-=(k&-k);
- }
- return ret;
- }
- void add(int x,int y){
- nxt[++cnt]=first[x];first[x]=cnt;to[cnt]=y;
- nxt[++cnt]=first[y];first[y]=cnt;to[cnt]=x;
- }
- int lca(int x,int y){
- if(dep[x]<dep[y]){
- swap(x,y);
- }
- for(int k=dep[x]-dep[y],p=;k;k>>=,p++){
- if(k&){
- x=fa[p][x];
- }
- }
- if(x==y){
- return x;
- }
- for(int k=LOG-;k>=;k--){
- if(fa[k][x]!=fa[k][y]){
- x=fa[k][x],y=fa[k][y];
- }
- }
- return fa[][x];
- }
- void dfs(int x){
- pin[x]=(++tot);
- for(int e=first[x];e;e=nxt[e]){
- int y=to[e];
- if(y==fa[][x]){
- continue;
- }
- dep[y]=dep[x]+;
- fa[][y]=x;
- dfs(y);
- }
- pout[x]=(++tot);
- }
- int ask(int x,int y){
- int t=query(pin[x])+query(pin[y])-*query(pin[lca(x,y)]);
- return (t<=);
- }
- void init(){
- n=read(); T=read();
- for(int i=;i<n;i++){
- int x=read(),y=read();
- add(x,y);
- }
- dfs();
- for(int k=;k<LOG;k++){
- for(int i=;i<=n;i++){
- fa[k][i]=fa[k-][fa[k-][i]];
- }
- }
- }
- void solve(){
- char ch[]={};
- while(T--){
- scanf("%s",ch);
- int x=read();
- if('Q'==ch[]){
- int y=read();
- if(ask(x,y)){
- printf("Yes\n");
- }
- else{
- printf("No\n");
- }
- }
- else if('C'==ch[]){
- int y=read();
- if(dep[x]<dep[y]){
- swap(x,y);
- }
- change(pin[x],);
- change(pout[x]+,-);
- id[++tmp]=x;
- }
- else{
- x=id[x];
- change(pin[x],-);
- change(pout[x]+,);
- }
- }
- }
- int main()
- {
- // freopen("data.in","r",stdin);
- init();
- solve();
- return ;
- }
树上差分AC
WISCO信息组NOIP模拟赛-部落冲突的更多相关文章
- WISCO信息组NOIP模拟赛-数据结构
传送门 差分+暴力 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstri ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
随机推荐
- win7下,使用django运行django-admin.py无法创建网站
安装django的步骤: 1.安装python,选择默认安装在c盘即可.设置环境变量path,值添加python的安装路径. 2.下载ez_setup.py,下载地址:http://peak.tele ...
- android头像选择(拍照,相册,裁剪)
组织头像上传时候,不兼容android6.0,并且 imageview.setImageBitmap(BitmapFactory.decodeFile(IMAGE_FILE_LOCATION));// ...
- PV & PVC - 每天5分钟玩转 Docker 容器技术(150)
Volume 提供了非常好的数据持久化方案,不过在可管理性上还有不足. 拿前面 AWS EBS 的例子来说,要使用 Volume,Pod 必须事先知道如下信息: 当前 Volume 来自 AWS EB ...
- sql 多条记录插入
--多条记录插入,用逗号分开值. INSERT dbo.studentinfor ( id, name, class, age, hpsw ) ', -- id - nvarchar(50) N'te ...
- 前端基础之CSS-Day13
1.CSS 语法 1.1.CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector { property: value; property: value; ... proper ...
- vueJs 源码解析 (三) 具体代码
vueJs 源码解析 (三) 具体代码 在之前的文章中提到了 vuejs 源码中的 架构部分,以及 谈论到了 vue 源码三要素 vm.compiler.watcher 这三要素,那么今天我们就从这三 ...
- kubernetes入门(09)kubernetes的命令
Help执行<kubectl>或<kubectl help> | <kubectl --help>获得命令的帮助信息.kubectl的帮助信息.示例相当详细,而且简 ...
- OAuth2.0学习(1-10)新浪开放平台微博认证-手机应用授权和refresh_token刷新access_token
1.当你是使用微博官方移动SDK的移动应用时,授权返回access_token的同时,还会多返回一个refresh_token: JSON 1 2 3 4 5 6 { "access ...
- C#微信公众号开发——access_token的获取
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_toke ...
- jenkins创建multibranch pipeline
参考以下文章进行实践: https://jenkins.io/doc/pipeline/tour/hello-world/#what-is-a-jenkins-pipeline (看见一个介绍的还不错 ...