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(线段树)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  4. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  5. CF893F:Subtree Minimum Query(线段树合并)

    Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...

  6. zoj 3325 Machine(线段树)

    题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...

  7. ZOJ 2301/HDU 1199 线段树+离散化

    给这个题目跪了两天了,想吐简直 发现自己离散化没学好 包括前一个离散化的题目,实际上是错了,我看了sha崽的博客后才知道,POJ那题简直数据弱爆了,本来随便一组就能让我WA掉的,原因在于离散化的时候, ...

  8. ZOJ 2859 二维线段树

    思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...

  9. query 线段树 + 区间排序

    https://nanti.jisuanke.com/t/41391 这个题目没有很难想,比较暴力,但是要会算复杂度,不会算复杂度,就会觉得自己的算法会超时,实际上不会. 这个题目就是直接暴力求出每一 ...

随机推荐

  1. oracle SQL Develop导出数据库中的表格数据到excel

    首先打开oracle数据库 1.查询数据库, SELECT * FROM pub_attribute WHERE ELEMENT_CODE='bb382e10d7ce437b8a8c980ba20ac ...

  2. Android开发之执行定时任务AlarmManager,Timer,Thread

    1.Thread:使用线程方式2.Timer是java的特性3.AlarmManager:AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过Alarm ...

  3. 进程间通信的WM_COPYDATA的使用

    http://blog.csdn.net/ao929929fei/article/details/6316174 接收数据的一方 ON_WM_COPYDATA() afx_msg BOOL OnCop ...

  4. dict 字典

    Python 学习笔记[dict的操作方法] Python中dict详解

  5. python - os.path,路径相关操作

    python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...

  6. Java [leetcode 4] Median of Two Sorted Arrays

    问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  7. RTP/RTCP(一)-H264关于RTP协议的实现

    H264关于RTP协议的实现2010-07-22 13:35完整的C/S架构的基于RTP/RTCP的H.264视频传输方案.此方案中,在服务器端和客户端分别进行了功能模块设计.服务器端:RTP封装模块 ...

  8. ubuntu12.04上搭建darwin streaming server6.03

    个人建议:使用DarwinStreamingSrvr5.5.5,因为DarwinStreamingSrvr6.0.3安装过程中有很多问题需要解决!而且安装只需执行./Install就可以! 1:下载d ...

  9. 【转】This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in

    原文网址:http://1982106a.blog.163.com/blog/static/8436495620149239361692/ 预览layout.xml文件时提示: This versio ...

  10. Android Studio进行NDK编程