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 这个题目没有很难想,比较暴力,但是要会算复杂度,不会算复杂度,就会觉得自己的算法会超时,实际上不会. 这个题目就是直接暴力求出每一 ...
随机推荐
- iOSTab bar
http://www.apkbus.com/android-130504-1-1.html #import #import "FirstViewController.h"#impo ...
- lightOJ 1326 Race(第二类Stirling数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1326 题意:有n匹马赛跑.问有多少种不同的排名结果.可以有多匹马的排名相同. 思路:排 ...
- 解决 “无法安装 Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持”
http://blog.csdn.net/davidhsing/article/details/8762621 无法安装Microsoft visual studio 2010 service pac ...
- sdut 2934 人活着系列之平方数 (完全背包变形)
题目链接 分析:完全背包的变形,每一层的d[]数组代表这一层的这个数新加入以后所构成的val的种类. #include <iostream> #include <cstdio> ...
- iOS开发:mac使用svn管理项目
记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...
- Spring 使用注解方式进行事务管理
转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...
- Fatal NI connect error 12170
Fatal NI connect error 12170 转载:http://www.xifenfei.com/1812.html 今天在一台服务器的日志文件中,发现如下信息: Fatal NI co ...
- FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- linux面试题3
1. 下面的网络协议中,面向连接的的协议是: A . A 传输控制协议 B 用户数据报协议 C 网际协议 D 网际控制报文协议 2. 在/etc/fstab文件中指定的文件系统加载参数中, D 参数一 ...
- 【转】visual studio 2012进行C语言开发[图文]
原文网址:http://blog.csdn.net/chengyafei0104/article/details/9826025 现在大家计算机大概都脱离XP了,so,之前蛮多可以用的编译器,可能放在 ...