Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree
考虑到$ck <= 60$,那么用位运算统计颜色种数
对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询。
用线段树来维护。
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, a, b) for (int i(a); i <= (b); ++i)
- struct node{
- long long num, lazy;
- } tree[ << ];
- struct Node{
- int l, r;
- } e[];
- vector <int> v[];
- int n, m;
- long long val[], c[];
- int Time;
- bool vis[];
- long long ans, cover;
- int op;
- int x, y;
- void dfs(int x, int fa){
- e[x].l = ++Time;
- val[Time] = c[x];
- vis[x] = true;
- for (auto u : v[x]){
- if (u == fa) continue;
- dfs(u, x);
- }
- e[x].r = Time;
- }
- inline void pushup(int i){
- tree[i].num = tree[i << ].num | tree[i << | ].num;
- }
- inline void pushdown(int i){
- if (tree[i].lazy){
- tree[i << ].num = tree[i << | ].num = (1LL << tree[i].lazy);
- tree[i << ].lazy = tree[i << | ].lazy = tree[i].lazy;
- tree[i].lazy = ;
- }
- }
- void build(int i, int l, int r){
- tree[i].lazy = ;
- if (l == r){
- tree[i].num = (1LL << val[l]);
- return ;
- }
- int mid = (l + r) >> ;
- build(i << , l, mid);
- build(i << | , mid + , r);
- pushup(i);
- }
- void update(int i, int L, int R, int l, int r, long long cover){
- if (l <= L && R <= r){
- tree[i].lazy = cover;
- tree[i].num = (1LL << cover);
- return ;
- }
- int mid = (L + R) >> ;
- pushdown(i);
- if (l <= mid) update(i << , L, mid, l, r, cover);
- if (r > mid) update(i << | , mid + , R, l, r, cover);
- pushup(i);
- }
- void solve(int i, int L, int R, int l, int r){
- if (l <= L && R <= r){
- ans |= tree[i].num;
- return;
- }
- pushdown(i);
- int mid = (L + R) >> ;
- if (l <= mid) solve(i << , L, mid, l, r);
- if (r > mid) solve(i << | , mid + , R, l, r);
- }
- int main(){
- scanf("%d%d", &n, &m);
- rep(i, , n) v[i].clear();
- rep(i, , n) scanf("%lld", c + i);
- rep(i, , n - ){
- scanf("%d%d", &x, &y);
- v[x].push_back(y);
- v[y].push_back(x);
- }
- memset(vis, , sizeof vis); Time = ;
- dfs(, );
- build(, , n);
- rep(i, , m){
- scanf("%d%d", &op, &x);
- if (op == ){
- scanf("%lld", &cover);
- update(, , n, e[x].l, e[x].r, cover);
- }
- else{
- ans = ;
- solve(, , n, e[x].l, e[x].r);
- int ret = ;
- for (; ans; ans -= ans & -ans) ++ret;
- printf("%d\n", ret);
- }
- }
- return ;
- }
Codeforces 620E New Year Tree(线段树+位运算)的更多相关文章
- CodeForces 620E New Year Tree(线段树的骚操作第二弹)
The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited h ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- hdu 5023 线段树+位运算
主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- poj 3225 线段树+位运算
略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...
- POJ 2777 Count Color(线段树+位运算)
题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...
- poj_2777线段树+位运算
第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
随机推荐
- OwinStartupAttribute出错
尝试加载应用时出现了以下错误.- 找不到包含 OwinStartupAttribute 的程序集.- 找不到包含 Startup 或 [AssemblyName].Startup 类的程序集.若要禁用 ...
- thinkphp5开发restful-api接口学习 笔记二
目录 第4节 为api项目搭建数据库 第5节 使用markdown书写接口文档 第6节(判断数据库中是否有此用户) 第7节 为项目配置URL 需求分析 配置主域名和二级域名 使用tp5路由进行URL解 ...
- Python学习笔记:输入输出,注释,运算符,变量,数字类型,序列,条件和循环控制,函数,迭代器与生成器,异常处理
输入输出 输入函数input()和raw_input() 在Python3.x中只有input()作为输入函数,会将输入内容自动转换str类型: 在Python2.x中有input()和raw_inp ...
- (转)git常见错误
error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error: insufficient pe ...
- hdu-2544 最短路(最短路)
Time limit1000 ms Memory limit32768 kB 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到 ...
- German Collegiate Programming Contest 2015
// Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...
- Mysql登陆、退出、更改环境编码
登录: mysql -h[数据库地址] -u[username] -p[password] -P[端口] //大写P表示端口,小写p表示密码 例如:mysql -hlocalhost -uroot ...
- 【15】ES6 for Humans: The Latest Standard of JavaScript: ES2015 and Beyond
[15]ES6 for Humans 共148页: 目前看到:已经全部阅读. 亚马逊地址: 魔芋:总结: 我先看的是阮一峰的在线书籍.这本书的内容很多都与之重复的. 居然卖¥463.也是没谁了. ...
- Vue简单了解
目录 1. 前端概览 2. 现代前端开发方式 3. MVVM开发核心 4. Vue核心 5. Vue优点 6. Vue难点 7. Vue与非Vue项目结合 8. Vue调试 9. Vue与SEO 今天 ...
- 令人惊叹的Npm工具包
1.http-server (简单搭建http服务器) 2.json-server (JSON服务器,快速搭建resful api接口) 3.cssnano (css多功能优化工具) PS:比uncs ...