就是用哪里开哪里,每次访问之前看看是否为空,为空就新建一个节点。
代码量小,非常好用。
###题目来源:BZOJ 1012
###代码:
#include <cstdio>
#include <iostream>
#define mid ((l+r)>>1)
typedef long long ll;
const int maxn = 200010;
struct Tree{
ll mx;
Tree* ch[2];
} T[maxn*20], *root;
ll cnt, D, m, Pre = 0, pp;
Tree* get(){return &T[++cnt];}
void insert(Tree *now, int l, int r, ll val, int pos){
now->mx = std::max(now->mx, val);
if(l == r) return;
int wh = 1; if(pos <= mid) wh = 0;
if(now->ch[wh] == NULL) now->ch[wh] = get();
insert(now->ch[wh], wh == 0 ? l : mid+1, wh == 0 ? mid : r, val, pos);
}
ll que(Tree *now, int l, int r, int pos1, int pos2){
if(pos1 == l && pos2 == r) return now->mx;
if(pos2 <= mid) return que(now->ch[0], l, mid, pos1, pos2);
if(pos1 >= mid+1) return que(now->ch[1], mid+1, r, pos1, pos2);
return std::max(que(now->ch[0], l, mid, pos1, mid),que(now->ch[1], mid+1, r, mid+1, pos2));
}
int main(){
scanf("%lld%lld", &m, &D);
root = get();
for(int i = 1; i <= m; i ++){
char cc[2]; ll x;
scanf("%s%lld", cc, &x);
if(cc[0] == 'A'){
x = (Pre+x)%D;
insert(root, 1, m, x, ++pp);
}else{
Pre = que(root, 1, m, pp-x+1, pp);
printf("%lld\n", Pre);
}
}
return 0;
}
本文介绍了一种数据结构——懒惰树状数组的实现方法,主要用于解决区间更新和查询问题。通过动态创建节点的方式减少内存消耗,适用于在线算法竞赛中需要高效处理区间操作的情况。
657

被折叠的 条评论
为什么被折叠?



