这道题可以用珂朵莉树做,但是由于数据比较不随机,而我也没有手写一颗平衡树,所以就被卡掉了,只拿了70分


珂朵莉树是一种基于平衡树的(伪)高效数据结构。

它的核心操作是推平一段区间。

简而言之,就是把之前的零零碎碎的都干掉,用一个美而饱满的大区间取代。

然后我们更新操作和查询操作就暴力遍历一遍,统计一下和就可以了。


Split操作

 inline set<node>::iterator split(int pos){
set<node>::iterator it=s.lower_bound(node(pos));
if(it!=s.end()&&it->l==pos)return it;
--it;
int L=it->l,R=it->r;long long V=it->v;
s.erase(it),s.insert(node(L,pos-,V));
return s.insert(node(pos,R,V)).first;
}

split操作就是获得区间的迭代器。

所以我们要先找到pos在哪里(也就是it)。

然后把它拆掉,再合在一起。

在这个过程中我们就可以拿到迭代器。(干什么等会说)


Update操作

 inline void update(int l,int r,long long val=){
set<node>::iterator itl=split(l),itr=split(r+);
for(;itl!=itr;++itl)itl->v+=val;
}

现在刚才的split就派上用场了。

我们现在相当于是获得了区间的两端(也是区间)。

然后暴力遍历一下,给每个区间都打一个标记。


Query操作

 inline long long query(int l,int r){
set<node>::iterator itl=split(l),itr=split(r+);
long long ans=;
for(;itl!=itr;++itl)ans+=(itl->r-itl->l+)*itl->v;
return ans;
}

收集一下标记加在一起就可以了,思路和update一样。


70分代码如下:

3741ms 2368kb

 #include<bits/stdc++.h>

 using namespace std;

 namespace StandardIO{

     template<typename T>inline void read(T& x){
x=;T f=;char c=getchar();
for(;c<''||c>'';c=getchar())if(c=='-')f=-;
for(;c>=''&&c<='';c=getchar())x=x*+c-'';
x*=f;
} template<typename T>inline void write(T x){
if(x<)putchar('-'),x*=-;
if(x>=)write(x/);
putchar(x%+'');
} } using namespace StandardIO; namespace ChthollyTree{ struct Tree{
private:
struct node{
int l,r;mutable long long v;
node(int L,int R=-,long long V=):l(L),r(R),v(V){}
bool operator < (const node &o)const{
return l<o.l;
}
};
set<node>s; inline set<node>::iterator split(int pos){
set<node>::iterator it=s.lower_bound(node(pos));
if(it!=s.end()&&it->l==pos)return it;
--it;
int L=it->l,R=it->r;long long V=it->v;
s.erase(it),s.insert(node(L,pos-,V));
return s.insert(node(pos,R,V)).first;
} inline void update(int l,int r,long long val=){
set<node>::iterator itl=split(l),itr=split(r+);
for(;itl!=itr;++itl)itl->v+=val;
} inline long long query(int l,int r){
set<node>::iterator itl=split(l),itr=split(r+);
long long ans=;
for(;itl!=itr;++itl)ans+=(itl->r-itl->l+)*itl->v;
return ans;
} public:
Tree(){}
~Tree(){} inline void Init(int n){
s.insert(node(,n));
} inline void Update(int l,int r,long long val){
update(l,r,val);
} inline int Query(int l,int r){
return query(l,r);
}
}; } using namespace ChthollyTree; namespace Solve{ const int N=; int n,m;
int sum[N];
Tree ljz; inline void solve(){
read(n),read(m);
ljz.Init(n);
for(register int i=;i<=n;++i){
int tmp;read(tmp);
sum[i]=sum[i-]+tmp;
}
while(m--){
int op,x,y,z;
read(op),read(x),read(y);
if(op==){
read(z);
ljz.Update(x,y,z);
}else{
write(ljz.Query(x,y)+sum[y]-sum[x-]),putchar('\n');
}
}
} } using namespace Solve; int main(){
solve();
}

