题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色

比较容易想到dfs序+线段树去做

dfs序是很久以前看的bilibili上电子科技大学发的视频学习的 将一颗树通过dfs编号的方式 使每个点的子树的编号连在一起作为相连的区间 就可以配合线段树搞子树

因为以前好像听说过 线段树可以解决一种区间修改和查询区间中不同的xx个数...所以一下子就想到了...

但是我不会写线段树..只会最简单的单点修改区间查询...不会用延迟标记...所以拿出书现学了一发..

问学长怎么记录不同的颜色个数 学长就机智的告诉了我用longlong来搞..

虽然知道了思路..还是写了一天多..

dfs序 做出每个点的编号 并且记录每个点的子树的编号的左右区间

初始进行crea的时候 进行赋值并且pushup

利用longlong的64位来存有哪种颜色 利用或来做转移

1L<<50 什么的 好像左移过了多少就崩了..要在外边定义L的变量..哭..

写线段树好累...(脸上挂满泪痕)...不过桃桃的小粉红敲起来好舒服...

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
#define L long long
int n , m ;
struct node{
int l,r;
L ma;
};
int a[400050];
node tree[400050*8];
vector<int >q[400050];
int id[400050];
L mark[400050*8];
int cnt ;
struct no{
int l,r;
};
no zg[400050];
int fx[400050];
void dfs(int u){
id[u] = ++cnt;
fx[cnt] = u;
zg[u].l = cnt;
for(int i=0;i<q[u].size();i++){
int v = q[u][i];
if(id[v] == -1){
dfs(v);
}
}
zg[u].r = cnt;
}
void pushup(int root){
tree[root].ma = tree[root*2].ma | tree[root*2+1].ma;
}
void pushdown(int root){
if(mark[root] != -1){
mark[root*2] = mark[root*2+1] = mark[root];
tree[root].ma = mark[root];
tree[root*2].ma = tree[root*2+1].ma = mark[root];
mark[root] = -1;
}
}
void crea(int root ,int l,int r){
tree[root].l = l;
tree[root].r = r;
if(l == r){
L res = 1;
res <<= a[fx[l]];
tree[root].ma = (res);
return ;
}
int m = (l + r) >> 1;
crea(root*2,l,m);
crea(root*2+1,m+1,r);
pushup(root);
}
void upda(int root , int l , int r ,int c){
if(tree[root].r < l || tree[root].l > r){
return ;
}
if(tree[root].r <= r && tree[root].l >= l){
L res = 1;
res <<= c;
mark[root] = (res);
pushdown(root);
return ;
}
pushdown(root);
upda(root*2,l,r,c);
upda(root*2+1,l,r,c);
pushup(root);
}
L query(int root ,int l , int r){
pushdown(root);
if(tree[root].r < l || tree[root].l > r){
return 0;
}
if(tree[root].r <= r && tree[root].l >= l){
return tree[root].ma;
}
L ans = 0;
ans |= query(root*2,l,r);
ans |= query(root*2+1,l,r);
pushup(root);
return ans ;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
q[i].clear();
}
cnt = 0;
memset(id,-1,sizeof(id));
for(int i=1;i<=n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
q[v].push_back(u);
q[u].push_back(v);
}
memset(mark,-1,sizeof(mark));
dfs(1);
crea(1,1,cnt);
for(int i=1;i<=m;i++){
int k ;
scanf("%d",&k);
if(k == 1){
int v,c;
scanf("%d%d",&v,&c);
int ll = zg[v].l;
int rr = zg[v].r;
upda(1,ll,rr,c);
}
else {
int v;
scanf("%d",&v);
int ll = zg[v].l;
int rr = zg[v].r;
L res = query(1,ll,rr);
int ans = 0;
while(res > 0){
ans += (res %2);
res >>= 1;
}
printf("%d\n",ans);
}
}
}

  

Educational Codeforces Round 6 E dfs序+线段树的更多相关文章

  1. Codeforces 838B - Diverging Directions - [DFS序+线段树]

    题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...

  2. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  4. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  5. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  6. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  7. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  8. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  9. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

随机推荐

  1. Linux 网络子系统

    今天记录一下Linux网络子系统相关的东西. 因为感觉对这一块还是有一个很大的空白,这件事情太可怕了. 摘抄多份博客进行总结一下Linux网络子系统的相关东西. 一. Linux网络子系统体系结构 L ...

  2. 如何用angularjs给从后台传来数据添加链接

    <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF- ...

  3. python——连接Oracle数据库

    前言 Python自带的模块中有很多操纵文件的.我们可以把文件的数据读出来,经过处理还可以将数据写入文件中.但是对于数据的管理和分析来说,数据库还是专业一些.如果Python能和数据库结合在一起,那么 ...

  4. 8.7 jquery-dom manipulation

    // 获得设定内容 [text(),html(),val()]; // 获得设定属性 [attr(),removeAttr()]; // 获得设定 css class [addClass,remove ...

  5. RobotFrameWork(三)数据类型

    1.1 数字变量 执行结果: 1.2 布尔变量和None/null 执行结果: 1.3 字符串.元组.list和字典 执行结果: 1.4 space和empty 执行结果:

  6. With great power comes great responsibility

    We trust you have received the usual lecture from the local SystemAdministrator. It usually boils do ...

  7. .NET LINQ 元素操作

    元素操作      元素操作从一个序列返回单个特定元素. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 ElementAt 返回集合中指定索引处的元素. ...

  8. MVC+Easeyui dialog的小问题

    今天在尝试 MVC+Easyui的练习中遇到的一些,小问题. 在.net MVC 中  在_layout.cshtml中设置Easyui 环境  ,在传到子页中,发现$("#dlg" ...

  9. .Net自带缓存Cache的使用

    对于数据比较大,经常要从数据库拿出来用的,可以考虑使用.Net自带的缓存Cache,简单好用: //向内存中插入一个缓存 System.Web.HttpRuntime.Cache.Insert(&qu ...

  10. TCP协议三次握手和四次挥手

    http://www.cnblogs.com/rootq/articles/1377355.html TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对 ...