Z - New Year Tree

CodeForces - 620E

这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset,

首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树。

然后就是一个区间修改和区间查询。这个区间查询时查询这个区间的种类数。

这个之前写过几个题目也是查区间种类数的

G. Yash And Trees 线段树 bitset

20190709 暑训 区间种类数 莫队的学习

莫队和权值线段树应该都是不支持修改的,所以这个题目用不上,

然后就是这个高端压位卡常容器bitset 我觉得这个题目应该可以用到。

#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <bitset>
#include <algorithm>
#include <map>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
typedef bitset<> bit;
const int maxn = 4e5 + ;
vector<int>G[maxn];
ll el[maxn], er[maxn], a[maxn], tot, num[maxn]; void dfs(int u,int pre)
{
el[u] = ++tot;
num[tot] = u;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if (v == pre) continue;
dfs(v, u);
}
er[u] = tot;
} bit tree[maxn * ];
ll lazy[maxn * ]; void build(int id,int l,int r)
{
lazy[id] = ;
if(l==r)
{
tree[id].reset();
tree[id] = (1ll << (a[num[l]] - ));
return;
}
int mid=(l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
tree[id] = tree[id << ] | tree[id << | ];
} void push_down(int id)
{
if(lazy[id]!=)
{
tree[id << ].reset(); tree[id << | ].reset();
tree[id << ] = (1ll << (lazy[id] - ));
tree[id << | ] = (1ll << (lazy[id] - ));
lazy[id << ] = lazy[id << | ] = lazy[id];
lazy[id] = ;
}
} void update(int id,int l,int r,int x,int y,int val)
{
if(x<=l&&y>=r)
{
tree[id].reset();
tree[id] = (1ll << (val - ));
lazy[id] = val;
return;
}
push_down(id);
int mid = (l + r) >> ;
if (x <= mid) update(id << , l, mid, x, y, val);
if (y > mid) update(id << | , mid + , r, x, y, val);
tree[id] = tree[id << ] | tree[id << | ];
} bit query(int id,int l,int r,int x,int y)
{
if (x <= l && y >= r) return tree[id];
int mid = (l + r) >> ;
bit ans;
ans.reset();
push_down(id);
if (x <= mid) ans = query(id << , l, mid, x, y);
if (y > mid) ans |= query(id << | , mid + , r, x, y);
return ans;
} int main() {
int n, m;
tot = ;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%lld", &a[i]);
for (int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, -);
build(, , n);
while (m--) {
int opt, v, x;
scanf("%d", &opt);
if (opt == ) {
scanf("%d%d", &v, &x);
update(, , n, el[v], er[v], x);
}
else {
scanf("%d", &v);
bit ans = query(, , n, el[v], er[v]);
printf("%d\n", ans.count());
}
}
return ;
}

Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset的更多相关文章

  1. Alyona and a tree CodeForces - 739B (线段树合并)

    大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...

  2. Vasya and a Tree CodeForces - 1076E (线段树 + dfs)

    题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...

  3. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  4. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  5. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  6. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  7. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  8. hdu1698 线段树区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

随机推荐

  1. SIM900A 通过RS232串口进行短信的发送。

    一.基本数据 1.SIM900A模块支持RS232串口和LVTTL串口.保留了232口,在学习或者开发时可以监听51低端单片机和模块指令执行情况,能更快的找出原因,节省开发和学习的时间. 2.此模块供 ...

  2. spring jar 包 用处功能:

    自己积累的: @   spring-context-3.0.5.RELEASE.jar :主要用于 spring程序中加载类 ApplicationContext 用.eq: ApplicationC ...

  3. SpringBoot系列(六)集成thymeleaf详解版

    SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介  1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎.  2. Thymeleaf ...

  4. AJ学IOS(42)UI之核心动画CAAnimationGroup以及其他

    AJ分享,必须精品 效果: 代码: 很简单,不多说,就是把一堆动画放一起,看代码. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent * ...

  5. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  6. 不使用 if-elif 语句,如何优雅地判断某个数字所属的等级?

    偶然看到了 stackoverflow 上的一个问题,还挺有启发,故分享一下. 题目大意是:有从 A 到 F 的 5 个等级,现要判断某个数值(从 0 到 1 之间)所属的等级.举例,如数值 > ...

  7. python爬取《龙岭迷窟》的数据,看看质量剧情还原度到底怎么样

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:简单 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行 ...

  8. python实现双向链表的操作

    双向链表 双向链表又叫做双链表,每个节点有两个指针域和一个数据域.prev指针域指向前一个节点,next指针域指向下一个节点.注意,第一个节点的prev指针域指向空值,最后一个节点的next域也是指向 ...

  9. PHP出现SSL certificate:unable to get local issuer certificate的解决办法

    当本地curl需要访问https时,如果没有配置证书,会出现SSL certificate: unable to get local issuer certificate错误信息. 解决办法: 1.下 ...

  10. [PHP] 文件创建、写入、读取

    创建$p = fopen('text.txt','a+b'); 写入第一种方式//var_export方式存储数组到文件中 //这中方式存浮点型数据,存储后会多很多数字!只适合简单的存储吧!我感觉! ...