前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线段树(结构体建法_QAQ)

线段树(结构体建法_QAQ)

作者头像
Lokinli
发布2023-03-09 16:08:16
1640
发布2023-03-09 16:08:16
举报
文章被收录于专栏:以终为始

线段树(结构体)模板

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
int ans;
struct node
{
    int l, r, w;
    int f;
};
struct node tree[50000 * 4 + 1];
void BuildSegmentTree(int k, int l, int r)     // 建线段树
{
    tree[k].l = l;
    tree[k].r = r;
    if(l == r)
    {
        scanf("%lld", &tree[k].w);
        return ;
    }
    int m = (tree[k].l + tree[k].r) >> 1;
    BuildSegmentTree(k << 1, l, m);
    BuildSegmentTree(k << 1 | 1, m + 1, r);
    tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void down(int k)                      // 懒标记
{
    tree[k << 1].f += tree[k].f;
    tree[k << 1 | 1].f += tree[k].f;
    tree[k << 1]. w += tree[k].f * (tree[k << 1].r - tree[k <<1].l + 1);
    tree[k << 1 |1].w += tree[k].f * (tree[k << 1| 1].r - tree[k << 1| 1].l + 1);
    tree[k].f = 0;
}
void Single_Point_Modification(int k, int x, int y)   // 单点修改
{
    if(tree[k].l == tree[k].r)
    {
        tree[k].w += y;
        return ;
    }
    int m = (tree[k].l + tree[k].r) >> 1;
    if(x <= m) Single_Point_Modification(k << 1,x, y);
    else Single_Point_Modification(k << 1 | 1, x, y);
    tree[k].w = tree[k << 1].w + tree[k << 1 | 1]. w;
}
void Single_Point_Query(int k, int x)     // 单点查询
{
    if(tree[k].l == tree[k].r)
    {
        ans = tree[k].w;
        return ;
    }
    if(tree[k].f) down(k);
    int m = (tree[k].l + tree[k].r) >> 1;
    if(x <= m) Single_Point_Query(k << 1, x);
    else Single_Point_Query(k << 1 | 1, x);
}
void Interval_modification(int k, int x, int y,int z)  //区间修改
{
    if(tree[k].l >= x && tree[k].r <= y)
    {
        tree[k].w += (tree[k].r - tree[k].l + 1) * z;
        tree[k].f += z;
        return;
    }
    if(tree[k].f) down(k);
    int m = (tree[k].l + tree[k].r) >> 1;
    if(x <= m) Interval_modification(k << 1, x, y, z);
    if(y > m) Interval_modification(k << 1 | 1, x, y, z);
    tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void Interval_Query(int k, int x, int y)  //区间查询
{
    if(tree[k].l >= x && tree[k].r <= y)
    {
        ans += tree[k].w;
        return ;
    }
    if(tree[k].f) down(k);
    int m = (tree[k].l + tree[k].r) / 2;
    if(x <= m) Interval_Query(k << 1, x, y);
    if(y > m) Interval_Query(k << 1 | 1, x, y);
}

char s[10];
int main()
{
    int T, n;
    while(~scanf("%d",&T))
    {
        int cas = 0;
        while(T--)
        {
            scanf("%d", &n);
            BuildSegmentTree(1,1,n);
            printf("Case %d:\n",++ cas);
            while(1)
            {
                int x, y, z;
                getchar();
                scanf("%s",s);
                if(s[0] == 'E')break;
                else if(s[0]== 'A')  // 区间修改
                {
                    scanf("%d %d %d", &x, &y, &z);
                    Interval_modification(1,x,y,z);
                }
                else if(s[0] =='B')   // 区间查询
                {
                    ans = 0;
                    scanf("%d %d", &x, &y);
                    Interval_Query(1, x, y);
                    printf("%d\n",ans);
                }
                else if(s[0]=='C')  // 单点修改
                {
                    scanf("%d %d", &x, &y);
                    Single_Point_Modification(1,x,y);
                }
                else if(s[0]=='D')  // 单点查询
                {
                    ans = 0;
                    scanf("%d", &x);
                    Single_Point_Query(1,x);
                    printf("%d\n",ans);
                }
            }
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档