Given integers . There are 2 types of operations:
- 1 l r: Change to ;
- 2 l r: Query the value of modulo 99971.
For each query, please output the answer.
Input
There are multiple test cases. The first line of the input is an integer (about 5), indicating the number of test cases. For each test case:
The first line contains two integers () and (), indicating the number of the given integers and the number of operations.
The second line contains integers (), indicating the given integers.
The first integer on each of the following lines will be (), indicating the type of operation.
- If equals 1, then two integers () follow, indicating the first type of operation;
- If equals 2, then two integers () follow, indicating a query.
Output
For each query, output one line containing one integer, indicating the answer.
Sample Input
1 5 3 1 2 3 4 5 2 1 5 1 1 3 2 1 3
Sample Output
15 36
题解:线段树
首先打表发现几乎所有数立方的循环节大小为48,剩下的数的循环节大小也都是48的因子,因此可以设所有数的循环节为48并考虑每种情况。这里用到线段树,sum[rt][x]表示更新x次时节点rt的数值之和,可以利用可持久化线段树的方法,搞一个永久性标记add[rt],表示节点rt更新add[rt],递归的时候只要把路径上的add[rt]都加起来就行。重点是PushUP(一开始我写PushUP只更新了sum[rt][add[rt]]的情况,因此总是不对,应该要把所有48种情况都要更新!!!)
#include<bits/stdc++.h>
#define FIN freopen("in.txt","r",stdin);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MX = 1e5 + 5;
const int mod = 99971;
LL sum[MX << 2][50];
int add[MX << 2];
int T, n, m;
void PushUP (int rt) {
for (int i = 0; i < 48; i++)
sum[rt][i] = (sum[rt << 1][ (i + add[rt << 1]) % 48] + sum[rt << 1 | 1][ (i + add[rt << 1 | 1]) % 48]) % mod;
}
void build (int l, int r, int rt) {
add[rt] = 0;
if (l == r) {
scanf ("%lld", &sum[rt][0]);
sum[rt][0] %= mod;
for (int i = 1; i < 48; i++) sum[rt][i] = sum[rt][i - 1] * sum[rt][i - 1] * sum[rt][i - 1] % mod;
return;
}
int m = (l + r) >> 1;
build (lson), build (rson);
PushUP (rt);
}
void update (int L, int R, int l, int r, int rt) {
if (L <= l && R >= r) {
add[rt] = (add[rt] + 1) % 48;
return;
}
int m = (l + r) >> 1;
if (L <= m) update (L, R, lson);
if (R > m) update (L, R, rson);
PushUP (rt);
}
LL query (int L, int R, int x, int l, int r, int rt) {
x = (x + add[rt]) % 48;
if (L <= l && R >= r) return sum[rt][x];
int m = (l + r) >> 1; LL ret = 0;
if (L <= m) ret += query (L, R, x, lson);
if (R > m) ret += query (L, R, x, rson);
return ret % mod;
}
int main() {
scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
build (1, n, 1);
int op, l, r;
while (m--) {
scanf ("%d%d%d", &op, &l, &r);
if (op == 1) update (l, r, 1, n, 1);
else printf ("%lld\n", query (l, r, 0, 1, n, 1) );
}
}
}