ZOJ 3911 线段树
题意:有N个数字,M个操作,然后回答每个Q开头的询问
操作形式:
A val pos:在pos位置上+val
Q l r:询问l~r之间有多少个质数
R val l r:把l~r之间的数字替换成val
分析:建成两棵树,一棵更新 原数列,一棵更新 质数序列(只有0,1)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define repu(i,a,b) for(int i=a;i<b;i++)
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = ;
const int M = ;
int h , w , n;
bool p[M];
int prime()
{
memset(p,false,sizeof(p));
p[] = p[] = true;
for(int i = ; i <= M; i += )
p[i] = true;
for(int i = ; i <= sqrt(M); i+=)
{
int k = i * ;
for(int j = i*i; j <= M; j += k)
p[j] = true;
}
}
struct Segment
{
int col[maxn<<];
int sum[maxn<<];
void init()
{
memset(col,,sizeof(col));
memset(sum,,sizeof(sum));
}
void PushUp(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
}
void PushDown(int rt,int m)
{
if(col[rt] != -)///如果是0就进不去
{
col[rt<<] = col[rt<<|] = col[rt];
sum[rt<<] = (m - (m >> )) * col[rt];
sum[rt<<|] = (m >> ) * col[rt];
col[rt] = -;
}
}
void build(int l,int r,int rt)
{
col[rt] = -;
sum[rt] = ;
if (l == r) return ;
int m = (l + r) >> ;
build(lson);
build(rson);
PushUp(rt);
}
void Update(int p,int add,int l,int r,int rt)
{
if(l == r)
{
sum[rt] += add;
return ;
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
if (p <= m) Update(p, add, lson);
else Update(p, add, rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
col[rt] = c;
///因为序列只有0,1;如果初始化为0的话,赋值有可能赋成0,pushdown就进不了,也就跟新不了数据了
sum[rt] = c * (r - l + );
return ;
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
if (L <= m) update(L , R , c , lson);
if (R > m) update(L , R , c , rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
int ret = ;
if (L <= m) ret += query(L , R , lson);
if (m < R) ret += query(L , R , rson);
PushUp(rt);
return ret;
}
} Seg,seg;
int main()
{
int T , n , m, a , b , c;
prime();
scanf("%d",&T);
for (int cas = ; cas <= T ; cas ++)
{
Seg.init();
seg.init();
scanf("%d%d",&n,&m);
Seg.build( , n , );
seg.build( , n , );
repu(i,,n+)
{
scanf("%d",&a);
Seg.Update(i,a,,n,);
if(!p[a])///如果是质数才加1
seg.Update(i,,,n,);
}
char s[];
while(m--)
{
scanf("%s",s);
if(s[] == 'R')
{
scanf("%d%d%d",&a, &b, &c);
Seg.update(b , c , a , , n , );
if(!p[a])///如果是质数,更新成1
seg.update(b , c , , , n , );
else
seg.update(b , c , , , n , );
}
else if(s[] == 'A')
{
scanf("%d%d",&a,&b);///在b位置上加a,如果加的数字成了质数,也得更新另一棵树
int t = Seg.query(b,b,,n,);
Seg.Update(b,a,,n,);
if(!p[t+a] && p[t])///原本不是质数,变成了质数
seg.Update(b,,,n,);
else if(p[t+a] && !p[t])///原本是质数,变成了合数
seg.Update(b,-,,n,);
else if(p[t+a] && p[t]) ;
else if(!p[t+a] && !p[t]) ;
}
else
{
scanf("%d%d",&a,&b);
printf("%d\n",seg.query(a,b,,n,));
}
}
}
return ;
}
AC代码
ZOJ 3911 线段树的更多相关文章
- Prime Query (ZOJ 3911 线段树)
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a sequen ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- ZOJ 3279-Ants(线段树)
传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds Memory Limit: 32768 KB echo is a curious and cleve ...
- zoj 3888 线段树 ***
卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...
- F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树 但是没有push_up 最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段 思路是 ...
- HDU 3911 线段树区间合并
北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...
- HDU 1199 && ZOJ 2301 线段树离散化
一段长度未知的线段.一种操作:a b c ,表示区间[a,b]涂为颜色C,w代表白色,b代表黑色,问终于的最长连续白色段,输出起始位置和终止位置 离散化处理.和寻常的离散化不同,须要把点化成线段.左闭 ...
- zoj 2706 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1706 trick:关于正数和负数的整除问题,正数整除是自动向下取整的 ...
- ZOJ 3299 线段树 离散化
本来是个很简单的题目,难住我的主要是这么几点 1.它所有的点都是坐标,不是实际的砖块,1,3指的是1-2 2-3的砖块...后来就是用1 代表1-2 ,2代表2-3.....,这样的话,每次读入的数据 ...
随机推荐
- YY前端课1
1. HTML页面主要由声明.头部.主体三部分构成. 2. 头部及其meta标签,以及主体的title标签很重要,对SEO.搜索优化等很重要. 3. 常用的charset编码格式有2种,一种是utf- ...
- Python:函数
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 定义和调用 >>> def add(x,y): ... print('x=',x) #Python3必须加&quo ...
- MD5加密代码
import java.security.MessageDigest;public class MD5_tes { public final static String MD5(String s){ ...
- Command Pattern 命令模式
定义: 命令模式将‘请求’封装成对象,以便使用不同的请求,队列或者日志来参数化其他对象,命令模式也支持可撤销的操作. 类图 如上图所示:Command类是用来声明执行操作的接口:ConcreteCom ...
- WebStorm调试node.js
直接上图:
- VScode调试Python
第一步,确保装上了PYTHON扩展 然后打开文件夹(这个东西必须打开文件夹才能进行调试,不能打开一个文件就调试) 打开文件夹后,那里显示没有配置,这时需要你按下F5 弹出选择环境,点击Python 它 ...
- ucenter 新增用户后 自动登录discuz 不用激活
uc_client models user.php function add_user($username, $password, $email, $uid = 0, $questionid = '' ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- java数据结构_笔记(5)_图的算法
图的算法 1 图的遍历图的遍历就是从图中某个顶点出发,按某种方法对图中所有顶点访问且仅访问一次.遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础. 2 深度优先遍历从图中某个顶点V 出发 ...
- hibernate中validate的使用(转)
原文链接:http://blog.csdn.net/xing_sky/article/details/8484551 首先是要加入下面两个包 hibernate-validator-4.1.0.Fin ...