题目链接:点我~~
题意:给n个数,有两种操作,一种是查询区间和,另一种是在区间上每一个数加上v。
思路:第一次摸splay tree,这题算是个模板题,适合思考人生。。。
//Splay(x,0); 将x变为跟节点
//Splay(x,root); 将x变为root下的节点 维护区间时通常旋转为root的右子树
//所需要维护的区间[l,r],通过旋转后即为 root右孩子的左子树
模板参考链接:ˋ( ° ▽、° )  ̄へ ̄
//#include <bits/stdc++.h> #include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<queue> #include<cmath> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PI; typedef pair< PI, int> PII; const double eps=1e-5; const double pi=acos(-1.0); const int mod=1e9+7; const int INF=0x3f3f3f3f; #define Key_value ch[ch[root][1]][0] const int MAXN = 300000; const int MAXM = 20100; int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN]; int root,tot1; LL sum[MAXN]; int s[MAXN],tot2;//内存池和容量 int a[MAXN]; int n,q; int add[MAXN]; void NewNode(int &r,int father,int k) { if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就是++tot2 else r = ++tot1; pre[r] = father; ch[r][0] = ch[r][1] = 0; key[r] = k; sum[r] = k; size[r] = 1; } void Update_add(int r,int v) { if(!r)return; add[r] += v; key[r] += v; sum[r] += (LL)v*size[r]; } void push_up(int r) { int lson = ch[r][0], rson = ch[r][1]; size[r] = size[lson] + size[rson] + 1; sum[r] = sum[lson] + sum[rson] + key[r]; } void push_down(int r) { if(add[r]) { int lson = ch[r][0], rson = ch[r][1]; Update_add(lson,add[r]); Update_add(rson,add[r]); add[r]=0; } } void Build(int &x,int l,int r,int father) { if(l > r)return; int mid = (l+r)/2; NewNode(x,father,a[mid]); Build(ch[x][0],l,mid-1,x); Build(ch[x][1],mid+1,r,x); push_up(x); } void Init() { root = tot1 = tot2 = 0; ch[root][0] = ch[root][1] = size[root] = pre[root] = 0; sum[root] = key[root] = 0; NewNode(root,0,-1); NewNode(ch[root][1],root,-1); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); Build(Key_value,1,n,ch[root][1]); push_up(ch[root][1]); push_up(root); } //旋转,0为左旋,1为右旋 void Rotate(int x,int kind) { int y = pre[x]; push_down(y); push_down(x); ch[y][!kind] = ch[x][kind]; pre[ch[x][kind]] = y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y] = x; pre[x] = pre[y]; ch[x][kind] = y; pre[y] = x; push_up(y); } //Splay调整,将r结点调整到goal下面 void Splay(int r,int goal) { push_down(r); while(pre[r] != goal) { if(pre[pre[r]] == goal) { push_down(pre[r]); push_down(r); Rotate(r,ch[pre[r]][0] == r); } else { push_down(pre[pre[r]]); push_down(pre[r]); push_down(r); int y = pre[r]; int kind = ch[pre[y]][0]==y; if(ch[y][kind] == r) { Rotate(r,!kind); Rotate(r,kind); } else { Rotate(y,kind); Rotate(r,kind); } } } push_up(r); if(goal == 0) root = r; } int Get_kth(int r,int k) //得到第k个结点 { push_down(r); int t = size[ch[r][0]] + 1; if(t == k)return r; if(t > k)return Get_kth(ch[r][0],k); else return Get_kth(ch[r][1],k-t); } //因为加了个空结点,所以将第l个点旋转到根结点,第r+2个点旋转到根结点的右孩子 //l-1 ~~ r+1 LL Get_Sum(int l,int r) { Splay(Get_kth(root,l),0); Splay(Get_kth(root,r+2),root); return sum[Key_value]; } void modify(int l,int r,int v) { Splay(Get_kth(root,l),0); Splay(Get_kth(root,r+2),root); Update_add(Key_value,v); push_up(ch[root][1]); push_up(root); } void Treavel(int x) { if(x) { Treavel(ch[x][0]); printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size\ = %2d\n",x,ch[x][0],ch[x][1],pre[x],size[x]); Treavel(ch[x][1]); } } void debug() { printf("root:%d\n",root); Treavel(root); } int main() { scanf("%d%d",&n,&q); Init(); char op[20]; int x,y,z; while(q--) { scanf("%s",op); int l,r,v; if(op[0]=='Q') { scanf("%d%d",&l,&r); cout<<Get_Sum(l,r)<<endl;; } else { scanf("%d%d%d",&l,&r,&v); modify(l,r,v); } } return 0; }
==第二版==
//#include <bits/stdc++.h> #include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<queue> #include<cmath> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PI; typedef pair< PI, int> PII; const double eps=1e-5; const double pi=acos(-1.0); const int mod=1e9+7; const int INF=0x3f3f3f3f; #define Key_value ch[ch[root][1]][0] #define keyTree (ch[ch[root][1]][0]) const int MAXN = 300000; const int MAXM = 20100; struct SplayTree { int sz[MAXN]; int ch[MAXN][2]; int pre[MAXN]; int root, top1; /*这是题目特定变量*/ int num[MAXN]; int val[MAXN]; int add[MAXN]; LL sum[MAXN]; inline void Rotate(int x,int f) //旋转,0为左旋,1为右旋 { int y = pre[x]; push_down(y); push_down(x); ch[y][!f] = ch[x][f]; pre[ ch[x][f] ] = y; pre[x] = pre[y]; if(pre[x]) ch[ pre[y] ][ ch[pre[y]][1] == y ] = x; ch[x][f] = y; pre[y] = x; push_up(y); } inline void Splay(int x,int goal) //将x旋转到goal的下面 { push_down(x); //防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了 while(pre[x] != goal) { if(pre[pre[x]] == goal) { Rotate(x, ch[pre[x]][0] == x); } else { int y = pre[x], z = pre[y]; int f = (ch[z][0] == y); if(ch[y][f] == x) { Rotate(x, !f), Rotate(x, f); } else { Rotate(y, f), Rotate(x, f); } } } push_up(x); if(goal == 0) root = x; } inline void RotateTo(int k,int goal) //把第k位的数转到goal下边 { int x = root; push_down(x); while(sz[ ch[x][0] ] != k) { if(k < sz[ ch[x][0] ]) { x = ch[x][0]; } else { k -= (sz[ ch[x][0] ] + 1);//多减去1,所以k+1位 x = ch[x][1]; } push_down(x); } Splay(x,goal); } inline int Get_kth(int r,int k) //得到第k个结点 { push_down(r); int t = sz[ch[r][0]] + 1; if(t == k)return r; if(t > k)return Get_kth(ch[r][0],k); else return Get_kth(ch[r][1],k-t); } //以上一般不修改////////////////////////////////////////////////////////////////////////////// void debug() { printf("%d\n",root); Treaval(root); } void Treaval(int x) { if(x) { Treaval(ch[x][0]); printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][0],ch[x][1],pre[x],sz[x],val[x]); Treaval(ch[x][1]); } } //以上Debug //以下是题目的特定函数: inline void NewNode(int &x,int c) { x = ++top1; ch[x][0] = ch[x][1] = pre[x] = 0; sz[x] = 1; val[x] = sum[x] = c;/*这是题目特定函数*/ add[x] = 0; } //把延迟标记推到孩子 类似线段树 inline void push_down(int x) /*这是题目特定函数*/ { if(add[x]) { val[x] += add[x]; add[ ch[x][0] ] += add[x]; add[ ch[x][1] ] += add[x]; sum[ ch[x][0] ] += (LL)sz[ ch[x][0] ] * add[x]; sum[ ch[x][1] ] += (LL)sz[ ch[x][1] ] * add[x]; add[x] = 0; } } //把孩子状态更新上来 inline void push_up(int x) { sz[x] = 1 + sz[ ch[x][0] ] + sz[ ch[x][1] ]; /*这是题目特定函数*/ sum[x] = add[x] + val[x] + sum[ ch[x][0] ] + sum[ ch[x][1] ]; } /*初始化*/ inline void makeTree(int &x,int l,int r,int f) { if(l > r) return ; int m = (l + r)>>1; NewNode(x, num[m]); /*num[m]权值改成题目所需的*/ makeTree(ch[x][0], l, m - 1, x); makeTree(ch[x][1], m + 1, r, x); pre[x] = f; push_up(x); } inline void init(int n) /*这是题目特定函数*/ { ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; add[0] = sum[0] = 0; root = top1 = 0; //为了方便处理边界,加两个边界顶点 NewNode(root, 0); NewNode(ch[root][1], 0); pre[top1] = root; sz[root] = 2; for (int i = 0 ; i < n ; i ++) scanf("%d",&num[i]); makeTree(keyTree, 0, n-1, ch[root][1]); push_up(ch[root][1]); push_up(root); } inline void update( ) /*这是题目特定函数*/ { int l, r, c; scanf("%d%d%d",&l,&r,&c); RotateTo(l-1,0); RotateTo(r+1,root); add[keyTree] += c; //sz当前节点儿子的数量 sum[keyTree] += (LL)c * sz[ keyTree ]; } inline void query() /*这是题目特定函数*/ { int l, r; scanf("%d%d",&l,&r); RotateTo(l-1, 0); RotateTo(r+1, root); printf("%lld\n",sum[keyTree]); } } spt; int main() { int n,m; scanf("%d%d",&n,&m); spt.init(n); //spt.debug(); while(m--) { char op[5]; scanf("%s",op); if(op[0] == 'Q') { spt.query(); } else { spt.update(); //spt.debug(); } } return 0; }
- 版权声明:本文基于《知识共享署名-相同方式共享 3.0 中国大陆许可协议》发布,转载请遵循本协议
- 文章链接:http://www.carlstedt.cn/archives/1180 (转载时请注明本文出处及文章链接)
发表评论
快来吐槽一下吧!