zoukankan      html  css  js  c++  java
  • LCT 实现区间加减区间求和

    Description

    用 LCT 实现序列的区间加减,区间求和。

    Solution

    对于每一个操作,只需要用一下 split 操作,把 u~v 的所有点弄到一个 splay 里面去即可

    为了支持打标记,需要维护每个点的子树大小

    pushup 时,将某个点的子树大小更新为其左右孩子子树大小和 +1 即可

    注意加法标记是需要下传的


    自己做的时候遇到的一个小问题是:pushup 时更新 sum 需要用到单个节点的权值 val[p],这个东西怎么单独维护

    后来发现这个东西也可以懒掉,即只有标记下传到这个点的时候才更新它,或者更确切地说,我们额外记录一份 val[],这份东西的更新状态和 lct 中的区间和 a[] 保持一致就好了

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 1000000;
    
    int n,m,val[N];
    
    namespace lct {
    	int top, q[N], ch[N][2], fa[N], rev[N];
    	int a[N], tag[N], siz[N];
    	inline void pushup(int x){
    	    a[0] = siz[0] = 0;
    		a[x] = a[ch[x][0]] + a[ch[x][1]] + val[x];
    		siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
    	}
    	void add(int p,int v) {
    	    if(!p) return;
            val[p] += v;
            a[p] += siz[p]*v;
            tag[p] += v;
    	}
    	inline void pushdown(int x){
    		if(rev[x]) {
                rev[ch[x][0]]^=1;
                rev[ch[x][1]]^=1;
                rev[x]^=1;
                swap(ch[x][0],ch[x][1]);
    		}
    		if(tag[x]) {
                add(ch[x][0],tag[x]);
                add(ch[x][1],tag[x]);
                tag[x]=0;
    		}
    	}
    	inline bool isroot(int x){
    		return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
    	}
    	inline void rotate(int p){
    		int q=fa[p], y=fa[q], x=ch[fa[p]][1]==p;
    		ch[q][x]=ch[p][x^1]; fa[ch[q][x]]=q;
    		ch[p][x^1]=q; fa[q]=p; fa[p]=y;
    		if(y) if(ch[y][0]==q) ch[y][0]=p;
    		else  if(ch[y][1]==q) ch[y][1]=p;
    		pushup(q);
    		pushup(p);
    	}
    	inline void splay(int x){
    		q[top=1]=x;
    		for(int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i];
    		for(int i=top;i;i--) pushdown(q[i]);
    		for(;!isroot(x);rotate(x))
    			if(!isroot(fa[x]))
    				rotate((ch[fa[x]][0]==x)==(ch[fa[fa[x]]][0]==fa[x])?fa[x]:x);
    	}
    	void access(int x){
    		for(int t=0;x;t=x,x=fa[x])
    			splay(x),ch[x][1]=t,pushup(x);
    	}
    	void makeroot(int x){
    		access(x);
    		splay(x);
    		rev[x]^=1;
    	}
    	int find(int x){
    		access(x);
    		splay(x);
    		while(ch[x][0]) x=ch[x][0];
    		return x;
    	}
    	void split(int x,int y){
    		makeroot(x);
    		access(y);
    		splay(y);
    	}
    	void cut(int x,int y){
    		split(x,y);
    		if(ch[y][0]==x)
    			ch[y][0]=0, fa[x]=0;
    	}
    	void link(int x,int y){
    		makeroot(x);
    		fa[x]=y;
    		pushup(y);
    	}
    	int query(int x,int y) {
            split(x,y);
            return a[y];
    	}
    }
    
    signed main(){
    	ios::sync_with_stdio(false);
    	int n,m,t1,t2,t3,t4;
    	cin>>n>>m;
    	for(int i=1;i<=n;i++) {
            lct::siz[i]=1;
    	}
    	for(int i=1;i<n;i++) {
            lct::link(i,i+1);
    	}
    	for(int i=1;i<=n;i++) {
            cin>>t1;
            val[i]=t1;
    	}
    	for(int i=1;i<=m;i++) {
            cin>>t1>>t2>>t3;
            if(t1==1) cin>>t4;
            if(t1==1) {
                lct::split(t2,t3);
                lct::add(t3,t4);
            }
            else {
                cout<<lct::query(t2,t3)<<endl;
            }
    	}
    }
    
    
  • 相关阅读:
    Java学习之路----计算圆形的面积和周长
    数据库系统的基本组成内容
    软件测试的含义以及测试的对象
    wg sync.WaitGroup执行顺序
    go channel
    字符串操作
    scanf
    py停止工作
    jira索引失败
    py kafka
  • 原文地址:https://www.cnblogs.com/mollnn/p/13182550.html
Copyright © 2011-2022 走看看