ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】
Prime Query
Time Limit: 1 Second Memory Limit: 196608 KB
You are given a simple task. Given a sequence A[i] with N numbers. You have to perform Q operations on the given sequence.
Here are the operations:
- A v l, add the value v to element with index l.(1<=V<=1000)
- R a l r, replace all the elements of sequence with index i(l<=i<= r) with a(1<=a<=10^6) .
- Q l r, print the number of elements with index i(l<=i<=r) and A[i] is a prime number
Note that no number in sequence ever will exceed 10^7.
Input
The first line is a signer integer T which is the number of test cases.
For each test case, The first line contains two numbers N and Q (1 <= N, Q <= 100000) - the number of elements in sequence and the number of queries.
The second line contains N numbers - the elements of the sequence.
In next Q lines, each line contains an operation to be performed on the sequence.
Output
For each test case and each query,print the answer in one line.
Sample Input
1
5 10
1 2 3 4 5
A 3 1
Q 1 3
R 5 2 4
A 1 1
Q 1 1
Q 1 2
Q 1 4
A 3 5
Q 5 5
Q 1 5
Sample Output
2
1
2
4
0
4
题目大意:三种类型的操作。A操作单点更新,给某位置增加值v,R操作区间更新,整个区间都更新为值v,Q操作区间查询,查询区间内的素数个数。
解题思路:用线段树维护三个值,一个lazy标记,一个val表示点的值,一个pnum表示素数的个数。在单点查询的时候,用一个PushDown操作将lazy标记下放,同时将值下发。最后还要用PushUp操作在回溯的时候更新值。对于区间查询来说,在查询的时候将lazy下放。关键要写好PushDown和PushUp这两个操作。(今天比较坑,自己建树的时候,没有上推,QAQ)。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=1e5+200;
const int maxv=1e7+200;
struct SegTree{
int val,pnum,lazy;
}segtrees[maxn*4];
int prime[maxv],pprime[maxv];
void getprime(){
int num_prime=0;
prime[0]=prime[1]=1;
for(int i=2;i<maxv;i++){
if(!prime[i])
pprime[num_prime++]=i;
for(int j=0;j<num_prime && i*pprime[j]<maxv;j++){
prime[i*pprime[j]]=1;//合数标为1,同时,prime[j]是合数i*prime[j]的最小素因子
if(!(i%pprime[j]))//即比一个合数大的质数和该合数的乘积可用一个更大的合数和比其小的质数相乘得到
break;
}
}
}
void PushUp(int rt){
segtrees[rt].pnum = segtrees[rt*2].pnum + segtrees[rt*2+1].pnum; //
}
void build(int rt,int L,int R){
segtrees[rt].val=0;
segtrees[rt].lazy=0;
segtrees[rt].pnum=0;
if(L==R){
int &v=segtrees[rt].val;
scanf("%d",&v);
if(prime[v]){
segtrees[rt].pnum=0;
}else{
segtrees[rt].pnum=1;
}
return ;
}
build(lson);
build(rson);
PushUp(rt);
} void PushDown(int rt,int L,int R){
if(segtrees[rt].lazy==1){ segtrees[rt].lazy=0;
segtrees[rt*2].lazy=1;
segtrees[rt*2+1].lazy=1; segtrees[rt*2].val=segtrees[rt].val;
segtrees[rt*2+1].val=segtrees[rt].val;
segtrees[rt].val=0;
}
}
void add(int rt,int L,int R,int pos,int _val){
if(L==R){
int &v=segtrees[rt].val;
v+=_val;
if(prime[v]){
segtrees[rt].pnum=0;
}else{
segtrees[rt].pnum=1;
}
return ;
}
PushDown(rt,L,R);
if(pos<=mid){
add(lson,pos,_val);
}else{
add(rson,pos,_val);
}
PushUp(rt);
}
void repla(int rt,int L,int R,int l_ran,int r_ran,int _val){
if(l_ran<=L&&R<=r_ran){
segtrees[rt].lazy=1;
segtrees[rt].val=_val;
int &v=segtrees[rt].val;
if(prime[v]){
segtrees[rt].pnum=0;
}else{
segtrees[rt].pnum=R-L+1;
}
return ;
}
PushDown(rt,L,R);
if(l_ran<=mid){
repla(lson,l_ran,r_ran,_val);
} if(r_ran>mid){
repla(rson,l_ran,r_ran,_val);
}
PushUp(rt);
}
int query(int rt,int L,int R,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){
return segtrees[rt].pnum;
}
PushDown(rt,L,R);
int ret=0;
if(l_ran<=mid){
ret+=query(lson,l_ran,r_ran);
}
if(r_ran>mid){
ret+=query(rson,l_ran,r_ran);
}
return ret;
}
int main(){
int T,n,m;
char opr[10];
getprime();
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
build(1,1,n);
int a,b,c;
for(int i=1;i<=m;i++){
scanf("%s",opr);
if(opr[0]=='A'){
scanf("%d %d",&a,&b);
add(1,1,n,b,a);
}else if(opr[0]=='R'){
scanf("%d %d %d",&a,&b,&c);
repla(1,1,n,b,c,a);
}else{
scanf("%d %d",&a,&b);
int ans=query(1,1,n,a,b);
printf("%d\n",ans);
}
}
}
return 0;
}
ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】的更多相关文章
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- 51nod1287(二分/线段树区间最值&单点更新)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1287 题意:中文题诶- 解法1:b[i] 存储 max(a[0 ...
- hdoj1754 I Hate It【线段树区间最大值维护+单点更新】
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【线段树区间最值单点更新模板】BNUOJ 52965 E Excellent Engineers
http://acm.bnu.edu.cn/v3/external/gym/101512.pdf #include<bits/stdc++.h> using namespace std; ...
- POJ3468(线段树区间求和+区间查询)
https://vjudge.net/contest/66989#problem/C You have N integers, A1, A2, ... , AN. You need to deal w ...
- 143 - ZOJ Monthly, October 2015 I Prime Query 线段树
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- query 线段树 + 区间排序
https://nanti.jisuanke.com/t/41391 这个题目没有很难想,比较暴力,但是要会算复杂度,不会算复杂度,就会觉得自己的算法会超时,实际上不会. 这个题目就是直接暴力求出每一 ...
- 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 ...
- poj3667(线段树区间合并&区间查询)
题目链接: http://poj.org/problem?id=3667 题意:第一行输入 n, m表示有 n 间房间(连成一排的), 接下来有 m 行输入, 对于接下来的 m 行输入: 1 x : ...
随机推荐
- Python-socket发送文件并解决粘包问题
服务器端要先根据客户端要下载的文件进行判断是否存在,还要根据文件大小来进行传送,最后还要比对文件的md5值来判断传送的文件是否正确,通过判断剩余字节来解决粘包问题 服务器端 # -*- coding: ...
- 没办法,SVD就讲的这么好
2)奇异值: 下面谈谈奇异值分解.特征值分解是一个提取矩阵特征很不错的方法,但是它只是对方阵而言的,在现实的世界中,我们看到的大部分矩阵都不是方阵,比如说有N个学生,每个学生有M科成绩,这样形成的一个 ...
- 项目一:项目第二天 Jquery ztree使用展示菜单数据 2、 基础设置需求分析 3、 搭建项目框架环境--ssh(复习) 4、 SpringData-JPA持久层入门案例(重点) 5、 Easyui menubutton菜单按钮使用 6、 Easyui messager消息框使用
1. Jquery ztree使用展示菜单数据 2. 基础设置需求分析 3. 搭建项目框架环境--ssh(复习) 4. SpringData-JPA持久层入门案例(重点) 5. Easyui menu ...
- R语言中的字符处理
R语言中的字符处理 (2011-07-10 22:29:48) 转载▼ 标签: r语言 字符处理 字符串 连接 分割 分类: R R的字符串处理能力还是很强大的,具体有base包的几个函数和strin ...
- servlet与filter的加载顺序详解
项目:3个filter,3个servlet,匹配的url路径/hello. 情况1:servlet没加<load-on-startup></load-on-startup>情 ...
- 3. Shodan新手入坑指南
什么是 Shodan? 首先,Shodan 是一个搜索引擎,但它与 Google 这种搜索网址的搜索引擎不同,Shodan 是用来搜索网络空间中在线设备的,你可以通过 Shodan 搜索指定的设备,或 ...
- 《鸟哥的Linux私房菜》读书笔记1
1.MBR 可以说是整个硬盘最重要的地方了,因为在 MBR 里面记录了两个重要的东西,分别是:开机管理程序,与磁盘分割表 ( partition table ).下次记得人家在谈磁盘分割的时候, 不要 ...
- 使用tuple统计文件中单词的个数
name = input("Enter file:") if len(name) < 1 : name = "input.txt" fhand = ope ...
- Environment Perception: 3D Truss Environment Mapping and Parametric Expression Extraction
Experiments Preparation roscore rosrun pcl_ros pcd_to_pointcloud ~/.ros/wh2_lg707070_1ms0.01_filtere ...
- HELLO---MVC
前言 很荣幸有机会参加BS的项目,这个图书馆系统这个项目,需要用到ITOO框架,其中涉及到好多小框架的学习,MVC就是其中的一个学习知识点,像大家一样,刚刚接触一个新鲜的知识,心里除了恐惧还有就是茫然 ...