湘潭大学校赛H-统计颜色 线段树
链接:https://www.nowcoder.com/acm/contest/105/H
来源:牛客网
n个桶按顺序排列,我们用1~n给桶标号。有两种操作:
1 l r c 区间[l,r]中的每个桶中都放入一个颜色为c的球 (1≤l,r ≤n,l≤r,0≤c≤60)
2 l r 查询区间[l,r]的桶中有多少种不同颜色的球 (1≤l,r ≤n,l≤r)
ac代码:
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
using namespace std;
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e5 + ;
ll tree[maxn * ], lazy[maxn * ];
void pushdown(ll root) {
tree[root << ] |= lazy[root];
tree[root << | ] |= lazy[root];
lazy[root << ] |= lazy[root];
lazy[root << | ] |= lazy[root];
lazy[root] = ;
}
void update(ll l, ll r, ll L, ll R, ll root, ll clr) {
if (l <= L&&r >= R) {
tree[root] |= clr;
lazy[root] |= clr;
return;
}
ll mid = L + R >> ;
if (lazy[root])pushdown(root);
if (r <= mid)update(l, r, L, mid, root << , clr);
else if (l > mid)update(l, r, mid+, R, root << | , clr);
else {
update(l, mid, L, mid, root << , clr);
update(mid + , r, mid + , R, root << | , clr);
}
tree[root] = tree[root << ] | tree[root << | ]; }
ll query(ll l, ll r, ll L, ll R, ll root, ll clr) {
if (l <= L&&r >= R) { return tree[root];
}
ll mid = L + R >> ;
if (lazy[root])pushdown(root);
if (r <= mid)return query(l, r, L, mid, root << , clr);
else if (l > mid)return query(l, r, mid+ , R, root << | , clr);
else {
return query(l, mid, L, mid, root << , clr)| query(mid + , r, mid + , R, root << | , clr);; }
tree[root] = tree[root << ] | tree[root << | ]; }
int main()
{
ios::sync_with_stdio(false);
int n, m;
while (cin >> n >> m) {
mmm(tree, );
mmm(lazy, );
rep(i, , m) {
ll x, l, r, c;
cin >> x;
if (x == ) {
cin >> l >> r >> c;
update(l, r, , n, , 1ll << c); }
if (x == ) {
cin >> l >> r;
ll ans = query(l, r, , n,, );
int res = ;
while (ans) {
if (ans & )res++;
ans >>= ;
}
cout << res << endl;
}
}
} }
tle代码
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
// ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
#define _CRT_SECURE_NO_WARNINGS const int maxn = 1e5 + ;
int a[maxn], n, q, l, r, val;
typedef long long ll;
struct node {
int l, r;
long long sum, lazy;
void update(long long x) {
//sum += (ll)1 * (r - l + 1)*x;
//lazy += x;
sum |= x;
lazy |= x;
}
}tree[maxn * ];
void push_up(int x) {
//tree[x].sum = tree[x << 1].sum + tree[x << 1 | 1].sum;
tree[x].sum = tree[x << ].sum | tree[x << | ].sum;
}
void push_down(int x) {
int lazyval = tree[x].lazy;
if (lazyval) {
tree[x << ].update(lazyval);
tree[x << | ].update(lazyval);
tree->lazy = ;
}
}
void build(int x, int l, int r) {
tree[x].l = l; tree[x].r = r;
tree[x].sum = tree[x].lazy = ;
if (l == r) {
tree[x].sum = ;
}
else {
int mid = l + r >> ;
build(x << , l, mid);
build(x << | , mid + , r);
push_up(x);
}
}
void update(int x, int l, int r, long long val) {
int L = tree[x].l, R = tree[x].r;
if (l <= L&&r >= R) {
tree[x].update(val);
}
else {
push_down(x);
int mid = L + R >> ;
if (mid >= l)update(x << , l, r, val);
if (r > mid)update(x << | , l, r, val);
push_up(x);
}
}
long long query(int x, int l, int r) {
int L = tree[x].l, R = tree[x].r;
if (l <= L&&r >= R) {
return tree[x].sum;
}
else {
push_down(x);
long long ans = ;
int mid = L + R >> ;
if (mid >= l)ans |= query(x << , l, r);
if (r > mid)ans |= query(x << | , l, r);
push_up(x);
return ans;
}
} void Run() { } void smain() {
cin >> n >> q; {
build(, , n);
rep(i, , q) {
int x;
scanf("%d", &x);
//cin >> x;
if (x == ) {
int l, r, c;
scanf("%d%d%d", &l, &r,&c);
//cin >> l >> r >> c;
update(, l, r, << c); }
else {
int l, r; scanf("%d%d", &l, &r);
//cin >> l >> r;
ll res= query(, l, r);
int cnt=; while (res) {
if (res & )cnt++;
res >>= ;
}
printf("%d\n", cnt);
//cout << cnt << endl;
}
}
}
}
湘潭大学校赛H-统计颜色 线段树的更多相关文章
- 2018年湘潭大学程序设计竞赛 H统计颜色
链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- P3939 数颜色 线段树动态开点
P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...
- 洛谷P4065 [JXOI2017]颜色(线段树)
题意 题目链接 Sol 线段树板子题都做不出来,真是越来越菜了.. 根据题目描述,一个合法区间等价于在区间内的颜色没有在区间外出现过. 所以我们可以对于每个右端点,统计最长的左端点在哪里,刚开始以为这 ...
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- [JXOI2017]颜色 线段树扫描线 + 单调栈
---题面--- 题解: 首先题目要求删除一些颜色,换个说法就是要求保留一些颜色,那么观察到,如果我们设ll[i]和rr[i]分别表示颜色i出现的最左边的那个点和最右边的那个点,那么题目就是在要求我们 ...
- P3703 [SDOI2017]树点涂色 LCT维护颜色+线段树维护dfs序+倍增LCA
\(\color{#0066ff}{ 题目描述 }\) Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点 ...
- 7.18 NOI模拟赛 因懒无名 线段树分治 线段树维护直径
LINK:因懒无名 20分显然有\(n\cdot q\)的暴力. 还有20分 每次只询问一种颜色的直径不过带修改. 容易想到利用线段树维护直径就可以解决了. 当然也可以进行线段树分治 每种颜色存一下直 ...
- BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队
题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...
随机推荐
- Python 贝叶斯分类
很久的时间没有更新了,一是因为每天加班到比较晚的时间,另外,公司不能上网,回家后就又懒得整理,最近在看机器学习实战的书籍,因此才又决定重新拾起原先的博客! 今天讲的是第三章的贝叶斯分类方法,我们从一个 ...
- 小型IT部门建设之我见
关于IT团队建设,一般的原则是四大块组成: 1:业务分析师,对接业务部门,把业务需求转变成技术需求 专注于BRS( Business Requirement Speci ...
- Spring Boot 2.0 返回JSP页面实战
1. 模板引擎JSP的限制 在开始之前呢,我觉得我们有必要先去了解下 Spring Boot 2.0 官方文档中提到的如下内容: 模板引擎 除了REST Web服务之外,还可以使用Spring MVC ...
- flink source code
https://github.com/apache/flink/tree/master/docs https://github.com/flink-china/1.6.0 https://github ...
- LeetCode: Next Permutation 解题报告
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- layui table 前台数字格式保留两位小数,不足补0(mysql 数据库)
layui table 对于后台json数据,有数字的,默认不会原样显示,而是只取数值,即100.00显示为100.如果想原样显示,需转为字符串. 项目采用mysql数据库,字段类型为decimal( ...
- Java知多少(75)Object类
Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: p ...
- React Native 项目整合 CodePush 全然指南
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/y4x5M0nivSrJaY3X92c/article/details/81976844 作者 | 钱 ...
- 【Docker】退出容器和进入容器
运行容器:docker run -it 镜像名 /bin/bash 退出容器: exit 或者 Ctrl+P+Q 查看容器:docker ps -a 查看运行的容器:docker ps 重启容器:do ...
- Rsync实现多台Windows工作电脑文件同步
你要准备的软件有: 最新版 Rsync for windows 服务端:cwRsync_Server_2.1.5_Installer.zip 客户端:cwRsync_2.1.5_Installer.z ...