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& ...
随机推荐
- mysql 的CURDATE() 与 NOW() 的区别
SELECT CURDATE() 查询出的是当前天的开始时间点,比如今天是 2015.02.03号,那不管我在今天什么时间点查询,结果都是今天的凌晨,即今天的开始的那个时间点,因为它只具体到年月日,没 ...
- 逻辑对象中时间类型 保存时 隐藏bug
开发功能中的一些逻辑对象中的一些时间 属性,在保存数据库时有一个隐藏的bug,假如 我vo属性定义的就是date 类型,那我定时保存数据库时可能就会出错,eq:假如这个属性隔天要重置一些东西,表中这个 ...
- shell执行${var:m:n}报错Bad substitution解决办法
Ubuntu系统下,执行字符串截取脚本时,总是报错:Bad substitution,脚本非常简单如下: #!/bin/sh str1="hello world!" :} 执行后报 ...
- 常见的 PHP 面试题和答案分享
如何直接将输出显示给浏览器? 将输出直接显示给浏览器,我们必须使用特殊标记 <?=and?>. PHP 是否支持多重继承? PHP 只支持单继承.PHP 的类使用关键字 extends 继 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects
The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...
- SpringBoot 集成 Elasticsearch
前面在 ubuntu 完成安装 elasticsearch,现在我们SpringBoot将集成elasticsearch. 1.创建SpringBoot项目 我们这边直接引入NoSql中Spring ...
- 曹工说Redis源码(7)-- redis server 的周期执行任务,到底要做些啥
文章导航 Redis源码系列的初衷,是帮助我们更好地理解Redis,更懂Redis,而怎么才能懂,光看是不够的,建议跟着下面的这一篇,把环境搭建起来,后续可以自己阅读源码,或者跟着我这边一起阅读.由于 ...
- Ansible Facts 变量详解
Ansible Facts 变量详解与使用案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放: 3. ...
- 基于 HTML WebGL 的会展中心智能监控系统
前言 随着近几年物联网.万物互联等诸多概念的大行其道,智慧城市的概念也早已经被人们耳熟能详,而作为城市的组成部分,智慧建筑也是重中之重,智慧园区,智慧小区等也如雨后春笋般的相继出现. 智慧建筑是指通过 ...
- go的 三个点 ...
这三个点,比较任性,可前可后,可攻可守... 举2个栗子: 1.func sub(arg ...int) (total int){} 2.argsArr = apend(argsArr[:3], ar ...