http://www.zybbs.org/JudgeOnline/problem.php?id=2243

  题目大意:给你一棵树,节点有颜色,要求可以查询某路径中连续颜色段的数目和修改某一段路径的颜色。

  两次拉实之后查询和修改即可。

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define NIL LCT
#define MM 200001
#define MN 100001
using namespace std;queue<int> q;
int n,m,a,b,c,color[MN];
char s[10];
struct EDGE{int pnt;EDGE *pre;EDGE (){}EDGE(int _pnt,EDGE *_pre):pnt(_pnt),pre(_pre){}
}Edge[MM],*EP=Edge,*edge[MM];inline void addedge(int a,int b){edge[a]=new(++EP)EDGE(b,edge[a]);edge[b]=new(++EP)EDGE(a,edge[b]);
}struct LinkCutTree{struct NODE{int c,lc,rc,mark,cnt;bool root;NODE *left,*right,*father;NODE (){}NODE(int _c,NODE *_left,NODE *_right,NODE *_father):c(_c),lc(_c),rc(_c),left(_left),right(_right),father(_father){mark=0,cnt=1,root=true;}}LCT[MN],*NP,*node[MN];void init(){NP=NIL;NIL->c=NIL->lc=NIL->rc=NIL->mark=0;NIL->left=NIL->right=NIL->father=NIL;NIL->root=false;}void build(){q.push(1);node[1]=new(++NP)NODE(color[1],NIL,NIL,NIL);while(!q.empty()){int i=q.front();q.pop();for(EDGE *j=edge[i];j;j=j->pre)if(node[j->pnt]!=node[i]->father){node[j->pnt]=new(++NP)NODE(color[j->pnt],NIL,NIL,node[i]);q.push(j->pnt);}}}void renew(NODE *&t,int key){if(t!=NIL) t->c=t->lc=t->rc=t->mark=key,t->cnt=1;}void pushdown(NODE *&t){if(t->mark){renew(t->left,t->mark);renew(t->right,t->mark);t->mark=0;}}void update(NODE *&t){t->lc=t->rc=t->c;if(t->left!=NIL) t->lc=t->left->lc;if(t->right!=NIL) t->rc=t->right->rc;t->cnt=t->left->cnt+t->right->cnt+1;if(t->c==t->left->rc) t->cnt--;if(t->c==t->right->lc) t->cnt--;}void zig(NODE *&t){NODE *f=t->father,*r=t->right;pushdown(f);pushdown(t);t->father=f->father;if(f->root) t->root=true,f->root=false;else{if(f->father->left==f) f->father->left=t;else f->father->right=t;}t->right=f,f->father=t,f->left=r,r->father=f;update(f);}void zag(NODE *&t){NODE *f=t->father,*l=t->left;pushdown(f);pushdown(t);t->father=f->father;if(f->root) t->root=true,f->root=false;else{if(f->father->left==f) f->father->left=t;else f->father->right=t;}t->left=f,f->father=t,f->right=l,l->father=f;update(f);}void splay(NODE *&t){pushdown(t);while(!t->root){if(t->father->root){if(t->father->left==t) zig(t);else zag(t);}else{if(t->father->father->left==t->father){if(t->father->left==t) zig(t->father),zig(t);else zag(t),zig(t);}else{if(t->father->left==t) zig(t),zag(t);else zag(t->father),zag(t);}}}update(t);}void Expose(NODE *&t){NODE *p=t,*q=NIL;while(p!=NIL){splay(p);p->right->root=true;p->right=q;p->right->root=false;update(p);q=p;p=p->father;}}void Modify(int a,int b,int c){Expose(node[a]);NODE *p=node[b],*q=NIL;while(p!=NIL){splay(p);if(p->father==NIL){p->c=c;renew(p->right,c);renew(q,c);}p->right->root=true;p->right=q;p->right->root=false;update(p);q=p;p=p->father;}}void query(int a,int b){Expose(node[a]);NODE *p=node[b],*q=NIL;while(p!=NIL){splay(p);if(p->father==NIL){int ans=q->cnt+p->right->cnt+1;if(p->c==q->lc) ans--;if(p->c==p->right->lc) ans--;printf("%d\n",ans);}p->right->root=true;p->right=q;p->right->root=false;update(p);q=p;p=p->father;}}
}tree;int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&color[i]);for(int i=1;i<n;i++){scanf("%d%d",&a,&b);addedge(a,b);}tree.init();tree.build();while(m--){scanf("%s",s);if(s[0]=='Q'){scanf("%d%d",&a,&b);tree.query(a,b);}else{scanf("%d%d%d",&a,&b,&c);tree.Modify(a,b,c);}}return 0;
}

转载于:https://www.cnblogs.com/Delostik/archive/2011/08/11/2134544.html

