ZOJ 3911 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
来自 <http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3911>
【题意】:
单点修改,区间修改,查询区间素数个数
【解题思路】:
维护线段树,结点内容为:Val-实时数值(只针对单点) Sum-区间素数个数 Lazy-区间修改后传递给子区间
关键点在于数值和素数标记的同时更新和传递,由于区间更新时没有直接更新到最底层,故单点查询也需要pushdown操作。这里选择将区间更新后的值直接下传,至于素数标记,下传后再进行判断并赋值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mid(a,b) ((a+b)>>1)
#define LL int
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; char is_prime[maxn*];
void sieve()
{
int m=(int)sqrt((maxn*)+0.5);
fill(is_prime,is_prime+(maxn*),);
is_prime[]=is_prime[]=;
for(int i=;i<=m;i++) if(is_prime[i])
for(int j=i*i;j<(maxn*);j+=i) is_prime[j]=;
} int n,q;
LL num[maxn];
struct Tree
{
int left,right;
LL sum;
LL val,lazy;
}tree[maxn<<]; /*递归建树*/
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].lazy=; if(left==right){
tree[i].val=num[left];
tree[i].sum=(is_prime[num[left]]? :);
return ;
} int mid=mid(left,right); build(i<<,left,mid);
build(i<<|,mid+,right); tree[i].sum=tree[i<<].sum+tree[i<<|].sum;
} /*区间修改,标记下传:每当访问到当前结点的子节点时,下传标记*/
void pushdown(int i)
{
if(tree[i].lazy){
int tmp = (is_prime[tree[i].lazy]? :);
tree[i<<].val=tree[i].lazy;
tree[i<<|].val=tree[i].lazy;
tree[i<<].lazy=tree[i].lazy;
tree[i<<|].lazy=tree[i].lazy;
tree[i<<].sum=(tree[i<<].right-tree[i<<].left+)*tmp;
tree[i<<|].sum=(tree[i<<|].right-tree[i<<|].left+)*tmp;
tree[i].lazy=; /*下传后清零*/
}
} /*区间修改,d为改变量*/
void update(int i,int left,int right,LL d)
{
if(tree[i].left==left&&tree[i].right==right)
{
int tmp = (is_prime[d]? :);
tree[i].sum=(right-left+)*tmp;
tree[i].val=d;
tree[i].lazy=d;
return ;
} pushdown(i); int mid=mid(tree[i].left,tree[i].right); if(right<=mid) update(i<<,left,right,d);
else if(left>mid) update(i<<|,left,right,d);
else
{
update(i<<,left,mid,d);
update(i<<|,mid+,right,d);
} tree[i].sum=tree[i<<].sum+tree[i<<|].sum;
} /*单点修改,d为改变量*/
void update(int i,int x,LL d)
{
if(tree[i].left==tree[i].right){
tree[i].val+=d;
tree[i].sum=(is_prime[tree[i].val]? :);
return;
} pushdown(i);
int mid=mid(tree[i].left,tree[i].right); if(x<=mid) update(i<<,x,d);
else update(i<<|,x,d); tree[i].sum=tree[i<<].sum+tree[i<<|].sum;
} /*区间结果查询*/
LL query(int i,int left,int right)
{
if(tree[i].left==left&&tree[i].right==right)
return tree[i].sum; pushdown(i); int mid=mid(tree[i].left,tree[i].right); if(right<=mid) return query(i<<,left,right);
else if(left>mid) return query(i<<|,left,right);
else return query(i<<,left,mid)+query(i<<|,mid+,right);
} int main(int argc, char const *argv[])
{
//IN; sieve();
int t;scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++) scanf("%d",&num[i]);
build(,,n); while(q--)
{
char c;
while(c=getchar()){
if(c=='A'||c=='R'||c=='Q') break;
}
if(c=='A'){
int v,l;
scanf("%d %d",&v,&l);
update(,l,v);
}
if(c=='R'){
int a,l,r;
scanf("%d %d %d",&a,&l,&r);
update(,l,r,a);
}
if(c=='Q'){
int l,r;
scanf("%d %d",&l,&r);
printf("%d\n", query(,l,r));
}
}
} return ;
}
ZOJ 3911 Prime Query(线段树)的更多相关文章
- 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 ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- zoj 3511 Cake Robbery(线段树)
problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...
- CF893F:Subtree Minimum Query(线段树合并)
Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...
- zoj 3325 Machine(线段树)
题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...
- ZOJ 2301/HDU 1199 线段树+离散化
给这个题目跪了两天了,想吐简直 发现自己离散化没学好 包括前一个离散化的题目,实际上是错了,我看了sha崽的博客后才知道,POJ那题简直数据弱爆了,本来随便一组就能让我WA掉的,原因在于离散化的时候, ...
- ZOJ 2859 二维线段树
思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...
- query 线段树 + 区间排序
https://nanti.jisuanke.com/t/41391 这个题目没有很难想,比较暴力,但是要会算复杂度,不会算复杂度,就会觉得自己的算法会超时,实际上不会. 这个题目就是直接暴力求出每一 ...
随机推荐
- Windows JAVA 环境配置
Java SE Development Kit Downloads http://www.oracle.com/technetwork/java/javase/overview/index.html ...
- Toad创建DBLINKsop
Toad创建DBLINKsop 1.创建服务: 点击“测试”,出现如下测试窗口后点击更改登录,用户名和密码数据目标主机用户名.密码; 出现如下窗口后,点击“关闭”,然后点击“完成”即可; 2.创建db ...
- CSS效果
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- BZOJ2229: [Zjoi2011]最小割
题解: 真是一道神题!!! 大家还是围观JZP的题解吧(网址找不到了...) 代码: #include<cstdio> #include<cstdlib> #include&l ...
- ADO和DAO的区别
ADO(ACTIVEX DATA OBJECTS)应用层的数据访问接口ODBC 数据库驱动接口OLE DB 系统级数据访问接口DAO (DATA ACCESS OBJECTS) 对象的数据访问接口AD ...
- c& c++ enum
1.为什么要用enum 写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等. ...
- ViewPager+Fragment替代TabHost效果的简单示例
本示例旨在展示fragment替代tabhost的效果,具体的业务逻辑还要根据这个示例进行扩展. 效果图如下: 主Activity代码: package com.llb.view; import ja ...
- Mac 配置jdk
1.打开终端,开始操作 cd ~touch.bash_profile vi .bash_profile 2.在此文本中添加以下内容 export JAVA_HOME=/Library/Java/Jav ...
- T-SQL查询进阶-10分钟理解游标
转:http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的 ...
- CXF之jaxws:endpoint对spring bean的引用
由于CXF对spring的无缝支持,CXF的使用,经常与spring捆绑在一起.随之而起的,自然是想在jaxws:endpoint中引用spring bean.在CXF提供的HelloWorld例子中 ...