链接: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. SourceInsight: sourceInsight4.0 修改默认字体

    快捷键 Alt + Y

  2. Oracle数据库学习(一)安装和简单使用

    新公司的新项目,需要用到Oracle数据库,所以现在便来解除此数据库,不得不说,这个数据库还这是麻烦. 安装倒是简单,就是中间会遇到各种问题. 安装步骤参考:https://blog.csdn.net ...

  3. 【iCore4 双核心板_ARM】例程十六:USB_HID实验——双向数据传输

    实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid. ...

  4. 阿里云 FTP 无法读取目录问题

    安全组除了添加制定 FTP端口外 需要加入再添加1024/65535端口 放行策略,ftp被动模式需要随机开一个此范围端口进行传输 添加安全组规则参考文档:https://help.aliyun.co ...

  5. RR算法 调度

    RR算法是使用非常广泛的一种调度算法. 首先将所有就绪的队列按FCFS策略排成一个就绪队列,然后系统设置一定的时间片,每次给队首作业分配时间片.如果此作业运行结束,即使时间片没用完,立刻从队列中去除此 ...

  6. C语言中的字符串处理库函数介绍与实现

    一.介绍 本文将主要介绍字符串处理库函数中的strlen.strcpy.strcat.strcmp.atoi等,主要由<string.h>头文件提供. 二.strlen函数:求字符串的长度 ...

  7. 什么是对象:EVERYTHING IS OBJECT(万物皆对象)

      所有的事物都有两个方面: 有什么(属性):用来描述对象. 能够做什么(方法):告诉外界对象有那些功能. 后者以前者为基础. 大的对象的属性也可以是一个对象.

  8. CentOS命令介绍综合

    1,显示当前使用的shell [root@localhost ~]# echo $SHELL2,显示当前系统使用的所有shell [root@localhost ~]# cat /etc/shells ...

  9. IntelliJ IDEA下spring boot项目打包

    Spring Boot自带Tomcat插件,可以直接编写启动类,开启Tomcat服务 springboot适合前后端分离,打成jar进行部署更合适 application.properties配置端口 ...

  10. nodejs即时通讯模块+SocketIO4Net的使用小结

    实现思路:客户端js连接了nodejs服务,通过.net连接nodejs服务,通过.net发送消息到nodejs,然后通过nodejs将消息推送给(用户)客户端 1.先下载nodejs安装,至于怎么安 ...