• 线段树自用模板


    include <string.h>

    include <stdio.h>

    include

    using namespace std;
    int n,m,a[110000],lazy[110000];
    void build(int x,int l,int r)//
    {
    if (lr)
    {
    tree[x];
    return ;
    }
    int mid=(l+r)/2;
    build(x2,l,mid);
    build(x
    2+1,mid+1,r);
    tree[x]=tree[x2]+tree[x2+1];
    }
    //单点修改
    void update(int w,int c,int l,int r,int x) //w为更新点,c为更新值
    {
    if (l
    r)
    {
    tree[x]+=c;
    }
    int mid=(l+r)/2;
    if (w<=mid)
    update(w,c,l,mid,x2);
    else
    update(w,c,mid+1,r,x
    2+1);
    tree[x]=tree[x2]+tree[x2+1];//回溯
    }
    //lazy标记
    void pushdown(int x,int l,int r)
    {
    if (lazy[x])
    {
    int mid=(l+r)/2;
    lazy[x2]+=lazy[x];
    lazy[x
    2+1]+=lazy[x];

    	tree[x*2]+=lazy[x]*(mid-l+1);
    	tree[x*2+1]+=lazy[x]*(r-mid);
    	lazy[x]=0;
    }
    

    }
    //区间更新
    //add为更新值,L,R为更新范围,l,r为线段树范围
    void update_range(int add,int L,int R,int l,int r,int x)
    {
    if (L<=l&&R>=r)
    {
    lazy[x]+=add;
    tree[x]+=(r-l+1)*add;
    return;
    }
    push_down(x,l,r);

    int mid=(l+r)/2;
    if (mid>=L)
    	update_down(add,L,R,l,r,x*2);
    if (mid<R)
    	update_down(add,L,R,l,r,x*2+1);
    tree[x]=tree[x*2]+tree[x*2+1];
    

    }
    //区间查找
    long long query_range(int x,int L,int R,int l,int r)
    {
    if (L<=l&&R>=r)
    return tree[x];
    push_down(x,l,r);

    int mid=(l+r)/2;
    long long sum=0;
     if (mid>=L)
     sum+=query_range(x*2,L,R,l,mid);
     if (mid<R)
     sum+=query_range(x*2+1,L,R,mid+1,r);
    return sum;
    

    }

    int main()
    {
    int i,j;
    while(~scanf ("%d%d",&n,&m)&&n&&m)
    {
    int x,z,y;
    for (i=0; i<n; i++)
    {
    scanf ("%d%d%d",&x,&y,&z);
    build(x,y,z);
    }
    }
    return 0;
    }

  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/shidianshixuan/p/13746601.html
Copyright © 2020-2023  润新知