线段树入门题,换成splay tree 来搞搞。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std; #define MAXN 200100 long long Add[MAXN];//延迟标记 struct Splay_Tree
{
int cnt, rt;//cnt为节点数,rt == root struct Tree{
long long K;
long long sumk;
int key;//关键字
int num, size;//num是这个节点有多少重复,size是以这个节点为根的子树大小。
int fa, son[];
}T[MAXN]; inline void init()
{
cnt = ;//初始化超级根节点(标记为0的节点)
T[].size = ;
T[].sumk = ;
T[].K = ;
rt = ;
memset(Add,,sizeof(Add));
}
inline void PushUp(int x)
{
T[x].size=T[T[x].son[]].size+T[T[x].son[]].size+T[x].num;
T[x].sumk=T[T[x].son[]].sumk+T[T[x].son[]].sumk+T[x].K;
} inline void PushDown(int x)
{
if(Add[x])
{
if(T[x].son[])//
{
T[T[x].son[]].K += Add[x];
T[T[x].son[]].sumk += T[T[x].son[]].size*Add[x];
Add[T[x].son[]]+=Add[x];
}
if(T[x].son[])
{
T[T[x].son[]].K+=Add[x];
T[T[x].son[]].sumk += T[T[x].son[]].size*Add[x];
Add[T[x].son[]]+=Add[x];
}
Add[x]=;//不管子节点有没有,这层一定往下推,没有子节点相当于标记无效。
}
} inline int Newnode(int key, int fa,int K) //新建一个节点并返回
{
++cnt;
T[cnt].K = T[cnt].sumk = K;
T[cnt].key=key;
T[cnt].num=T[cnt].size=;
T[cnt].fa=fa;
T[cnt].son[]=T[cnt].son[]=;
return cnt;
} inline void Rotate(int x, int p) //0左旋 1右旋
{
int y=T[x].fa;
PushDown(y);//这里是不是必须的,我感觉从小往上不需要往上推
PushDown(x);
T[y].son[!p]=T[x].son[p];
T[T[x].son[p]].fa=y;
T[x].fa=T[y].fa;
if(T[x].fa)
T[T[x].fa].son[T[T[x].fa].son[] == y]=x;
T[x].son[p]=y;
T[y].fa=x;
PushUp(y);
PushUp(x);
} void Splay(int x, int To) //将x节点移动到To的子节点中
{
while(T[x].fa != To)
{
if(T[T[x].fa].fa == To)
Rotate(x, T[T[x].fa].son[] == x);
else
{
int y=T[x].fa, z=T[y].fa;
int p=(T[z].son[] == y);
if(T[y].son[p] == x)
Rotate(x, !p), Rotate(x, p); //之字旋
else
Rotate(y, p), Rotate(x, p); //一字旋
}
}
if(To == ) rt=x;
} int GetPth(int p, int To) //返回第p小的节点 并移动到To的子节点中
{
if(!rt || p > T[rt].size) return ;
int x=rt;
while(x)
{
PushDown(x);
if(p >= T[T[x].son[]].size+ && p <= T[T[x].son[]].size+T[x].num)
break;
if(p > T[T[x].son[]].size+T[x].num)
{
p-=T[T[x].son[]].size+T[x].num;
x=T[x].son[];
}
else
x=T[x].son[];
}
Splay(x, );
return x;
} int Find(int key) //返回值为key的节点 若无返回0 若有将其转移到根处
{
if(!rt) return ;
int x=rt;
while(x)
{
PushDown(x);
if(T[x].key == key) break;
x=T[x].son[key > T[x].key];
}
if(x) Splay(x, );
return x;
} int Prev() //返回根节点的前驱 非重点
{
if(!rt || !T[rt].son[]) return ;
int x=T[rt].son[];
while(T[x].son[])
{
PushDown(x);
x=T[x].son[];
}
Splay(x, );
return x;
} int next() //返回根结点的后继 非重点
{
if(!rt || !T[rt].son[]) return ;
int x=T[rt].son[];
while(T[x].son[])
{
PushDown(x);
x=T[x].son[];
}
Splay(x, );
return x;
} void Insert(int key,int K) //插入key值
{
if(!rt)
rt=Newnode(key, , K);
else
{
int x=rt, y=;
while(x)
{
PushDown(x);
y=x;
if(T[x].key == key)
{
T[x].num++;
T[x].size++;
break;
}
T[x].size++;//既然一定调整
x=T[x].son[key > T[x].key];
}
if(!x)
x = T[y].son[key > T[y].key] = Newnode(key, y, K);
Splay(x, );
}
} void Delete(int key) //删除值为key的节点1个
{
int x=Find(key);
if(!x) return;
if(T[x].num>)
{
T[x].num--;
PushUp(x);
return;
}
int y=T[x].son[];
while(T[y].son[])
y=T[y].son[];
int z=T[x].son[];
while(T[z].son[])
z=T[z].son[];
if(!y && !z)
{
rt=;
return;
}
if(!y)
{
Splay(z, );
T[z].son[]=;
PushUp(z);
return;
}
if(!z)
{
Splay(y, );
T[y].son[]=;
PushUp(y);
return;
}
Splay(y, );
Splay(z, y);
T[z].son[]=;
PushUp(z);
PushUp(y);
} int GetRank(int key) //获得值<=key的节点个数 并将其转移到根处 若<key只需将<=换为<
{
if(!rt) return ;
int x=rt, ret=, y=;
while(x)
{
y=x;
if(T[x].key <= key)
{
ret += T[T[x].son[]].size + T[x].num;
x=T[x].son[];
}
else
x=T[x].son[];
}
Splay(y, );
return ret;
} // 这个删除太丑了
// void Delete(int l, int r) //删除值在[l, r]中的所有节点 l!=r
// {
// if(!Find(l)) Insert(l);// 你这样写真的好吗? 泥煤
// int p=Prev();
// if(!Find(r)) Insert(r);
// int q=next();
// if(!p && !q)
// {
// rt=0;
// return;
// }
// if(!p)
// {
// T[rt].son[0]=0;
// PushUp(rt);
// return;
// }
// if(!q)
// {
// Splay(p, 0);
// T[rt].son[1]=0;
// PushUp(rt);
// return;
// }
// Splay(p, q);
// T[p].son[1]=0;
// PushUp(p);
// PushUp(q);
// }
}spt; int main() {
int n,q;
scanf("%d%d",&n,&q);
spt.init();
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
spt.Insert(i, tmp);
}
for(int i=;i<q;i++)
{
getchar();
char sign;
scanf("%c",&sign);
long long ans=;
if(sign == 'Q')
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
{
int id = spt.Find(a);
// mark
ans = spt.T[id].K;
//cout<<spt.T[id].K<<endl;
}
else
{
int ida,idb;
idb = spt.Find(b);//在这题,因为点没有删除,所以点的标号和splaytree中标号一致。
ida = spt.Find(a);//但是有lazy标记,所以还是得找一遍,可以把标记往下推。
spt.Splay(idb, ida);
int idson = spt.T[idb].son[];
//spt.PushDown(idb);
ans = spt.T[idb].K + spt.T[ida].K + spt.T[idson].sumk;
}
printf("%I64d\n",ans);
//cout<<ans<<endl;
}
else
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c); if(a==b)
{
int id = spt.Find(a);
spt.T[id].K += c;
spt.T[id].sumk += c;
// 这个节点的值改变了,还是得往上推
spt.Splay(id, );
}
else
{
int ida,idb;
idb = spt.Find(b);
ida = spt.Find(a); spt.T[ida].K += c;
//spt.T[ida].sumk += c;
spt.T[idb].K += c;
//spt.T[idb].sumk += c; spt.Splay(idb, ida); int idson = spt.T[idb].son[]; if(idson != )
{
spt.T[ idson ].sumk += spt.T[ idson ].size*c;
spt.T[ idson ].K += c;
Add[idson] += c;// 当全局变量来用
spt.Splay(idson, );//还得往上推一下吧
} } }
}
return ;
}

