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个板子给出板子的高度和左右坐标(同高度不重叠) 所有箱子从上向下落,求每块板子能接到的箱子数. 分析: 首先给的区间很大,一开始 ...
随机推荐
- 【Git教程】Git教程及使用命令
Git是目前世界上最先进的分布式版本控制系统,可以自动记录和管理文件的改动,还可以团队写作编辑,也就是帮助我们对不同的版本进行控制.2008年,GitHub网站上线,为开源项目提供免费存储,迅速发 ...
- php 安装mysql扩展注意事项
1.yum search php-mysql (Linux环境) 这一点,根据具体的情况会遇到不同的搜索结果.我搜索到的结果是:php-mysql.i386 : A module for PHP ap ...
- 01.Python基础-4.字符串
1 字符串初识 字符串编码 字符串定义:多个字符(双引号或单引号中的数据)组成 字符串下标 类似列表list 格式化 就是占位符 最常用%s %d %f等等 转义字符 转义字符 描述 \ (在行尾时) ...
- MVC笔记(一)
1 MVC介绍 MVC是一个编程思想. 是一种设计模式 思想: 将一个功能分解成3个部分, M: Model (模型) 处理数据相关的逻辑 V: View (视图) 显示页面 C: Controlle ...
- position:fixed div如何居中
div{position:fixed;margin:auto;left:0; right:0; top:0; bottom:0;width:200px; height:150px;}
- 你必须搞清楚的String,StringBuilder,StringBuffer
String,StringBuilder 以及 StringBuffer 这三个类的关系与区别一直是 Java 的经典问题,这次就来讲一下关于这三个类的一些知识 一. 简单对比 String : 字符 ...
- mysql修改配置文件
在Apache, PHP, MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接影响到论坛的速度和承载量!同 ...
- Elasticsearch 7.0 发布都有哪些新特性
了解about云知识星球 .pcb{margin-right:0} 问题导读 1.Elasticsearch&Kibana 7.哪些需要修改? 2.Elasticsearch7 有哪些新特性? ...
- codevs——T3657 括号序列
http://codevs.cn/problem/3657/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description ...
- 关于idea控制台乱码问题
乱码是常有的事儿,改一下也就两分钟......不多说看图: 上图中的勾选项一定不要忘记,它可以隐藏你项目中encoding设置. 在上图两个文件中加入 -Dfile.encoding=UTF-8 在上 ...