Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 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序+线段树的更多相关文章
- Codeforces 838B - Diverging Directions - [DFS序+线段树]
题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...
- 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 ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- 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 ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
随机推荐
- php-css外边距
css 基本语法 selector{declaration1;declaration2;....delecrationN;} (选择器和一条或多条声明) 选择器为需要改变样式的html元素,每条声 ...
- javascript基础01
javascript基础01 Javascript能做些什么? 给予页面灵魂,让页面可以动起来,包括动态的数据,动态的标签,动态的样式等等. 如实现到轮播图.拖拽.放大镜等,而动态的数据就好比不像没有 ...
- git 教程(15)--分支管理策略
通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的comm ...
- WindowsForm公共控件--2016年12月5日
Button text:修改按钮显示的文字 CheckBox Checked:是否选中 CheckedListBox checkedListBox.Items.Add(显示的值,初始选中状态); ch ...
- WCF X.509验证
1.证书的制作 makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=ParkingServer -sky exchange -pe makecert. ...
- .NET 多线程
多线程 在一个程序中,这些独立运行的程序片断叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”.多线程处理一个常见的例子就是用户界面.利用线程,用户可按下一个按钮,然后程序会立即作出响 ...
- 如何从零基础学习VR
转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 近期很多搞技术的朋友问我,如何步入VR的圈子?如何从零基础系统性的学习VR技术? 本人将于2017年1月 ...
- 如何用C#+WinRAR 实现压缩 分类:
前提:必须安装 WinRAR 1. 工具类 using System; using System.Diagnostics; using System.IO; using Microsoft.Win32 ...
- Arch Linux 安装博通 BCM4360 驱动(Arch Linux, Ubuntu, Debian, Fedora...)
BCM4360 在2010年9月,博通完全开源的硬件驱动[1].该驱动程序 brcm80211已被列入到自2.6.37之后的内核中.随着2.6.39发布,这些驱动程序已被重新命名为 brcmsmac和 ...
- RSA的傻瓜原理
设想一下,这种场景,你处在一个狭小的房间里,房间之中有一共有三个人,你是其中一个,你们之间被玻璃隔开,两两对视,现在给另外两个人起个名字叫小红.小明.你和他们通讯只能用写字板来展示给对方(简单起见,只 ...