ZOJ4009:And Another Data Structure Problem(线段树区间更新)

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

Author:  CHEN, Jingbang
Source:  ZOJ Monthly, March 2018

题意:给N个数,操作对区间内每个数变成自身的立方,查询对区间的数求和。

思路:好像是去年区域赛一个热身题,打表发现此模数下48次一个循环,用线段树维护执行的次数即可。

# include <bits/stdc++.h>
# define lson l,mid,id<<1
# define rson mid+1,r,id<<1|1
using namespace std;
typedef long long LL;
const int mod = 99971;
const int maxn = 1e5+30;
int sum[maxn*3][48], add[maxn*3];
int cal(int x)
{
    LL t=1LL*x*x%mod*x%mod;
    return (int)t;
}
void pushup(int id)
{
    for(int i=0; i<48; ++i)
    {
        sum[id][i] = sum[id<<1][(i+add[id<<1])%48] + sum[id<<1|1][(i+add[id<<1|1])%48];
        sum[id][i] %= mod;
    }
}
void build(int l, int r, int id)
{
    add[id] = 0;
    if(l==r)
    {
        scanf("%d",&sum[id][0]);
        for(int i=1; i<=47; ++i) sum[id][i]=cal(sum[id][i-1]);
        return;
    }
    int mid=l+r>>1;
    build(lson);
    build(rson);
    pushup(id);
}
void update(int L, int R, int l, int r, int id)
{
    if(L<=l && R>=r)
    {
        ++add[id];
        return;
    }
    int mid=l+r>>1;
    if(L<=mid) update(L, R, lson);
    if(R>mid) update(L, R, rson);
    pushup(id);
}
int query(int L, int R, int l, int r, int id, int pre)
{
    if(L<=l && R>=r)
        return sum[id][(add[id]+pre)%48];
    int mid = l+r>>1;
    int res = 0;
    if(L<=mid) res += query(L, R, lson, pre+add[id]);
    if(R>mid) res += query(L, R, rson, pre+add[id]);
    return res%mod;
}
int main()
{
    int t, n, m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1, n, 1);
        while(m--)
        {
            int op,x,y;
            scanf("%d%d%d",&op,&x,&y);
            if(op == 1) update(x,y,1,n,1);
            else printf("%d\n",query(x,y,1,n,1, 0));
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值