码迷,mamicode.com
首页 > 其他好文 > 详细

ZKW线段树 非递归版本的线段树

时间:2018-08-28 14:25:45      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:define   space   isp   map   fine   fir   blog   soc   tor   

学习和参考

 

下面是支持区间修改和区间查询的zkw线段树模板,先记下来。

技术分享图片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <iomanip>
#include   <cstdlib>
#include    <cstdio>
#include    <string>
#include    <vector>
#include    <bitset>
#include    <cctype>
#include     <queue>
#include     <cmath>
#include      <list>
#include       <map>
#include       <set>
//#include <unordered_map>
//#include <unordered_set>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
//__gnu_pbds::cc_hash_table<int,int>ret[11];    //这是很快的hash_map
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18

const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}

/*-----------------------showtime----------------------*/

            const int maxn = 100009;
            ll tree[maxn << 2],add[maxn<<2];
            int N = 1,n,m;;
            void  build(){
                for(; N<=n+1; N<<=1);
                for(int i = N+1; i<=N+n; i++)
                    scanf("%d", tree + i);
                for(int i = N-1; i>=1; i--)
                    tree[i] = tree[i<<1] + tree[i<<1|1];
            }

            void update(int s,int t,int k){
                int lnum = 0,rnum = 0,num = 1;
                for(s = N + s -1, t = N + t + 1; s ^ t ^ 1; s>>=1,t>>=1,num<<=1){
                    tree[s] += 1ll * k * lnum;
                    tree[t] += 1ll * k * rnum;

                    if(~s & 1) {
                        add[s ^ 1] += k;
                        tree[s^1] += 1ll * k * num;
                        lnum += num;
                    }
                    if(t&1) {
                        add[t^1] += k;
                        tree[t^1] += 1ll*k*num;
                        rnum += num;
                    }
                }
                for(; s; s>>=1,t>>=1){
                    tree[s] += 1ll*k * lnum;
                    tree[t] += 1ll * k * rnum;
                }
            }

            ll query(int s,int t){
                int lnum = 0,rnum = 0,num = 1;
                ll ans = 0;
                for(s=N+s-1,t=N+t+1; s ^ t ^ 1; s>>=1, t>>=1, num<<=1){
                    if(add[s]) ans += 1ll*add[s] * lnum;
                    if(add[t]) ans += 1ll*add[t] * rnum;

                    if(~s & 1){ans += 1ll*tree[s^1] ; lnum += num;}
                    if(t & 1){ans += 1ll*tree[t ^ 1]; rnum += num;}
                }

                for(; s;s>>=1,t>>=1){
                    ans += 1ll*add[s] * lnum;
                    ans += 1ll*add[t] * rnum;
                }
                return ans;
            }
int main(){

            scanf("%d%d", &n, &m);
            build();
            for(int i=1; i<=m; i++){
                int op;
                scanf("%d", &op);
                if(op == 1) {
                    int l,r,k;
                    scanf("%d%d%d", &l, &r, &k);
                    update(l,r,k);
                }
                else {
                    int l,r;
                    scanf("%d%d", &l, &r);
                    printf("%lld\n", query(l,r));
                }
            }
            return 0;
}
ZKW

 

ZKW线段树 非递归版本的线段树

标签:define   space   isp   map   fine   fir   blog   soc   tor   

原文地址:https://www.cnblogs.com/ckxkexing/p/9547641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!