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 ...
随机推荐
- MyEclipse 快捷键大全(@Hcy)
MyEclipse 快捷键1(CTRL)-------------------------------------Ctrl+1 快速修复Ctrl+D: 删除当前行 Ctrl+Q 定位到最后编辑的地方 ...
- Spring+Spring MVC+Hibernate环境搭配
Spring+Spring MVC+Hibernate简称"SSH".Spring容器是Spring的核心,该 容器负责管理spring中的java组件.Spring的核心机制:依 ...
- Jenkins系列之Jenkins的工具配置和插件(二)
上一篇我们介绍了Jenkins的安装,这一篇我们介绍如何配置Jenkins的工具和如何下载插件. 首先我们先来看如何配置工具,这里的工具是指JDK.Ant.Maven.Git等. 一.如图,点击系统管 ...
- 解决header,footer等HTML5标签在IE(IE6/IE7/IE8)无效的方法
HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. HTML5的新标签元素有: <header&g ...
- 转自王垠Blog——写给清华大学的退学申请
清华梦的粉碎—写给清华大学的退学申请(转自王垠Blog) 清华梦的诞生 小时候,妈妈给我一个梦.她指着一个大哥哥的照片对我说,这是爸爸的学生,他考上了清华大学,他是我们中学的骄傲.长大后,你也要进 ...
- jquery 时间戳转日期
搜了一下发现这个时间戳转时间的代码很好用,附上实践的代码 结果如下 / * * 时间戳转日期 * @param timestamp * @returns {*} */ function timesta ...
- [luogu4056 JSOI2009] 火星藏宝图 (贪心 dp)
传送门 Solution 一个显然的贪心:选的点数越多越好.这个随便推推就知道了. 那么我们就贪心的从一列上挑最靠下的转移 直接转移不斜率优化复杂度\(O(nm)\),吸一口O2过了... Code ...
- Yii 时间日期组件与composer 下载中出现的问题
首先本篇主要讲3点 一个Yii时间日期组件的两种用法 笔者使用composer下载该组件时出现问题的解决办法 1.composer下载出现的问题 file could not be downloade ...
- ubuntu 下安装wine
PPA地址: https://launchpad.net/~ubuntu-wine/+archive/ppa 添加wine的ppa源 sudo add-apt-repository ppa:ubunt ...
- odoo domain详解
参考的以下文档: luohuayong:Odoo domain写法及运用 baimo:odoo domain表达式 1.domain 表达式规则 最简单的格式:[('字段名','操作符',值)] 例: ...