题解 P3372 【【模板】线段树 1】(珂朵莉树解法)的更多相关文章

  1. [转]我的数据结构不可能这么可爱!——珂朵莉树(ODT)详解

    参考资料: Chtholly Tree (珂朵莉树) (应某毒瘤要求,删除链接,需要者自行去Bilibili搜索) 毒瘤数据结构之珂朵莉树 在全是珂学家的珂谷,你却不知道珂朵莉树?来跟诗乃一起学习珂朵 ...

  2. 洛谷$P2572\ [SCOI2010]$ 序列操作 线段树/珂朵莉树

    正解:线段树/珂朵莉树 解题报告: 传送门$w$ 本来是想写线段树的,,,然后神仙$tt$跟我港可以用珂朵莉所以决定顺便学下珂朵莉趴$QwQ$ 还是先写线段树做法$QwQ$? 操作一二三四都很$eas ...

  3. LOJ#557. 「Antileaf's Round」你这衣服租来的吗(FHQ Treap+珂朵莉树)

    题面 传送门 题解 好吧我是不太会复杂度分析-- 我们对于每种颜色用一个数据结构维护(比方说线段树或者平衡树,代码里写的平衡树),那么区间询问很容易就可以解决了 所以现在的问题是区间修改,如果区间颜色 ...

  4. 洛谷AT2342 Train Service Planning(思维,动态规划,珂朵莉树)

    洛谷题目传送门 神仙思维题还是要写点东西才好. 建立数学模型 这种很抽象的东西没有式子描述一下显然是下不了手的. 因为任何位置都以\(k\)为周期,所以我们只用关心一个周期,也就是以下数都在膜\(k\ ...

  5. 洛谷P2572 [SCOI2010]序列操作(珂朵莉树)

    传送门 珂朵莉树是个吼东西啊 这题线段树代码4k起步……珂朵莉树只要2k…… 虽然因为这题数据不随机所以珂朵莉树的复杂度实际上是错的…… 然而能过就行对不对…… (不过要是到时候noip我还真不敢打… ...

  6. CF915E Physical Education Lessons(珂朵莉树)

    中文题面 据说正解是动态开点线段树而且标记也不难下传的样子 然而这种区间推平的题目还是喜欢写珂朵莉树啊……码量小…… 虽然真要构造的话随便卡…… //minamoto #include<cstd ...

  7. CF915E Physical Education Lessons 珂朵莉树

    问题描述 CF915E LG-CF915E 题解 \(n \le 10^9\) 看上去非常唬人. 但是这种区间操作的题,珂朵莉树随便跑啊. \(\mathrm{Code}\) #include< ...

  8. LG4979 矿洞:坍塌 珂朵莉树

    问题描述 LG4979 题解 珂朵莉树+O2简直就是绝配 对于操作 A ,直接 \(\mathrm{assign}\) 推平就完事了. 对于操作 B ,如果它左右端点有在边界上的,直接把区间 \([l ...

  9. 『珂朵莉树 Old Driver Tree』

    珂朵莉树 珂朵莉树其实不是树,只是一个借助平衡树实现的数据结构,主要是对于有区间赋值的数据结构题,可以用很暴力的代码很高效地完成任务,当然这是建立在数据随机的基础上的. 即使数据不是随机的,写一个珂朵 ...

随机推荐

  1. 滴滴云安装mysql数据库

    Linux CentOS安装配置MySQL数据库   没什么好说的,直接正面刚吧. 安装mysql数据库 a)下载mysql源安装包:wget http://dev.mysql.com/get/mys ...

  2. python中*号用法总结

    python 中有很多地方用到星号,有时候会想知道这个*是干嘛用的,总结如下,有不当之处,还望不吝指出,谢谢.1.乘法: 在很多时候是用作乘法的,例如: In [90]: 2*7 Out[90]: 1 ...

  3. 【jQuery05】通过按键 来切换 class

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 微信小程序 分享海报

    const app = getApp(); const template = require('../../template/templates.js'); Page({ /** * 页面的初始数据 ...

  5. Java实现二叉树的创建、递归/非递归遍历

    近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...

  6. Linux下安装过程中编译PHP时报错:configure: error: libjpeg.(a|so) not found

    在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决的方法: 检查之后发现已经安装libjpeg.可是/usr/l ...

  7. 制作 Gif 工具

    ScreenToGif:非常小,非常强大: 从此可以十分方便地从视频中抠 gif 出来了: 以及制作一些教学类小 gif,插入到网页中: 丰富的编辑功能: 插入文本,插入标题,插入图像等: 下载地址: ...

  8. OWIN是Open Web Server Interface for .NET

    http://owin.org/ Servers and Hosts Katana Nowin Suave Frameworks Jasper Nancy SignalR WebApi WebShar ...

  9. struts2入门(搭建环境、配置、示例)

    转自:https://blog.csdn.net/u012862311/article/details/53412716 1.下载Struts2的jar包 下载地址:http://archive.ap ...

  10. A list is a sequence

    Like a string, a list is a sequence of values. In a string, the values are characters; in a list, th ...