CDOJ 1259 昊昊爱运动 II bitset+线段树
昊昊喜欢运动
他N天内会参加M种运动(每种运动用一个[1,m]的整数表示)
现在有Q个操作,操作描述如下
- 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m])
- 问昊昊第l天到第r天参加了多少种不同的运动
Input
输入两个数N, M (1≤N≤105, 1≤M≤100);
输入N个数ai(ai∈[1,m])表示在第i天昊昊做了第ai类型的运动;
输入一个数Q(1≤Q≤105);
输入Q行 每行描述以下两种操作
- 形如
M l r x,表示昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) - 形如
Q l r,表示昊昊想知道他第l天到第r天参加了多少种不同的运动
Output
对于所有的Q操作,每一行输出一个数 表示昊昊在第l天到第r天一共做了多少种活动
Sample input and output
| Sample Input | Sample Output |
|---|---|
5 3 |
3 |
每一个节点开一个bitset维护就可以, 区间更新区间查询。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int lazy[maxn<<], a[maxn<<];
bitset <> s[maxn<<];
void pushUp(int rt) {
s[rt] = s[rt<<]|s[rt<<|];
}
void pushDown(int rt) {
if(lazy[rt]) {
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
a[rt<<] = a[rt<<|] = lazy[rt];
s[rt<<].reset();
s[rt<<|].reset();
s[rt<<][lazy[rt]] = s[rt<<|][lazy[rt]] = ;
lazy[rt] = ;
return ;
}
}
void build(int l, int r, int rt) {
if(l == r) {
scanf("%d", &a[rt]);
lazy[rt] = ;
s[rt][a[rt]] = ;
return ;
}
int m = l+r>>;
build(lson);
build(rson);
pushUp(rt);
}
void update(int val, int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
a[rt] = val;
s[rt].reset();
s[rt][val] = ;
lazy[rt] = val;
return ;
}
pushDown(rt);
int m = l+r>>;
if(L<=m)
update(val, L, R, lson);
if(R>m)
update(val, L, R, rson);
pushUp(rt);
}
bitset<> query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return s[rt];
}
pushDown(rt);
bitset<> tmp;
tmp.reset();
int m = l+r>>;
if(L<=m)
tmp |= query(L, R, lson);
if(R>m)
tmp |= query(L, R, rson);
return tmp;
}
int main()
{
int q, n, m, x, y;
char ch;
cin>>n>>m;
build(, n, );
cin>>q;
while(q--) {
scanf(" %c%d%d", &ch, &x, &y);
if(ch == 'Q')
printf("%d\n", query(x, y, , n, ).count());
else {
int z;
scanf("%d", &z);
update(z, x, y, , n, );
}
}
return ;
}
CDOJ 1259 昊昊爱运动 II bitset+线段树的更多相关文章
- CDOJ 1259 昊昊爱运动 II 线段树+bitset
昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...
- cdojQ - 昊昊爱运动 II
地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: Q - 昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) ...
- UESTC-1259 昊昊爱运动 II
昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 昊昊喜 ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
- HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)
http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- CDOJ 1292 卿学姐种花 暴力 分块 线段树
卿学姐种花 题目连接: http://acm.uestc.edu.cn/#/problem/show/1292 Description 众所周知,在喵哈哈村,有一个温柔善良的卿学姐. 卿学姐喜欢和她一 ...
- Codeforces 633H Fibonacci-ish II【线段树】
LINK 题目大意 给你一个序列a,Q次询问,每次询问\([l,r]\) 把\([l,r]\)的数排序去重,得到序列b,f是斐波那契数列 求\(\sum_{b=1}^{len} b_if_i\) 思路 ...
随机推荐
- BeanUtils\DBUtils
BeanUtil: 需要导入 beanutil包和logging日志包 用于给对象属性赋值. setProperty与copyProperty区别: 这个问题搁置,还不会. 将map数据拷贝到对象中, ...
- 自定义标签体、MVC
自定义标签 文件以tld结尾,放在webinfo中 标签名 引用类 标签体 继承SimpleTagSupport,复写doTag() getContext(); getjspBody() invo ...
- Android 4.2启动代码分析(一)
Android系统启动过程分析 Android系统的框架架构图如下(来自网上): Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...
- 切点算法模板(Cut-vertex)
下面是一个模板被切割点,也cut_vertex_num[]排列(array)什么是切 - 点记录 Int cut_vertex_num[]; void dfs(int cur,int pa) { in ...
- 从一句SQL得出的启示
select count(*) + 1 from `table` where rank > (select rank from `table` where id = *) 上面那句SQL 给了我 ...
- node.js的一些知识
什么是node.js node.js是构建于chrome浏览器v8引擎上的一个js运行环境 可以解析和执行js代码 可以当做另一种上下文,脱离浏览器环境(后端)运行js代码,而代码解析就是基于V8引擎 ...
- SET ANSI_NULLS (Transact-SQL)
指定在 SQL Server 2014 中与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为. 当 SET ANSI_NULLS 为 ON ...
- BZOJ 1217: [HNOI2003]消防局的设立( 贪心 )
一个简单的贪心, 我们只要考虑2个消防局设立的距离为5时是最好的, 因为利用最充分. 就dfs一遍, 再对根处理一下就可以了. 这道题应该是SGU某道题的简化版...这道题距离只有2, 树型dp应该也 ...
- mysql在查询结果中增加排序字段
ELECT userId , () AS runRank , mostFast1 FROM user_info, ()) b WHERE mostFast1 IS NOT NULL ORDER BY ...
- T-SQL应用,视图、存储过程、触发器、游标、临时表等
sqlserver常用操作: 视图.存储过程.触发器.函数 --*********************批处理********************* --[在一个批处理中存有一个语法错误,则所有 ...