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个板子给出板子的高度和左右坐标(同高度不重叠) 所有箱子从上向下落,求每块板子能接到的箱子数. 分析: 首先给的区间很大,一开始 ...
随机推荐
- Quoit Design (HDU 1007)平面的最近点对
题目大意:给定平面上的 n 个点,求距离最近的两个点的距离的一半. n <= 10^5. 晕乎乎的度过了一上午... 总之来学习下分治吧233 分治就是把大问题拆成小问题,然后根据对小问题处 ...
- Ubuntu环境搭建svn服务器
记录一次使用Ubuntu环境搭建svn服务器的详细步骤 一.查看是否已经安装svn 命令:svn如果显示以下信息,说明已安装 二.卸载已安装的svn 命令:sudo apt-get remove ...
- Asp.Net IHttpHandler介绍
ASP.NET响应Http请求时常用的两个处理接口是IHttpHandler和IHttpModule. 一般的,IHttpHandler用来处理一类特定的请求,比如对每个*.asp, *.aspx文件 ...
- mybatis3中几个@Provider的使用方式
Mybatis的原身是ibatis,现在已经脱离了apache基金会,新官网是http://www.mybatis.org/.Mybatis3中增加了使用注解来配置Mapper的新特性,这里主要介绍@ ...
- solrj 操作 solr 集群版
一.添加 @Test public void testAddDocument() throws Exception{ //创建一个集群的连接,应该使用 CloudSolrServer,//zkHost ...
- CF909A Generate Login
CF909A Generate Login 题意翻译 给定两个用空格分隔的字符串,分别取两字符串的任意非空前缀,将两前缀合并为一个新的字符串,求可行字典序最小的字符串. 题目描述 The prefer ...
- webuploader 教程
1.引入js和css <!-- Web Uploader --> <link rel="stylesheet" type="text/css" ...
- UML基本架构建模--获取类
Getting Started 開始 Modeling a system involves identifying the things that are important to your p ...
- 一个关于 UIPickerView 的 bug
首先,我下描写叙述一下bug的发生情况: 在使用UIPickerView实现选择城市的时候.出现这样一个Bug 1.在iOS 6的系统上 2.Picker的数据上省份一栏选择了"香港&quo ...
- OC-JS交互(WebViewJavascriptBridge使用说明)
首先确保一份已经配好功能的html文件. 1.初始化一个webview(viewdidload) UIWebView* webView = [[UIWebView alloc] initWithFra ...