链接: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. redis-desktop-manager 的简单使用

    1:安装比较简单,所有软件几乎都一样(下载.安装)我就从安装好后,怎么玩记录吧!如下图,双击对应的图标就能打开此软件了 2-1:连接redis服务器的方式之一——导入对应的redis信息 连接配置的样 ...

  2. 2013-2015 Aaronyang的又一总结,牧童遥指纳尼村

    我没有时间去唠叨自己的事,可是你们是我喜欢的人,ay很愿意写给你们分享:去年的万人阅读的总结链接:<没学历的IT人生没那么悲催,献给程序员们> 提前声明:本文不良反应:请自备垃圾桶,准备装 ...

  3. 11g新特性-自动sql调优(Automatic SQL Tuning)

    11g新特性-自动sql调优(Automatic SQL Tuning) 在Oracle 10g中,引进了自动sql调优特性.此外,ADDM也会监控捕获高负载的sql语句. 在Oracle 11g中, ...

  4. mark 阿里支付

    开源软件企业版特惠高校版博客 我的码云 ·· 8月18日(周六)成都源创会火热报名中,四位一线行业大牛与你面对面,探讨区块链技术热潮下的冷思考. 开源项目 > WEB应用开发 > Web开 ...

  5. xml解析、写入遇到的坑

    前言 最近在看一个线上xml文件导出的问题,需求如下: 从我们平台导出一个后缀为tmx的术语语料数据(实际内容为xml文件),然后导入到其他第三方平台发现无法导入. 从其他平台导入的tmx文件无法导入 ...

  6. 源码分析HotSpot GC过程(一)

    «上一篇:源码分析HotSpot GC过程(一)»下一篇:源码分析HotSpot GC过程(三):TenuredGeneration的GC过程 https://blogs.msdn.microsoft ...

  7. html网页采集

    UI_Less.pas: unit UI_Less; interface uses Windows, Classes, Messages, Forms, MsHtml, Urlmon, ActiveX ...

  8. 带cookie跨域问题的思路以及echo的解决方案

    问题起因 前后端分离,前端要访问后端资源,而且需要携带cookie信息,这时碰到了跨域问题.一开始以为设置为允许跨域allow_origins为即可.可是浏览器还是拦截的请求,于是查看跨域规则,原来跨 ...

  9. 【iCore4 双核心板_FPGA】例程十五:基于单口RAM的ARM+FPGA数据存取实验

    实验现象: 写RAM命令格式:write:地址(0-255),数据(0-65535)\cr\lf 读RAM命令格式:read:地址(0-255)\cr\lf 核心代码: int main(void) ...

  10. CMS 01

    环境搭建 工具 sublime mysql 5.7, 数据库管理 Navicat django 1.10, django shell (可以用来检查错误) 操作系统, windows 7 搭建 dja ...