板子题,正在努力看懂板子。。

http://blog.csdn.net/acm_cxlove/article/details/7815019

http://www.cnblogs.com/kuangbin/archive/2013/04/21/3034081.html

 #include <cstdio>
#include <cstring>
#include <algorithm> #define keyTree (ch[ ch[root][1] ][0])
#define LL long long using namespace std; const int maxn = ; struct SpalyTree{
int sz[maxn];
int ch[maxn][];
int pre[maxn];
int root,top1,top2;
int ss[maxn], que[maxn]; void Rotate(int x,int f)
{
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]][]==y ] = x;
ch[x][f] = y;
pre[y] = x;
push_up(y);
}
void Splay(int x,int goal)
{
push_down(x);
while(pre[x] != goal)
{
if(pre[pre[x] ] == goal)
{
Rotate(x,ch[pre[x] ][] == x);
}
else
{
int y = pre[x],z = pre[y];
int f = (ch[z][] == y);
if(ch[y][f] == x)
{
Rotate(x,!f),Rotate(x,f);
}
else
{
Rotate(y,f),Rotate(x,f);
}
}
}
push_up(x);
if(goal == ) root = x;
}
void RotateTo(int k,int goal)
{
int x = root;
push_down(x);
while(sz[ ch[x][] ] != k)
{
if(k < sz[ ch[x][] ])
{
x = ch[x][];
}
else
{
k -= (sz[ch[x][] ] + );
x = ch[x][];
}
push_down(x);
}
Splay(x,goal);
}
void erase(int x)
{
int father = pre[x];
int head = ,tail = ;
for(que[tail++] = x;head < tail;head++)
{
ss[top2++] = que[head];
if(ch[que[head] ][]) que[tail++] = ch[que[head] ][];
if(ch[que[head] ][]) que[tail++] = ch[que[head] ][];
}
ch[father ][ch[father][]==x ] = ;
push_up(father);
}
void debug(){printf("%d\n",root);Treaval(root);}
void Treaval(int x)
{
if(x)
{
Treaval(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][],ch[x][],pre[x],sz[x],val[x]);
Treaval(ch[x][]);
}
}
void NewNode(int &x,int c)
{
if(top2) x = ss[--top2];
else x = ++top1;
ch[x][] = ch[x][] = pre[x] = ;
sz[x] = ; val[x] = sum[x] = c;
add[x] = ;
}
void push_down(int x)
{
if(add[x])
{
val[x] += add[x];
add[ch[x][] ] += add[x];
add[ch[x][] ] += add[x];
sum[ch[x][] ] += (LL)sz[ch[x][] ]*add[x];
sum[ch[x][] ] += (LL)sz[ch[x][] ]*add[x];
add[x] = ;
}
}
void push_up(int x)
{
sz[x] = +sz[ch[x][] ] + sz[ch[x][] ];
sum[x] = add[x] + val[x] + sum[ch[x][] ] + sum[ch[x][] ];
}
void makeTree(int &x,int l,int r,int f)
{
if(l > r) return ;
int m = (l+r)>>;
NewNode(x,num[m]);
makeTree(ch[x][],l,m-,x);
makeTree(ch[x][],m+,r,x);
pre[x] = f;
push_up(x);
}
void init(int n)
{
ch[][] = ch[][] = pre[] = sz[] = ;
add[] = sum[] = ; root = top1 = ;
NewNode(root, -);
NewNode(ch[root][],-);
pre[top1] = root;
sz[root] = ; for(int i=;i<n;i++)
scanf("%d",&num[i]);
makeTree(keyTree,,n-,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
void update()
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
RotateTo(l-,);
RotateTo(r+,root);
add[keyTree] += c;
sum[keyTree] += (LL)c*sz[keyTree];
}
void query()
{
int l,r;
scanf("%d%d",&l,&r);
RotateTo(l-,);
RotateTo(r+,root);
printf("%lld\n",sum[keyTree]);
} int num[maxn];
int val[maxn];
int add[maxn];
LL sum[maxn];
}spt; int n,m; int main()
{
while(~scanf("%d%d",&n,&m))
{
spt.init(n);
char op[];
for(int i=;i<m;i++)
{
scanf("%s",op);
if(op[] == 'Q')
{
spt.query();
}
else
spt.update();
}
}
}

POJ3468/splay树/成段更新的更多相关文章

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

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

  2. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

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

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

  4. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  5. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  6. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  7. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  8. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  9. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

随机推荐

  1. 使用Thrift让Python和C#可以相互调用

    在聊如何使用Thrift让Python和C#可以互相调用之前,我们先来看看下面的话题. 一.什么是微服务.微服务的特征.诞生的背景.优势和不足 微服务:使用一套小服务来开发单个应用的方式,每个服务运行 ...

  2. nodejs源码编译-mipse64el架构

    下载nodejs,node-v6.1.0.tar.gz 链接: https://pan.baidu.com/s/1eCtNBWD5yaKiQIHp3pRKew 提取码: faun 注意对应版本的gcc ...

  3. 什么是CLOS架构?

    Clos架构,诞生于1952年,是由一位叫Charles Clos的人提出的,所以它并不是一个新的概念. 这个架构主要描述了一种多级电路交换网络的结构.Clos最大的优点就是对Crossbar结构的改 ...

  4. [loadrunner]通过检查点判定事务是否成功

    //检查点设置语句 //tmp在此时为临时参数  web_reg_find("SaveCount=tmp",   "Text=xxx",   LAST);   ...

  5. H5 video标签的第二种格式

    36-video标签的第二种格式 2.第二种格式存在的意义: 由于视频数据非常非常的重要, 所以五大浏览器厂商都不愿意支持别人的视频格式, 所以导致了没有一种视频格式是所有浏览器都支持的 这个时候W3 ...

  6. 堆排、python实现堆排

    一.堆-完全二叉树 堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),是不稳定排序 堆排序中的堆有大顶堆.小顶堆两种.他们都是完 ...

  7. react购物车demo

    import React, { Component } from 'react'; import './App.css'; import {connect} from 'react-redux'; i ...

  8. VS2015 + OPENCV + CUDA 安装流程

    VS2015  https://blog.csdn.net/guxiaonuan/article/details/73775519?locationNum=2&fps=1 OPENCV htt ...

  9. [转帖]NUMA

    作者:ibless 来源:CSDN 原文:https://blog.csdn.net/ibless/article/details/80114009 其实 很早之前对这一块有了解 比较多的的是 CCN ...

  10. 解决.Net Mvc跨域请求问题

    针对ASP.NET MVC和ASP.NET Web API两种项目类型 1.针对ASP.NET MVC,只需要在web.config中添加如下的内容即可 <system.webServer> ...