0%

ZOJ 3911(Prime Query)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5638

题目大意:

给出 n 个数,并进行一下操作:

A v l, 将下标为 l 的数加上 v;

R a l r, 将 [l, r] 范围中的数替换为 a;

Q l r, 查询[l, r] 区间中素数的个数;

数据范围:

1 <= n <= 100000;

1 <= $n_i$ <= 1000000;

1 <= l <= r <= n;

1 <= v <= 1000;

1 <= a <= 1000000;

保证无论进行什么操作这 n 个数不会超过 10000000;

解题思路:

很明显的线段树的题目,包含了区间更新和单点更新,单点实时更新,区间需要设置 lazy 标记,注意单点更新时 lazy 的状态就行了,另外素数可以用 O(nlog(n)) 的复杂度打表 O(1) 判断。

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*=============================================================================
# Author: DATASOURCE
# Last modified: 2015-10-11 19:59
# Filename: i.cpp
# Description:
=============================================================================*/

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson l, mid, ls
#define rson mid, r, rs
#define ls ((rt << 1) + 1)
#define rs ((rt << 1) + 2)
using namespace std;
const int MAXN = 100010;

struct Node{
int val, rep, sum;
};

int data[MAXN];
Node tre[MAXN << 2];

int pcnt;
int prime[1000100];
bool isPrime[10000100];

void getPrime(){
pcnt = 0;
fill(isPrime, isPrime + 10000100, true);
isPrime[0] = isPrime[1] = false;
for(int i = 2; i < 10000100; i++){
if(isPrime[i]) prime[pcnt++] = i;
for(int j = 0; j < pcnt && i * prime[j] < 10000100; j++){
isPrime[i * prime[j]] = false;
if(i % prime[j] == 0) break;
}
}
}

void pushUp(int rt){
tre[rt].sum = tre[ls].sum + tre[rs].sum;
}

void pushDown(int l, int r, int rt){
if(tre[rt].rep){
tre[ls].rep = tre[rs].rep = tre[rt].rep;
int mid = (l + r) >> 1;
if(isPrime[tre[rt].rep]){
tre[ls].sum = mid - l;
tre[rs].sum = r - mid;
}else tre[ls].sum = tre[rs].sum = 0;
tre[rt].rep = 0;
}
}

void buildTree(int l, int r, int rt){
tre[rt].rep = 0;
if(r - l == 1){
tre[rt].val = data[l];
if(isPrime[data[l]]) tre[rt].sum = 1;
else tre[rt].sum = 0;
return;
}

int mid = (l + r) >> 1;
buildTree(lson);
buildTree(rson);

pushUp(rt);
}

void update(int L, int R, int l, int r, int rt, int val){
if(L == l && R == r){
tre[rt].rep = val;
if(isPrime[val]) tre[rt].sum = r - l;
else tre[rt].sum = 0;
return;
}

pushDown(l, r, rt);

int mid = (l + r) >> 1;
if(R <= mid) update(L, R, lson, val);
else if(L >= mid) update(L, R, rson, val);
else{
update(L, mid, lson, val);
update(mid, R, rson, val);
}

pushUp(rt);
}

void update1(int L, int R, int l, int r, int rt, int val){
if(L == l && R == r){
if(tre[rt].rep) tre[rt].val = tre[rt].rep;
tre[rt].rep = 0;
tre[rt].val += val;
data[l] = tre[rt].val;
if(isPrime[tre[rt].val]) tre[rt].sum = 1;
else tre[rt].sum = 0;
return;
}

pushDown(l, r, rt);

int mid = (l + r) >> 1;
if(R <= mid) update1(L, R, lson, val);
else if(L >= mid) update1(L, R, rson, val);
else{
update1(L, mid, lson, val);
update1(mid, R, rson, val);
}

pushUp(rt);
}

int query(int L, int R, int l, int r, int rt){
if(L == l && R == r) return tre[rt].sum;

pushDown(l, r, rt);

int mid = (l + r) >> 1;
if(R <= mid) return query(L, R, lson);
else if(L >= mid) return query(L, R, rson);
else return query(L, mid, lson) + query(mid, R, rson);
}

int main(){
int t;
scanf("%d", &t);
getPrime();
while(t--){
int n, q;
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++)
scanf("%d", &data[i]);
buildTree(0, n, 0);
while(q--){
char op;
scanf(" %c", &op);
if(op == 'A'){
int v, p;
scanf("%d%d", &v, &p);
update1(p - 1, p, 0, n, 0, v);
}else if(op == 'Q'){
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(l - 1, r, 0, n, 0));
}else {
int v, l, r;
scanf("%d%d%d", &v, &l, &r);
update(l - 1, r, 0, n, 0, v);
}
}
}
return 0;
}