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. Dubbo学习系列之十八(Skywalking服务跟踪)

    我们知道,微服务不是独立的存在,否则就不需要微服务这个架构了,那么当发起一次请求,如何知道这次请求的轨迹,或者说遇到响应缓慢. 请求出错的情况,我们该如何定位呢?这就涉及到APM(Applicatio ...

  2. Mitmproxy教程

    本文是一个较为完整的 mitmproxy教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 python 知识,且已经安装好了一个 python 3 开 ...

  3. E - Farthest Nodes in a Tree

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. Th ...

  4. Candy Distribution

    Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...

  5. api测试用例(编写思路)

    在API的自动化测试维度中,测试维度分为两个维度,一个是单独的对API的验证,客户端发送一个请求后,服务端得到客户端的请求并且响应回复给客户端: 另外一个维度是基于业务场景的测试,基于业务场景的也就是 ...

  6. [javascript]各种页面定时跳转(倒计时跳转)代码总结

    (1)使用setTimeout函数实现定时跳转(如下代码要写在body区域内) <script type="text/javascript"> //3秒钟之后跳转到指定 ...

  7. 基于nodejs的游戏服务器

    开源一个四年前自己写的node服务器,有兴趣的可以继续开发-- 架构为mysql,redis,node. 数据格式为 protocol buff 如果只做简单的演示,这个架构非常适合你.. 还是typ ...

  8. beego rel/reverse

    用户可以发布多个文章 对用户来说是一对多 对文章来说是多对一 用户是主表 文章是用户的从表 rel用在从表中,给主表结构体设置主键,也就是文章表对应用户表的外键 reverse用在主表中,指定主表与从 ...

  9. php---算法和数据结构

    <?php header("content-type:text/html;charset=utf-8"); $arr = array(3,5,8,4,9,6,1,7,2); ...

  10. docker-数据管理(3)

    Docker 容器中管理数据主要有两种方式: 数据卷(Data volumes) 数据卷容器(Data volumes containers 数据卷是一个可供一个或者多个容器使用的特殊目录,它绕过UF ...