cdoj 1259 线段树+bitset 区间更新/查询
Description
昊昊喜欢运动
他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
5 3
1 2 3 2 3
4
Q 1 4
Q 2 4
M 5 5 2
Q 1 5
Sample Output
3
2
3
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<bitset>
//#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
struct node
{
int l,r;
int add;
bitset<> sum;
} tree[];
int n,m;
int exm;
int l1,r1,x;
int q;
char judge;
void buildtree(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
if(left==right)
{
tree[root].sum.reset();
scanf("%d",&exm);
tree[root].sum[exm]=;
return ;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].sum=tree[root<<].sum|tree[root<<|].sum;//按位或得到新的运动种类
}
void pushdown(int root)
{
if(tree[root].add==)
return ;
tree[root<<].sum.reset();
tree[root<<|].sum.reset();
tree[root<<].sum[tree[root].add]=;
tree[root<<|].sum[tree[root].add]=;
tree[root<<].add=tree[root].add;//注意延迟标记也要往下传
tree[root<<|].add=tree[root].add;
tree[root].add=;
}
void updata(int root,int left,int right,int c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].sum.reset();//全部置零
tree[root].add=c;
tree[root].sum[c]=;//整个区间只有c运动
return ;
}
pushdown(root);//lazy 延迟标记
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(root<<,left,right,c);
else
{
if(left>mid)
updata(root<<|,left,right,c);
else
{
updata(root<<,left,mid,c);
updata(root<<|,mid+,right,c);
}
}
tree[root].sum=tree[root<<].sum|tree[root<<|].sum;
}
bitset<> query(int root,int left,int right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].sum;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return query(root<<,left,mid)|query(root<<|,mid+,right);
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
buildtree(,,n);
scanf("%d",&q);
getchar();
for(int i=; i<=q; i++)
{
scanf("%c",&judge);
if(judge=='Q')
{
scanf("%d %d",&l1,&r1);
printf("%d\n",query(,l1,r1).count());
}
else
{
scanf("%d %d %d",&l1,&r1,&x);
updata(,l1,r1,x);
}
getchar();//坑点
}
}
return ;
}
cdoj 1259 线段树+bitset 区间更新/查询的更多相关文章
- POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13805 Accepted: 5996 Descriptio ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1698:Just a Hook(线段树,区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- UVA 12436-Rip Van Winkle's Code(线段树的区间更新)
题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...
- hdu1698线段树的区间更新区间查询
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- zoj3686(线段树的区间更新)
对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a ...
- Color the ball (线段树的区间更新问题)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
随机推荐
- OA上传和编辑数据
1.VO:value object值对象.通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已.但应是抽象出的业务对象,可以和表对应,也可以不,这根据业务的需要. 2. String[]类型打 ...
- Centos7上docker的安装和基本使用
Centos7上Docker的相关操作 Docker的安装 1.检查内核版本 docker使用linux的内核必须是3.10以上,首先需要 # uname -r 2.使用yum安装docker # y ...
- MPP(大规模并行处理)
1. 什么是MPP? MPP (Massively Parallel Processing),即大规模并行处理,在数据库非共享集群中,每个节点都有独立的磁盘存储系统和内存系统,业务数据根据数据库模型和 ...
- C语言数组篇(二)指针数组和数组指针
数组指针 和 指针数组 这两个名词可以说是经常搞混了 数组指针--> 数组的指针 就是前面讲的 指向数组a的指针p; 指针数组--&g ...
- SpringMVC集成RSA加密算法
技术交流群: 233513714 本文介绍的是RSA加密算法+Spring Security在SpringMVC中的集成使用. Spring Security是什么? 引用: Spring Secur ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目3
2014-04-23 18:10 题目:设计一个点唱机. 解法:英文叫Musical Jukebox.这是点唱机么?卡拉OK么?这种题目实在是云里雾里,又没有交流的余地,我索性用一个vector来表示 ...
- DOS程序员手册(九)
第14章参考手册概述 本书余下的章节将向读者们介绍BIOS.DOS各种各样API函数和服务,作为一名程 序员,了解和掌握这些知识是很有好处的.在所介绍的参考手册中,每部手册都汇集了大 量的资源 ...
- linux运维笔记
一.查找大文件 sudo find / -size +100M -exec ls -lh {} \;
- 一道简单的CTFpython沙箱逃逸题目
看了几天的ssti注入然后了解到有python沙箱逃逸 学过ssti注入的话python沙箱逃逸还是很容易理解的. 看一道CTF题目,源码的话我改了改,一开始不能用,直接在py2上运行就好. 题目要求 ...
- django-settings里redis连接与缓存配置
# Django-redis的缓存配置 CACHES = { "default": { "BACKEND": "django_redis.cache. ...