题目链接

昊昊喜欢运动

他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. 数学之路-python计算实战(13)-机器视觉-图像增强

    指数变换的基本表达式为:y=bc(x-a)-1 当中參数b.c控制曲线的变换形状,參数a控制曲线的位置. 指数变换的作用是扩展图像的高灰度级.压缩低灰度级.能够用于亮度过高的图像 本博客全部内容是原创 ...

  2. Unity3d 要点板书

    WWW.unity3d.com.cn Unity Project  unity的项目文件/专案 Scene  unity的场景文件 Scene 场景视窗 Game 预览视窗 H... 物件视窗 Pro ...

  3. 推荐两个不错的CAD二次开发(.Net)手册

    推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...

  4. 字典 -- 数据结构与算法的javascript描述 第七章

    字典 字典是一种以键-值对形式存储数据的数据结构 最基本功能规划 add 添加数据到字典 remove 从字典中移除数据 get 从字典中取出数据 count 统计字典数据量 find 查找数据在字典 ...

  5. HTML系列(二):头部meta元素

    有关name: 一.页面关键字 网站关键字:用户通过搜索引擎能搜到该网站的词汇.最好控制在10个以内. 基本语法: <meta name="keywords" content ...

  6. 我的学习笔记之API-Guides翻译------AppComponent_Activites

    10.26第一天开始:贵在坚持,边看遍整理 一个应用程序通常由多个Activity组成,它们之间是松耦合的关系.特别的,有一个Activity作为app的主Activity,当app首次启动时呈现给用 ...

  7. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  8. 我用过的Linux命令之chmod

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...

  9. Cortex-M3 动态加载二(RWPI数据无关实现)

    上一篇关于动态加载讲述的是M3下面的ropi的实现细节,这一篇则讲述RW段的实现细节以及系统加载RW段的思路,我在M3上根据这个思路可以实现elf的动态加载,当然进一步的可以优化很多东西,还可以研究将 ...

  10. RTTI-CLASS

    package com.xt.test; interface Test1Interface { } interface Test2Interface { } class Test1 implement ...