Range Update Point Query

Range Update Point Query

题意:

给一个大小为n的数组a,现在有q个操作,操作分为两种:

  • 输入1,l,r,表示将l~r的所以数变成他们每一位的和,假如a[i]=123,则操作和a[i]=1+2+3=6。
  • 输入2,x,即输出当前的a[x]。
    (1<=t<=1000)(1<=n,q<=2e5)(1<=a[i]<=1e9)。

思路:

先预处理出每个数的所以操作结果,然后再用树状数组维护每个位置的操作数即可。

代码:

/*************************************************************************
  > File Name: b.cpp
  > Author: Beans
  > Mail: 3112748286@qq.com 
  > Created Time: 2023/5/12 16:41:50
 ************************************************************************/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define int long long
#define endl '\n'

using namespace std;

const int maxn = 3e5 + 7;

int t, n, q;
int tr[maxn * 4];
int a[maxn];

int lowbit(int x){

    return x & -x;
}

void add(int x, int c){

    for(int i = x; i <= n; i += lowbit(i))
        tr[i] += c;
}

int query(int x){

    int ans = 0;
    for(int i = x; i; i -= lowbit(i))
        ans += tr[i];
    return ans;
}

void solve(){

    cin >> n >> q;
    memset(tr, 0, sizeof tr);
    vector<vector<int>> p(n + 1, vector<int>());
    for(int i = 1; i <= n; i ++ ){

        int x;
        cin >> x;
        a[i] = x;
        if(x < 10)  p[i].push_back(x);
        else {

            while(x >= 10){

                int num = x;
                int ans = 0;
                while(num){

                    ans += num % 10;
                    num /= 10;
                }
                p[i].push_back(ans);
                x = ans;
            }
            if(x)   p[i].push_back(x);
        }
    }
    while(q -- ){

        int op;
        cin >> op;
        if(op == 1){

            int l, r;
            cin >> l >> r;
            add(l, 1);
            add(r + 1, -1);
        }
        else {

            int x;
            cin >> x;
            int cnt = query(x);
            if(cnt == 0)    cout << a[x] << endl;
            else    cout << p[x][min(cnt, (int)p[x].size()) - 1] << endl;
        }
    }
}

signed main(){

    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> t;
    while(t -- )
        solve();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方哲Beans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值