【SDOI 2011】Paint 染色相关推荐

  1. [树链剖分][SDOI 2011]染色,Housewife Wind

    文章目录 T1:Housewife Wind 题目 题解 code T2:染色 题目 题解 code 今天选择写这篇博客主要是为了告诉大家一个道理,数组比vectorvectorvector快太多了, ...

  2. 解题: SDOI 2011 染色

    题面 强行把序列问题通过树剖套在树上...算了算是回顾了一下树剖的思想=.= 每次树上跳的时候注意跳的同时维护当前拼出来的左右两条链的靠上的端点,然后拼起来的时候讨论一下拼接点,最后一下左右两边的端点 ...

  3. 信息学奥赛数学一本通 数论相关题目

    ​[数论]教堂 [数论]教堂_LZK1997的博客-CSDN博客 [数论]教堂_AKone123456的博客-CSDN博客 [数论]密码 [数论]密码_)NCuyALnA$Ke的博客-CSDN博客 [ ...

  4. webpack打包css image,webpack 打包CSS 引入图片

    加载css 安装style-loader, css-loader npm install style-loader css-loader --save-dev 配置webpack.config.js文 ...

  5. 转载爱哥自定义View系列--Paint详解

    上图是paint中的各种set方法 这些属性大多我们都可以见名知意,很好理解,即便如此,哥还是带大家过一遍逐个剖析其用法,其中会不定穿插各种绘图类比如Canvas.Xfermode.ColorFilt ...

  6. Paint the Tree CodeForces - 1244D(看似是树,其实是条链)

    目录 题目 官方题解: 百度翻译 题解 ac代码 题目 给多组两顶点连接,得到的图任意三个顶点都是不同的颜色,,给出各顶点染三种颜色的花费,问各店如何染,满足条件情况下,使得花费最少: You are ...

  7. hdu 5023 线段树染色问题

    题目链接 A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/1 ...

  8. 阿里云 超级码力在线编程大赛初赛 第3场 题目2. 房屋染色(DP)

    文章目录 1. 题目 2. 解题 1. 题目 有n个房子在一列直线上,现在Bob需要给房屋染色,共有k种颜色. 每个房屋染不同的颜色费用也不同,Bob希望有一种染色方案使得相邻的房屋颜色不同. 但Bo ...

  9. (四)Paint函数实现QTableView中增加QCheckBox的方法

      第四种方法是:实现QAbstractItemDelegate的paint()函数. 这种方法和<C++_GUI_Qt4_编程(第二版)>中第十章的自定义委托例子,画星星的作法是一样的, ...

最新文章

  1. Nutanix企业云助力广播传媒的融合媒体发展之路
  2. [转]docker 部署 mysql + phpmyadmin 3种方法
  3. 用c#开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)
  4. python datetime timedelta函数_Python Pandas DatetimeIndex.to_perioddelta()用法及代码示例
  5. node.js android 聊天,Node.js实现简单聊天服务器
  6. vb6 打印选项对话框_图纸打印次数太多,不知道哪次才是最新的?用打印戳记区分效果好...
  7. Reactive(1) 从响应式编程到好莱坞
  8. pandas string
  9. Redhat7没有安装ifconfig命令的解决方法
  10. 新一代 Windows 即将发布、GPT-3 成功商业化,这届微软 Build 大会究竟带来了什么?
  11. 谷歌开源 VR 应用
  12. 大学什么专业学c语言和机械制图,机械设计工程师大学时应该学什么
  13. BOS v2.0后台管理系统 JQuery Easyui 相关知识讲解
  14. 软件测试用例常用七大方法
  15. vscode工作区是什么意思_VS Code中的“工作区”是什么?
  16. C语言——if-else语句嵌套
  17. wordpress企业主题
  18. FTP/SFTP客户端:Transmit Mac中文版
  19. windos未能链接服务器,提示Windows没法连接到System Event Notification Service服务
  20. iOS开发学习笔记-C语言学习(一)

热门文章

  1. php继承和重载区别,php继承中方法重载(覆盖)的应用场合
  2. 编写程序,随机产生20个0到1之间的数,将这20个数写入文本文件中,要求每行5个数
  3. dockerfile 安装mysql_dockerfile构建mysql镜像
  4. Java在Linux下创建文件,文件夹,删除,拷贝文件的命令
  5. 学习笔记6-小项目-走迷宫、推箱子
  6. leetcode179. 最大数
  7. 求完全二叉树的结点个数
  8. (四)深入浅出TCPIP之TCP三次握手和四次挥手(下)的抓包分析
  9. mpiBlast安装详解以及使用说明
  10. 机器学习知识总结系列-机器学习中的数学-概率与数理统计(1-3-1)