题目链接

昊昊喜欢运动

他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
1 2 3 2 3
4
Q 1 4
Q 2 4
M 5 5 2
Q 1 5
3
2
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+线段树的更多相关文章

  1. CDOJ 1259 昊昊爱运动 II 线段树+bitset

    昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...

  2. cdojQ - 昊昊爱运动 II

    地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: Q - 昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) ...

  3. UESTC-1259 昊昊爱运动 II

    昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)     昊昊喜 ...

  4. 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 ...

  5. 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 ...

  6. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

  7. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  8. CDOJ 1292 卿学姐种花 暴力 分块 线段树

    卿学姐种花 题目连接: http://acm.uestc.edu.cn/#/problem/show/1292 Description 众所周知,在喵哈哈村,有一个温柔善良的卿学姐. 卿学姐喜欢和她一 ...

  9. Codeforces 633H Fibonacci-ish II【线段树】

    LINK 题目大意 给你一个序列a,Q次询问,每次询问\([l,r]\) 把\([l,r]\)的数排序去重,得到序列b,f是斐波那契数列 求\(\sum_{b=1}^{len} b_if_i\) 思路 ...

随机推荐

  1. 解密:LL与LR解析 2(译,完结)

    由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/2013/07/ll-and-lr-parsing-demystified.h ...

  2. js 全局函数

    全局函数与内置对象的属性或方法不是一个概念. 全局函数它不属于任何一个内置对象. JS中有14个全局函数 函数 描述 decodeURI() 解码某个编码的 URI. decodeURICompone ...

  3. MMDrawerController 的实践,已经实现,几行简单的代码实现侧栏

    学习方法,看readme,看给的Demo 看功能怎么实现的去模仿,个人感觉模仿是最快的学习方法 废话少说,上代码 导入MMDrawerController框架我就不多少了,之后做什么才是我们才关注的事 ...

  4. SqlServer数据类型、C#SqlDbType对应关系及转换

    { { } } { SqlDbType dbType = SqlDbType.Variant; { dbType = SqlDbType.Int; dbType = SqlDbType.VarChar ...

  5. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  6. Notes常用事件整理

    ①      ボタンのクリック事件: Sub Click(Source As Button) Dim ws As New NotesUIWorkspace Dim uidoc As NotesUIDo ...

  7. html5 input的type属性启动数字输入法

    html5 input的type属性启动数字输入法   当文本框只能输入数字是一个很常见的需求,比如电话号码,身份证号,卡号, 数量....等等只允许数字输入,为了更好的用户体验性,直接写出 启动数字 ...

  8. php-Eclipse对php中的namespace关键字报语法错误的问题

    namespace是php5.3以上才支持的, 解决办法:升级IDE 切换Eclipse中的php版本 Window  > Preferences  > PHP  > PHP Int ...

  9. codeforces 455C 并查集

    传送门 给n个点, 初始有m条边, q个操作. 每个操作有两种, 1是询问点x所在的连通块内的最长路径, 就是树的直径. 2是将x, y所在的两个连通块连接起来,并且要合并之后的树的直径最小,如果属于 ...

  10. hdu 4289 Control 网络流

    题目链接 给出一些点, 每个点有一个权值, 给出一些边, 起点以及终点, 去掉一些点使得起点和终点不连通, 求最小的val. 拆点, 把一个点s拆成s和s', 之间建一条边, 权值为点权. 对于一条边 ...