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