数据结构 线段树与树状数组

一、线段树

Reference:https://www.cnblogs.com/AC-King/p/7789013.html

要解决的问题:

1.查询区间[L,R]之间的最值

2.修改a[i]为x;

 

明确可以解决的问题:

必须是满足区间可加性的问题,例如:

符合区间加法的例子:
数字之和——总数字之和 = 左区间数字之和 + 右区间数字之和
最大公因数(GCD)——总GCD = gcd( 左区间GCD , 右区间GCD );
最大值——总最大值=max(左区间最大值,右区间最大值)
不符合区间加法的例子:
众数——只知道左右区间的众数,没法求总区间的众数
01序列的最长连续零——只知道左右区间的最长连续零,没法知道总的最长连续零

 

 

线段树的建立

#include 
using namespace std;
const int maxn = 110;
int a[maxn];
int minv[4 * maxn];
void pushup(int id) {
    minv[id] = min(minv[id << 1],minv[id << 1 | 1]);
}
void build(int id,int l,int r) {
    if(l == r) {
        minv[id] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    pushup(id);
}
int main() {
    int n;
    cin >> n;
    for(int i = 1;i <= n; ++i) {
        cin >> a[i];
    }
    build(1,1,n);
    return 0;
}

2.线段树的单点更新

#include 
using namespace std;
const int maxn = 110;
int a[maxn];
int minv[4 * maxn];
void pushup(int id) {
    minv[id] = min(minv[id << 1], minv[id << 1 | 1]);
}
void build(int id, int l, int r) {
    if (l == r) {
        minv[id] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    pushup(id);
}
void update(int id,int l,int r,int x,int v) {
    if(l == r) {
        minv[id] = v;
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) {
        update(id << 1, l, mid, x, v);
    }else {
        update(id << 1 | 1, mid + 1, r, x ,v);
    }
    pushup(id);
}
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    build(1, 1, n);
    int q;
	cin >> q;
    for(int i = 0;i < q; ++i) {
        int x,v;
        cin >> x >> v;
        update(1, 1, n, x, v);
    }
    
    return 0;
}

3.线段树的区间查询

#include 
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
int a[maxn];
int minv[4 * maxn];
void pushup(int id) {
    minv[id] = min(minv[id << 1], minv[id << 1 | 1]);
}
void build(int id, int l, int r) {
    if (l == r) {
        minv[id] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    pushup(id);
}
void update(int id, int l, int r, int x, int v) {
    if (l == r) {
        minv[id] = v;
        return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
        update(id << 1, l, mid, x, v);
    } else {
        update(id << 1 | 1, mid + 1, r, x, v);
    }
    pushup(id);
}
int query(int id, int l, int r, int x, int y) { // 区间查询
    if(x <= l && r <= y) {
        return minv[id];
    }
    int mid = (l + r) >> 1;
	int ans = inf;
    if(x <= mid) {
        ans = min(ans,query(id << 1,l, mid, x, y));
    }
    if(y > mid) {
        ans = min(ans, query(id << 1 | 1,mid + 1, r, x, y));
    }
    return ans;
}
int query(int id, int l, int r, int x) { //单点查询
    if (l == r) {
        return minv[id];
    }
    int mid = (l + r) >> 1;
    if (mid <= x) {
        return query(id << 1, l, mid, x);
    } else {
        return query(id << 1 | 1, mid + 1, r, x);
    }
}
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    build(1, 1, n);
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int x, v;
        cin >> x >> v;
        update(1, 1, n, x, v);
    }
    int p;
    cin >> p;
    for(int i = 0;i < p; ++i){
        int l, r;
        cin >> l >> r;
        cout << query(1,1,n,l,r) << endl;
    }
    return 0;
}

二、树状数组

1.求lowbit

lowboy(x) = x & (-x);

2.查询及更新

#include 
using namespace std;
const int maxn = 110;
int C[maxn], n;
int lowbit(int x) {
    return x &(-x);
} 
int getsum(int x) {
    int res = 0;
    for(int i = x;i > 0; i-=lowbit(i)) {
		res += C[i];        
    }
    return res;
}
void update(int x,int v) {
    for(int i = x; i <= n; i+= lowbit(i)) {
        C[i] += v;
    }
}
int main() {
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int v;
        cin >> v;
    	update(i,v);
    }
    int q;
    cin >> q;
    while(q--) {
        int l,r;
        cin >> l>> r;
        cout << getsum(r) - getsum(l - 1) << endl;
    }
    return 0;    
}

 

---分割线---

几道入门题目:

1.

题目:POJ3264 http://poj.org/problem?id=3264

解答:https://blog.csdn.net/Sensente/article/details/100799498

2.

题目:HDU1754 http://acm.hdu.edu.cn/showproblem.php?pid=1754

解答:https://blog.csdn.net/Sensente/article/details/100822674

3.

题目:HDU1166 http://acm.hdu.edu.cn/showproblem.php?pid=1166

解答:https://blog.csdn.net/Sensente/article/details/100829978

4.

题目:HDU1698 Just a Hook(延时标记线段树)

解答:https://blog.csdn.net/Sensente/article/details/100890962

你可能感兴趣的:(线段树,数据结构,NEW,ACM,ICPC,数据结构,线段树)