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 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
题解:线段树的单点更新,区间修改,区间查询
写的很挫
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
#define maxn 1000000+5 bool P[];
ll a[+];
struct ss
{
ll l,r,v,tag,sum;
}tr[maxn*]; void pushdown(int k)
{
if(!tr[k].tag)return;
if(tr[k].l==tr[k].r){
tr[k].v=tr[k].tag;
if(!P[tr[k].v])
tr[k].sum=;
else tr[k].sum=;
}
else {
if(!P[tr[k].tag])tr[k].sum=tr[k].r-tr[k].l+;
else {tr[k].sum=;}
tr[k<<].tag=tr[k].tag;
tr[k<<|].tag=tr[k].tag;
}
tr[k].tag=;
}
void pushup(int k)
{
pushdown(k<<);
pushdown(k<<|);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
}
void build(int k,int s,int t)
{
tr[k].l=s;
tr[k].r=t;
tr[k].tag=;
tr[k].sum=;
if(s==t)
{tr[k].v=a[s];if(!P[a[s]])tr[k].sum=;else tr[k].sum=;return;}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
pushup(k);
}
void add(int k,int x,int y)
{
pushdown(k);
if(x==tr[k].l&&tr[k].l==tr[k].r)
{
tr[k].v+=y;
if(!P[tr[k].v])
tr[k].sum=;
else tr[k].sum=;
return ;
}
int mid=(tr[k].l+tr[k].r)>>;
if(x<=mid)
add(k<<,x,y);
else add(k<<|,x,y);
pushup(k);
}
void update(int k,int x,int y,int c)
{
pushdown(k);
if(x==tr[k].l&&y==tr[k].r)
{
tr[k].tag=c;
return ;
}
int mid=(tr[k].l+tr[k].r)>>;
if(y<=mid)update(k<<,x,y,c);
else if(x>mid)update(k<<|,x,y,c);
else {
update(k<<,x,mid,c);
update(k<<|,mid+,y,c);
}
pushup(k);
}
ll ask(int k,int x,int y)
{
pushdown(k);
if(x==tr[k].l&&y==tr[k].r)
{
return tr[k].sum;
}
int mid=(tr[k].l+tr[k].r)>>;
if(y<=mid)return ask(k<<,x,y);
else if(x>mid)return ask(k<<|,x,y);
else {
return (ask(k<<,x,mid)+ask(k<<|,mid+,y));
} pushup(k);
}
int main()
{
P[]=;
for(int i=;i<=;i++)
{
if(!P[i])
for(int j=i+i;j<=;j+=i)
P[j]=;
}
ll n,q,c;
int T=read();
while(T--)
{
scanf("%lld%lld",&n,&q);
FOR(i,,n)
{
scanf("%lld",&a[i]);
}
char ch[];
ll x,y;
build(,,n);
FOR(i,,q)
{
scanf("%s%lld%lld",&ch,&x,&y);
if(ch[]=='A')
{
add(,y,x);
}
else if(ch[]=='R')
{
scanf("%lld",&c);
update(,y,c,x);
}
else if(ch[]=='Q')
{
printf("%lld\n",ask(,x,y));
}
}
}
return ;
}
挫挫的代码
143 - ZOJ Monthly, October 2015 I Prime Query 线段树的更多相关文章
- 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 ...
 - ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
		
Bob wants to pour water Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge There i ...
 - ZOJ 3910 Market ZOJ Monthly, October 2015 - H
		
Market Time Limit: 2 Seconds Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...
 - ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
		
Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB The bored Bob is playing a number game ...
 - ZOJ 3905 Cake ZOJ Monthly, October 2015 - C
		
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
 - ZOJ 3903 Ant  ZOJ Monthly, October 2015 - A
		
Ant Time Limit: 1 Second Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...
 - 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
		
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
 - ZOJ 2706 Thermal Death of the Universe (线段树)
		
题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的 ...
 - zoj 3686 A Simple Tree Problem (线段树)
		
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
 
随机推荐
- 如何利用CSS中的ime-mode用来控制页面上文本框中的全角/半角输入
			
css 之 ime-mode语法:ime-mode : auto | active | inactive | disabled取值:auto : 默认值.不影响ime的状态.与不指定 ime-mode ...
 - TypeError: 'TestCase' object is not iterable
			
这个异常呢其实是因为我对list没有足够熟悉 我一开始很疑惑,明明已经正确返回testcase对象了呀,为啥会报TypeError: 'TestCase' object is not iterable ...
 - 【6.24校内test】T1 江城唱晚
			
[题目背景] 墙角那株海棠,是你种下的思念. 生死不能忘,高烛照容颜. 一曲江城唱晚,重忆当年坐灯前, 青衫中绣着你留下的线. ——银临<江城唱晚> [问题描述] 扶苏是个喜欢一边听古风歌 ...
 - NAT、NAPT(PAT)的基本概念和工作原理及区别
			
转自:http://blog.sina.com.cn/s/blog_5d302bd20100gprv.html 近年来,随着 Internet 的迅猛发展,连入 Internet 的主机数量成倍增长. ...
 - NOIP 2006 金明的预算方案(洛谷P1064,动态规划递推,01背包变形,滚动数组)
			
一.题目链接:P1064 金明的预算方案 二.思路 1.一共只有五种情况 @1.不买 @2.只买主件 @3.买主件和附件1(如果不存在附件也要运算,只是这时附件的数据是0,也就是算了对标准的结果也没影 ...
 - Linux---文件目录管理
			
1. Linux文件目录架构 Linux的目录结构与win的目录有很大不同,首先,没有盘符的概念:然后Linux使用斜杠/标识目录,Linux首先建立一个根目录,然后将其他文件系统挂载到这个目录下. ...
 - ubuntu 通过ppa源安装mysql5.6
			
添加mysql5.6的源 sudo add-apt-repository -y ppa:ondrej/mysql-5.6 更新源 sudo apt-get update 安装mysql5.6 sudo ...
 - The turtle Module 一个画图的模块
			
感悟: 这样写的介绍才有用 import turtle bob = turtle.Turtle() print(bob) turtle.mainloop() Turtle()方法打开一个窗口,中间带有 ...
 - day21 02 包的进阶
			
day21 02 包的进阶 1._init_.py文件的操作---导入包 根据day21 01 包的初识,建立的glance包,直接import glance后通过“包点包..点方法”是不能执行所要的 ...
 - 洛谷 2434 [SDOI2005]区间
			
[题解] 鲜活的大水题... 把区间排个序然后瞎搞就可以了,发现现在区间的左端点比之前区间的最大的右端点还大,那就增加一个答案区间.每次更新目前最大右区间. #include<cstdio> ...