【线段树】模板

struct NODE
{
    int value;
    int left,right;
} node[maxn];
int father[MAX];

void BuildTree(int i,int left,int right)
{
    node[i].left = left;
    node[i].right = right;
    node[i].value = 0;
    if (left == right)
    {
        father[left] = i;
        return;
    }
    int mid = (left + right)/2;
    BuildTree(i<<1, left, mid);
    BuildTree(i<<1|1, mid + 1, right);
}

void UpdataTree(int ri)
{
    if (ri == 1)return;
    int fi = ri / 2;
    int a = node[fi<<1].value;
    int b = node[fi<<1|1].value;
    node[fi].value = max(a,b);
    UpdataTree(ri/2);
}

int Max = -9999;
void Query(int i,int l,int r)
{
    if (node[i].left == l && node[i].right == r)
    {
        Max = max(Max,node[i].value);
        return;
    }
    int ans = 0;
    i = i << 1;
    if (l <= node[i].right)
    {
        if (r <= node[i].right)
            Query(i, l, r);
        else
            Query(i, l, node[i].right);
    }
    i += 1;
    if (r >= node[i].left)
    {
        if (l >= node[i].left)
            Query(i, l, r);
        else
            Query(i, node[i].left, r);
    }
}

模板题目HDU1754:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 4000000;
const int MAX = 1000003;

struct NODE
{
    int value;
    int left,right;
} node[maxn];
int father[MAX];

void BuildTree(int i,int left,int right)
{
    node[i].left = left;
    node[i].right = right;
    node[i].value = 0;
    if (left == right)
    {
        father[left] = i;
        return;
    }
    int mid = (left + right)/2;
    BuildTree(i<<1, left, mid);
    BuildTree(i<<1|1, mid + 1, right);
}

void UpdataTree(int ri)
{
    if (ri == 1)return;
    int fi = ri / 2;
    int a = node[fi<<1].value;
    int b = node[fi<<1|1].value;
    node[fi].value = max(a,b);
    UpdataTree(ri/2);
}

int Max = -9999;
void Query(int i,int l,int r)
{
    if (node[i].left == l && node[i].right == r)
    {
        Max = max(Max,node[i].value);
        return;
    }
    int ans = 0;
    i = i << 1;
    if (l <= node[i].right)
    {
        if (r <= node[i].right)
            Query(i, l, r);
        else
            Query(i, l, node[i].right);
    }
    i += 1;
    if (r >= node[i].left)
    {
        if (l >= node[i].left)
            Query(i, l, r);
        else
            Query(i, node[i].left, r);
    }
}

int main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz
    int n,m,grade;
    while(~scanf("%d%d",&n,&m))
    {
        BuildTree(1,1,n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&grade);
            node[father[i]].value = grade;
            UpdataTree(father[i]);
        }

        while(m--)
        {
            char ch[3];
            int a,b;
            scanf("%s %d %d",ch,&a,&b);
            if (ch[0] == 'Q')
            {
                Max = 0;
                Query(1, a, b);
                printf("%d\n",Max);
            }
            else
            {
                node[father[a]].value = b;
                UpdataTree(father[a]);
            }
        }

    }
    return 0;
}
时间: 2024-10-10 10:03:52

【线段树】模板的相关文章

线段树模板(结构体)

线段树研究了两天了,总算有了点眉目,今天也把落下的题,补了一下. 贴一份线段树模板 线段树的特点: 1. 每一层都是区间[a, b]的一个划分,记 L = b - a 2. 一共有log2L层 3. 给定一个点p,从根到叶子p上的所有区间都包含点p,且其他区间都不包含点p. 4. 给定一个区间[l; r],可以把它分解为不超过2log2 L条不相交线段的并. 总结来说:线段树最近本的应用是4点: 1.单点更新:单点替换.单点增减 2.单点询问 3.区间询问:区间之和.区间最值 4.区间更新:区间

[ACM] 线段树模板

#include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ public: int l,r; int add;//附加值 int sum; }node[maxn]; int getRight(int n){//获得满足2^x>=n的最小x[从0层开始,给编号获得层数] return ceil(log10(n*1.0)/log10(2.0)); } void bu

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

【HDU 4819】Mosaic 二维线段树模板

二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节. 存模板: /* 二维线段树模板整理 */ #include<cstdio> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 805; const int INF = (1 << 30); int n; int posX[max

LA 2191电位计(线段树模板题)

线段树模板题,没啥好说的.....注意输出是case之间空一行就行.........之前一直没注意,一直wa 代码如下: #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #includ

洛谷P3372线段树模板1——线段树

题目:https://www.luogu.org/problemnew/show/P3372 线段树模板. 代码如下: #include<iostream> #include<cstdio> using namespace std; long long n,m,a[100005],ct; struct N{ long long lazy,sum; long long ls,rs; }p[200005]; void pushdown(long long cur,long long l

P3373 线段树模板

好,这是一个线段树模板. 1 #include <cstdio> 2 using namespace std; 3 const long long int N=1000010; 4 long long int sum[N],tag1[N],tag2[N],mo; 5 6 void build(int l,int r,int o) 7 { 8 tag1[o]=1; 9 if(l==r) 10 { 11 scanf ("%d",&sum[o]); 12 return;

线段树模板hdu 1754:I Hate It

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 100523    Accepted Submission(s): 37845 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的

POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120   Accepted: 43425 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o