· 区间查询
query函数:
当线段树当前结点范围被要查询范围包围时直接返回。
否则判断左右子树的遍历方向。测试数据不会出现越界的情况,不需考虑。
配套题目:click
#include<bits/stdc++.h>
using namespace std;
int a[100010],treea[400010],treeb[400010],ma,mi;
void build(int p,int l,int r)
{
if(l==r)
{
treea[p]=treeb[p]=a[l];
return;
}
int mid=(l+r)/2;
build(2*p,l,mid);
build(2*p+1,mid+1,r);
treea[p]=max(treea[2*p],treea[2*p+1]);
treeb[p]=min(treeb[2*p],treeb[2*p+1]);
}
void search(int p,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
ma=max(ma,treea[p]);
mi=min(mi,treeb[p]);
return;
}
int mid=(l+r)/2;
if(y<=mid) search(2*p,l,mid,x,y);
else if(x>mid) search(2*p+1,mid+1,r,x,y);
else{
search(2*p,l,mid,x,y);
search(2*p+1,mid+1,r,x,y);
}
}
int main()
{
int n,q; cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
while(q--)
{
ma=0, mi=0x3f3f3f3f;
int x,y; cin>>x>>y;
search(1,1,n,x,y);
cout<<ma-mi<<endl;
}
return 0;
}
· 分步建树
将数组长度作为分界线进行左右子树建立,当l==r时,就是最新元素的位置
配套题目:click
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
ll tree[4*MAXN],t;
void update(int node,int nodeleft,int noderight,int len,ll val){
if(nodeleft==noderight){
tree[node]=val; return;
}
int mid=(nodeleft+noderight)>>1;
if(mid>=len) update(node*2,nodeleft,mid,len,val);
else update(node*2+1,mid+1,noderight,len,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
ll find(int node,int nodeleft,int noderight,int l,int r){
if(l<=nodeleft&&noderight<=r){
return tree[node];
}
int mid=(nodeleft+noderight)>>1;
if(r<=mid) return find(node*2,nodeleft,mid,l,r);
else if(l>mid) return find(node*2+1,mid+1,noderight,l,r);
else{
return max(find(node*2,nodeleft,mid,l,r),find(node*2+1,mid+1,noderight,l,r));
}
}
int main(){
int n,d; cin>>n>>d;
int len=0;
for(int i=0;i<n;i++){
char ch; int a;
cin>>ch>>a;
if(ch=='A'){
len++;
update(1,1,MAXN-1,len,(a+t)%d);
}
else{
t=find(1,1,MAXN-1,len-a+1,MAXN-1);
cout<<t<<endl;
}
}
return 0;
}
· 区间查询与更改模板
题目:click
#include<iostream>
#define int long long
using namespace std;
typedef long long ll;
const int N = 100005;
int w[N];
struct node {
int l, r;
ll sum;
ll add;
}tree[N<<2];
void build(int node,int nodeleft,int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,w[nodeleft],0 };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
void pushdown(int node) {
struct node& root = tree[node], & left = tree[node * 2], & right = tree[node * 2 + 1];
if (root.add) {
left.add += root.add; left.sum += (left.r - left.l + 1) * root.add;
right.add += root.add; right.sum += (right.r - right.l + 1) * root.add;
root.add = 0;
}
}
void update(int node, int left, int right, int val) {
if (left <= tree[node].l && tree[node].r <= right) {
tree[node].sum += (tree[node].r - tree[node].l + 1) * val;
tree[node].add += val;
}
else {
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (left <= mid) update(node * 2, left, right, val);
if (right > mid) update(node * 2 + 1, left, right, val);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].sum;
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return query(node * 2, left, right) + query(node * 2 + 1, left, right);
}
}
signed main() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
if (c == 'C') {
int l, r, val; cin >> l >> r >> val;
update(1, l, r, val);
}
else {
int l, r; cin >> l >> r;
cout << query(1, l, r) << endl;
}
}
return 0;
}
· 单点修改区间查询模板
题目:click
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int num[N];
struct node {
int l, r;
int MAX;
}tree[N<<2];
void build(int node, int nodeleft, int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,num[nodeleft] };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
void update(int node, int id, int val) {
if (tree[node].l == id && tree[node].r == id) {
tree[node].MAX = val;
}
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (id <= mid) update(node * 2, id, val);
else update(node * 2 + 1, id, val);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].MAX;
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return max(query(node * 2, left, right), query(node * 2 + 1, left, right));
}
}
}
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) cin >> num[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
int a, b; cin >> a >> b;
if (c == 'Q') {
cout << query(1, a, b) << endl;
}
else {
update(1, a, b);
}
}
}
return 0;
}
·单点修改与查询 区间无效性
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
struct node {
int l, r, val, flag;
}t[N<<2];
int w[N];
void pushdown(int u) {
if (t[u].flag == -1) return;
t[u << 1].val = t[u << 1 | 1].val = t[u].flag;
t[u << 1].flag = t[u << 1 | 1].flag = t[u].flag;
t[u].flag = -1;
}
void build(int u, int l, int r) {
if (l == r) t[u] = { l,r,w[l],-1 };
else {
t[u] = { l,r,0,-1 };
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
}
void modify(int u, int id, int val) {
if (t[u].l == id && t[u].r == id) t[u].val += val;
else {
pushdown(u);
int mid = t[u].l + t[u].r >> 1;
if (id <= mid) modify(u << 1, id, val);
else modify(u << 1 | 1, id, val);
}
}
int query(int u, int id) {
if (t[u].l == id && t[u].r == id) return t[u].val;
pushdown(u);
int mid = t[u].l + t[u].r >> 1;
if (id <= mid) return query(u << 1, id);
else return query(u << 1 | 1, id);
}
signed main() {
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> w[i];
build(1, 1, n);
int q; cin >> q;
while (q--) {
int op; cin >> op;
if (op == 1) {
int x; cin >> x;
t[1].val = x;
t[1].flag = x;
}
else if (op == 2) {
int id, x; cin >> id >> x;
modify(1, id, x);
}
else {
int id; cin >> id;
cout << query(1, id) << endl;
}
}
return 0;
}