splay tree成段更新,成段查询poj3466的更多相关文章

  1. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  2. 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst

    题目大意: 2个操作 A.区间a b 增加 c B 查询a b; 注意事项:1.记住要清除标记 2.查询时要下放标记,但没必要向上更新 线段:自带的,不用建模 区间和性质:sum: /* WA 1次 ...

  3. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  4. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

  5. POJ2155 Matrix 【二维树状数组】+【段更新点查询】

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Descripti ...

  6. hdu1754(splay tree 单点更新,成段查询)

    题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...

  7. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

  8. [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和.昨晚思前想后找有没有一个数学上的 ...

  9. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

随机推荐

  1. xubuntu openocd nRF51822 download --- 2

    昨天非常晚的时候才最终发现事实上Unkown USB Device并非错误,仅仅是个警告而已,所以我们不关心就能够.让Makefile继续往下走就能够.于是我尝试mbs,s110.cload和firm ...

  2. NIO之Charset类字符编码对象

    Charset类字符编码对象 介绍 java中使用Charset来表示编码对象 This class defines methods for creating decoders and encoder ...

  3. Intellj IDEA14.0.2启动异常之3分钟修复

    今天是周一,刚到公司启动心爱的IDEA,,突然启动到一半,就抛异常了,直接弹窗,报例如以下的异常: java.lang.RuntimeException: com.intellij.ide.plugi ...

  4. atitit.RandomAccessFile rws rwd 的区别于联系

    atitit.RandomAccessFile rws rwd 的区别于联系 1. Rw rws  rwd1 2. "rws" 模式1 3. rwd"模式2 1. Rw  ...

  5. Atitit.java jna 调用c++ dll 的总结

    Atitit.java jna 调用c++ dll 的总结 1. JNA技术解密1 1.1. JNA工作原理1 2. JNA技术难点 Java—C和操作系统数据类型的对应表1 2.1. 1 2.2.  ...

  6. Angularjs学习笔记8_directive2

    指令难点在于参数 angular.module('app', []) .directive('myDirective', function() { return { restrict: String, ...

  7. volatile关键字比较好的解释

    http://www.tuicool.com/articles/IRvUJbN http://tengj.top/2016/05/06/threadvolatile4/?utm_source=tuic ...

  8. C# - Passing Reference-Type Parameters

    A variable of a reference type does not contain its data directly; it contains a reference to its da ...

  9. Yii简单使用阿里云短信教程!

    首先我们下载官方完整包的SDK官方标明了啊,PHP版本一定要不低于5.5下载后目录如下: 我们只需要将目录里的api_sdk复制出来到Yii项目的根目录的common模块下面的extensions文件 ...

  10. Git使用技巧(2)-- 基本操作

    常用 Git 命令清单 作者: 阮一峰 编辑更新:shifu204 日期: 2016年9月 1日 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练 ...