Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree
这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset,
首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树。
然后就是一个区间修改和区间查询。这个区间查询时查询这个区间的种类数。
这个之前写过几个题目也是查区间种类数的
莫队和权值线段树应该都是不支持修改的,所以这个题目用不上,
然后就是这个高端压位卡常容器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的更多相关文章
- Alyona and a tree CodeForces - 739B (线段树合并)
大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...
- 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 ...
- 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 ...
- 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 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序加上一个线 ...
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
随机推荐
- vue 本地调试跨域---带cookies(axios)
cookise跨域第二期之便捷优雅的本地调试(axios) 1.打开config/index.js,在proxyTable中添写如下代码: proxyTable: { '/agent': { //使用 ...
- Docker+Cmd+Cli+Git之前端工程化纪要(二)自定义类package.json文件管理模块包
全新升级后的FE工作流为:使用FE命令包进行项目的初始化,其中包括项目初始化.拉取脚手架.私库拉取模块包或后期扩展的CI/CD等与本公司工作流相关的操作. 出现的问题如下: 脚手架工具的包依赖信息存放 ...
- Tesseract-ocr 安装配置
参考:https://jingyan.baidu.com/article/219f4bf788addfde442d38fe.html 1.下载图形识别工具Tesseract-ocr,下载路径https ...
- U - Obtain a Permutation CodeForces - 1294E 思维
题解: 注意每一列与每一列之间互不影响,所以贪心地求出没一列的最小操作值,然后累加起来. 怎么求没一列的最小值呢?维护一个数组same表示其中same[i]=j表示将该序列向上翻滚i次有j个元素归位, ...
- 一起了解 .Net Foundation 项目 No.24
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Xamarin.Mobil ...
- 4. git log的常见用法
git log ======见https://blog.csdn.net/daguanjia11/article/details/73823617 +++++++++++++++++++++++ 使用 ...
- 泛微ecology OA系统在数据库配置信息泄露
漏洞描述 攻击者可通过该漏洞页面直接获取到数据库配置信息,攻击者可通过访问存在漏洞的页面并解密从而获取数据库配置信息,如攻击者可直接访问数据库,则可直接获取用户数据,由于泛微e-cology默认数据库 ...
- [Abp vNext 入坑分享] - 3.简单的用户模块功能开发
一.简要说明 本篇文章开始进行业务模块的开发模拟,借助user模块来进行业务开发,主要是用户相关的基础操作.主要是先使用Users来体验整个开发的流程.主要是先把一个基础流程跑顺利,在这里我并不会过于 ...
- 转:handler.post 为什么要将thread对象post到handler中执行呢?
转载网址:http://blog.csdn.net/fei0724/article/details/8664462在Android中使用Handler和Thread线程执行后台操作 对于线程的控制,我 ...
- 安卓微信浏览器中window.location.href失效的问题
最近接手一微信项目,测试功能时,发现跳转在android手机上不动了.iso系统可以正常跳转的.解决方法: window.location.href = url + '?v=' + (new Date ...