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. UINavigationController学习笔记

    http://site.douban.com/129642/widget/notes/5513129/note/187701199/ 1-view controllers的关系:Each custom ...

  2. [UVA796]Critical Links(割边, 桥)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

  4. Drupal 7.31版本爆严重SQL注入漏洞

    今早有国外安全研究人员在Twitter上曝出了Drupal 7.31版本的最新SQL注入漏洞,并给出了利用测试的EXP代码. 在本地搭建Drupal7.31的环境,经过测试,发现该利用代码可成功执行并 ...

  5. fancybox 关闭弹出窗口 parent.$.fancybox.close(); 无反应 fancybox 关闭弹出窗口父页面自动刷新,弹出子窗口前后事件

    当我们在父页面使用 fancybox 弹出窗口后,如果想自己手动关闭,则可以 function Cancel() { parent.$.fancybox.close(); } 如果关闭没有反应,最好看 ...

  6. BZOJ 3573 米特运输

    语文题... 原来除了hash还可以取对数啊orz #include<iostream> #include<cstdio> #include<cstring> #i ...

  7. 物联网操作系统HelloX开发者入门指南

    HelloX开发者入门指南 HelloX是聚焦于物联网领域的操作系统开发项目,可以通过百度搜索"HelloX",获取详细信息.当前开发团队正在进一步招募中,欢迎您的了解和加入.如果 ...

  8. 对于GLM的理解,与方差分析的对比

    最近遇到一个问题,如果因变量为一个连续变量(如胰岛素水平),主要考察的变量为分组变量(如正常血糖组,前糖尿病组,糖尿病组三组),现在的目的是想看调整多种变量(包括多个连续性变量和分类变量)后,胰岛素水 ...

  9. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

  10. 常用的css的技巧

    1.在做项目当中,由静态页面来载入到项目中,作为动态数据的部分,若是这个动态数据,前面或者后面有需要图片显示(图片是用background来显示的),一般不用float:left或者right,而是p ...