ZOJ-4009 And Another Data Structure Problem(线段树)

本文介绍了一种使用线段树解决特定数学运算问题的方法,通过对输入整数的立方循环节进行分析,并利用线段树高效处理更新与查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

And Another Data Structure Problem

Time Limit: 7 Seconds      Memory Limit: 262144 KB

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) );
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值