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 ...
随机推荐
- Java集合(一)--Comparable和Comparator
Comparable: 是集合内部的方法实现的排序,只有一个方法 public interface Comparable<T> { public int compareTo(T o); } ...
- mac install telnet
问题: -bash: telnet: command not found -bash: brew: command not found 解决: /usr/bin/ruby -e "$(cur ...
- 牛客多校Round 10
咕咕咕.... 去烽火台和兵马俑了
- 面向对象程序设计--Java语言第二周编程题:有秒计时的数字时钟
有秒计时的数字时钟 题目内容: 这一周的编程题是需要你在课程所给的时钟程序的基础上修改而成.但是我们并不直接给你时钟程序的代码,请根据视频自己输入时钟程序的Display和Clock类的代码,然后来做 ...
- UVA-1599 Ideal Path(双向BFS)
题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...
- shoppping collection
personal shopping collections shop Table of Contents 1. phone network 2. band share 3. Motorcycle He ...
- STM32单片机串口一键下载电路与操作方法详解
STM32三种启动模式对应的存储介质均是芯片内置的,它们是:1)用户闪存 = 芯片内置的Flash.2)SRAM = 芯片内置的RAM区,就是内存啦.3)系统存储器 = 芯片内部一块特定的区域,芯片出 ...
- 解决maven无法加载本地lib/下的jar包问题(程序包XXX不存在)
这次一个项目用到maven编译,我在本地开发的时候jar包都是放在WEB-INF/lib目录下,通过 BuildPath将jar包导入,然后用MyEclipse中的:maven package命令打成 ...
- Intellij IDEA神器居然还有这些小技巧---超级好用的
Intellij IDEA神器居然还有这些小技巧----https://my.oschina.net/samgege/blog/1808622?p=8
- UVa - 12617 - How Lader
先上题目: How Lader Lader is a game that is played in a regular hexagonal board (all sides equal, all ...