链接: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-统计颜色 线段树的更多相关文章

  1. 2018年湘潭大学程序设计竞赛 H统计颜色

    链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  2. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  3. P3939 数颜色 线段树动态开点

    P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...

  4. 洛谷P4065 [JXOI2017]颜色(线段树)

    题意 题目链接 Sol 线段树板子题都做不出来,真是越来越菜了.. 根据题目描述,一个合法区间等价于在区间内的颜色没有在区间外出现过. 所以我们可以对于每个右端点,统计最长的左端点在哪里,刚开始以为这 ...

  5. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. [JXOI2017]颜色 线段树扫描线 + 单调栈

    ---题面--- 题解: 首先题目要求删除一些颜色,换个说法就是要求保留一些颜色,那么观察到,如果我们设ll[i]和rr[i]分别表示颜色i出现的最左边的那个点和最右边的那个点,那么题目就是在要求我们 ...

  7. P3703 [SDOI2017]树点涂色 LCT维护颜色+线段树维护dfs序+倍增LCA

    \(\color{#0066ff}{ 题目描述 }\) Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点 ...

  8. 7.18 NOI模拟赛 因懒无名 线段树分治 线段树维护直径

    LINK:因懒无名 20分显然有\(n\cdot q\)的暴力. 还有20分 每次只询问一种颜色的直径不过带修改. 容易想到利用线段树维护直径就可以解决了. 当然也可以进行线段树分治 每种颜色存一下直 ...

  9. BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队

    题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...

随机推荐

  1. Vue 温故而知新 props如何双向属性绑定

    传送门:https://cn.vuejs.org/v2/guide/components-custom-events.html https://segmentfault.com/q/101000001 ...

  2. 经典的sql语句,将返回结果合并为一个字符串

    declare @ts varchar(999) select @ts=isnull(@ts+',','')+name from sysobjects where xtype='U' select @ ...

  3. Gradle 的下载安装配置以及创建第一个Gradle 项目

    1. 什么是Gradle? Gradle是一个开源的构建自动化工具,专注于灵活性和性能. Gradle构建脚本使用Groovy或Kotlin DSL编写. 阅读Gradle功能,了解Gradle的功能 ...

  4. python prettytable模块

    简介 Python通过PrettyTable模块可以将输出内容如表格方式整齐地输出. 安装 pip install prettytable 1 示例 from prettytable import P ...

  5. Could not load file or assembly 'System.Core, Version=2.0.5.0

    项目中用的4.0,本机没问题,服务器上出现问题,查了一下发现  autofac 里面用的这个版本 解决:下载安装这个4.0的补丁 http://support.microsoft.com/kb/246 ...

  6. left join、right join、inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  7. form表单下的button按钮会自动提交表单的问题

    form表单下的button按钮会自动提交表单的问题 2017年01月05日 18:02:44 蓝色水 阅读数:18012更多 个人分类: asp.net   form表单下的按钮在没有指定type类 ...

  8. WPF Input Validation Using MVVM

    Data validation is a key part in WPF.Validation is used to alert the user that the data he entered i ...

  9. qt在GUI显示时,将调试信息输出到控制台的设置

    1. 在.pro文件中添加一下设置: CONFIG += console 2. 项目的[构建和运行]中,需要勾选[Run in terminal]:

  10. (原)测试 Java中Synchronized锁定对象的用法

    今天再android_serial_port中看到了关键字 synchronized;因为刚好在学java和android,所以就查了一下它的用法: 于是把代码中的一小段代码拿了出来,做了一下修改,测 ...