ZOJ 3911Prime Query [素数处理 + 线段树]
Time Limit: 5 Seconds 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 5Sample Output
2
1
2
4
0
4
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define EPS 0.00000001
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull; const int maxn = 1e5+;
const int N = 1e7+;
int t[maxn << ],in[maxn << ],lazy[maxn << ];
int prime[N]; void getprime()
{
for(int i=;i<sqrt(1.0*N);i++)
if(!prime[i])
for(int j=i+i;j<N;j+=i)
prime[j] = ;
prime[] = ;
prime[] = ;
} void pushdown(int i,int b,int e)
{
if(lazy[i])
{
int mid = (b + e) / ;
in[i << ] = !prime[lazy[i]] * (mid - b + );
in[i << | ] = !prime[lazy[i]] * (e - mid); t[i << ] = lazy[i];
t[i << | ] = lazy[i]; lazy[i << ] = lazy[i];
lazy[i << | ] = lazy[i]; lazy[i] = ;
}
} void add(int i,int b,int e,int pos,int val)
{
if(b == e)
{
in[i] -= !prime[t[i]];
t[i] += val;
in[i] += !prime[t[i]];
return ;
}
pushdown(i, b, e); int mid = (b + e) / ;
if(pos <= mid) add(i << , b, mid, pos, val);
else add(i << | , mid + , e, pos, val); in[i] = in[i << ] + in[i << | ];
} void replace(int i,int b,int e,int l,int r,int val)
{
if(b >= l && e <= r)
{
in[i] = (!prime[val]) * (e - b + );
t[i] = val;
lazy[i] = val;
return ;
}
pushdown(i, b, e); int mid = (b + e) / ;
if(r <= mid) replace(i << , b, mid, l, r, val);
else if(l > mid) replace(i<< | , mid + , e, l, r, val);
else
{
replace(i << , b, mid, l, r, val);
replace(i << | , mid + , e, l, r, val);
} in[i] = in[i << ] + in[i << | ];
} int query(int i,int b,int e,int l,int r)
{
if(b >= l && e <= r)
return in[i];
pushdown(i, b, e); int mid = (b + e) / ;
if(r <= mid) return query(i << , b, mid, l, r);
else if(l > mid) return query(i << | , mid + , e, l, r);
else return query(i << , b, mid, l, r) + query(i << | , mid + , e, l, r);
} int main()
{
getprime(); int T;
scanf("%d",&T);
while(T--)
{
char s[];
int n,m,x,a,b,c; scanf("%d%d",&n,&m); memset(t,,sizeof(t));
memset(in,,sizeof(in));
memset(lazy,,sizeof(lazy)); for(int i=;i<=n;i++)
{
scanf("%d",&x);
add(,,n,i,x);
} for(int i=;i<m;i++)
{
scanf("%s%d%d",s,&a,&b);
if(s[] == 'A') add(,,n,b,a); else if(s[] == 'Q') printf("%d\n", query(,,n,a,b)); else
{
scanf("%d",&c);
replace(,,n,b,c,a);
} }
}
}
ZOJ 3911Prime Query [素数处理 + 线段树]的更多相关文章
- ZOJ 2671 Cryptography 矩阵乘法+线段树
B - Cryptography Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Subm ...
- zoj3886--Nico Number(素数筛+线段树)
Nico Number Time Limit: 2 Seconds Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- zoj 3888 Twelves Monkeys 二分+线段树维护次小值
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- 【HDU5869】 Different GCD Subarray Query 题解 (线段树维护区间GCD)
题目大意:求区间$[L,R]$中所有子区间产生的最大公因数的个数. ------------------------- 对于$gcd$,我们知道$gcd(a,b,c)=gcd(gcd(a,b),c)$ ...
- HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】
一.题目 Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...
- ZOJ 3299-Fall the Brick(线段树+离散化)
题意: n个区间 ,给出区间的左右坐标 ,区间内填满宽度为1的箱子,有m个板子给出板子的高度和左右坐标(同高度不重叠) 所有箱子从上向下落,求每块板子能接到的箱子数. 分析: 首先给的区间很大,一开始 ...
随机推荐
- SQL 到 NOSQL 的思维转变
转自:http://blogread.cn/it/article/3130?f=wb SQL 到 NOSQL 的思维转变 NOSQL系统一般都会宣传一个特性,那就是性能好,然后为什么呢?关系型数据库发 ...
- MNIST机器学习数据集
介绍 在学习机器学习的时候,首当其冲的就是准备一份通用的数据集,方便与其他的算法进行比较.在这里,我写了一个用于加载MNIST数据集的方法,并将其进行封装,主要用于将MNIST数据集转换成numpy. ...
- 【hdu 6336】 Matrix from Arrays
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找个规律会发现 M[i][j] = M[i-2*L][j] = M[i][j-2*L] 先预处理出来(1,1)-(2L,2L)这个矩 ...
- 2015 Multi-University Training Contest 8 hdu 5385 The path
The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...
- MySQL多线程数据导入导出工具Mydumper
http://afei2.sinaapp.com/?p=456 今天在线上使用mysqldump将数据表从一个库导入到另外一个库,结果速度特别慢,印象中有个多线程的数据导入导出工具Mydumper,于 ...
- Ajax发送简单请求案例
所谓简单请求,是指不包含任何参数的请求.这种请求通常用于自动刷新的应用,例如证券交易所的实时信息发送.这种请求通常用于公告性质的响应,公告性质的响应无需客户端的任何请求参数,而是由服务器根据业务数据自 ...
- Android recovery UI实现分析
Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...
- MongoDB 2.6安装
Workaround to install as a service You can manually install 2.6.0 as a service on Windows from an Ad ...
- 使用从mysql中取数据
import MySQLdb conn=MySQLdb.connect(host="xx",user="xx",passwd="xx",db ...
- UITableView属性 自己定义UITableViewCell
UITableView的属性全齐.供大家參考 附:http://www.bubuko.com/infodetail-561085.html //曾经在使用UITableView的时候,总是在cell上 ...