题目链接:https://www.nowcoder.com/acm/contest/163/H
四糸智乃的题解: https://www.nowcoder.com/discuss/90721?type=101&order=0&pos=1&page=0
来源:牛客网
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
You have N integers A1, A2, … , AN. You are asked to write a program to receive and execute two kinds of instructions:
1. C a b means performing Ai = (Ai2 mod 2018) for all Ai such that a ≤ i ≤ b.
2. Q a b means query the sum of Aa, Aa+1, …, Ab. Note that the sum is not taken modulo 2018.
The first line of the input is T(1≤ T ≤ 20), which stands for the number of test cases you need to solve.
The first line of each test case contains N (1 ≤ N ≤ 50000).The second line contains N numbers, the initial values of A1, A2, …, An. 0 ≤ Ai < 2018. The third line contains the number of operations Q (0 ≤ Q ≤ 50000). The following Q lines represents an operation having the format “C a b” or “Q a b”, which has been described above. 1 ≤ a ≤ b ≤ N.
For each test case, print a line “Case #t:” (without quotes, t means the index of the test case) at the beginning.
You need to answer all Q commands in order. One answer in a line.
示例1
1
8
17 239 17 239 50 234 478 43
10
Q 2 6
C 2 7
C 3 4
Q 4 7
C 5 8
Q 6 7
C 1 8
Q 2 5
Q 3 4
Q 1 8
Case #1:
779
2507
952
6749
3486
9937
线段树的好题,如果不能探究出幂次的规律,会发现对1e5的区间进行的1e5个询问十分棘手,退化到单点更新必然TLE;经过观察,我们只需要处理好循环节即可达到区间更新的效果。在这里0..2017的幂次%2018形成的循环节长度不会超过6
注意pushdown和update的写法。
每次lazy就相当于循环节往后移动一位;
假如某非叶节点的左右孩子的sum值都入了循环节,那么此节点所管辖的区间sum值也入了循环节。
如此,区间更新,时间复杂度可以接受。
#include
#define ll long long
#define L(u) u<<1
#define R(u) u<<1|1
using namespace std;
const int MX = 50001;
int T, n, q, l, r;
char ops;
bool circle[2018];
ll a[MX];
bool checkChain(int x) {
int t = x;
for (int i = 0; i < 6; ++i) x = x * x % 2018;
return t == x;
}
struct treeNode {
long long sum[6];
bool in_circle;
int l, r, lazy;
};
struct segmentTree {
treeNode tree[MX << 2];
void pushDown(int u) { //Key Point, Keynote
if (tree[u].lazy != 0) {
ll tmp[6];
for (int i = 0; i < 6; ++i)
tmp[i] = tree[u].sum[(i + tree[u].lazy) % 6];
memcpy(tree[u].sum, tmp, sizeof(tmp));
if (tree[u].l != tree[u].r) {
tree[L(u)].lazy += tree[u].lazy;
tree[R(u)].lazy += tree[u].lazy;
}
tree[u].lazy = 0;
}
}
void update(int u) {
pushDown(L(u));
pushDown(R(u));
tree[u].in_circle = tree[L(u)].in_circle && tree[R(u)].in_circle;
if (tree[u].in_circle) {
for (int i = 0; i < 6; ++i)
tree[u].sum[i] = tree[L(u)].sum[i] + tree[R(u)].sum[i];
} else
tree[u].sum[0] = tree[L(u)].sum[0] + tree[R(u)].sum[0];
}
void build(int u, int l, int r) {
tree[u].l = l;
tree[u].r = r;
tree[u].lazy = 0;
tree[u].in_circle = false;
memset(tree[u].sum, 0, sizeof(tree[u].sum));
if (l != r) {
int mid = l + r >> 1;
build(L(u), l, mid);
build(R(u), mid + 1, r);
update(u);
} else {
tree[u].sum[0] = a[l];
if (circle[a[l]]) {
tree[u].in_circle = true;
for (int i = 1; i < 6; ++i)
tree[u].sum[i] = tree[u].sum[i - 1] * tree[u].sum[i - 1] % 2018;
}
}
}
void change(int u, int l, int r) {
if (l == tree[u].l && r == tree[u].r) {
if (tree[u].in_circle) tree[u].lazy++;
else {
if (tree[u].l == tree[u].r) {
tree[u].sum[0] = tree[u].sum[0] * tree[u].sum[0] % 2018;
if (circle[tree[u].sum[0]]) {
tree[u].in_circle = true;
for (int i = 1; i < 6; ++i) {
tree[u].sum[i] = tree[u].sum[i - 1] * tree[u].sum[i - 1] % 2018;
}
}
} else {
int mid = tree[u].l + tree[u].r >> 1;
change(L(u), l, mid);
change(R(u), mid + 1, r);
update(u);
}
}
return;
}
int mid = tree[u].l + tree[u].r >> 1;
if (r <= mid)change(L(u), l, r);
else if (l > mid)change(R(u), l, r);
else {
change(L(u), l, mid);
change(R(u), mid + 1, r);
}
update(u);
}
ll sum(int u, int l, int r) {
pushDown(u);
if (tree[u].l == l && tree[u].r == r)
return tree[u].sum[0];
int mid = tree[u].l + tree[u].r >> 1;
if (r <= mid)return sum(L(u), l, r);
else if (l > mid)return sum(R(u), l, r);
else return sum(L(u), l, mid) + sum(R(u), mid + 1, r);
}
};
segmentTree ST;
int main() {
//freopen("../in","r",stdin);
for (int i = 0; i < 2018; ++i) circle[i] = checkChain(i);
scanf("%d", &T);
for (int I = 1; I <= T; ++I) {
printf("Case #%d:\n", I);
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
ST.build(1, 1, n);
scanf("%d", &q);
while (q--) {
scanf(" %c %d %d", &ops, &l, &r);
if (ops == 'Q')
printf("%lld\n", ST.sum(1, l, r));
else ST.change(1, l, r);
}
}
return 0;
}