这道题可以用珂朵莉树做,但是由于数据比较不随机,而我也没有手写一颗平衡树,所以就被卡掉了,只拿了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. Concurrency pattern

    In software engineering, concurrency patterns are those types of design patterns that deal with the  ...

  2. OSI概述问答

    1.    网络中体系结构的七层.四层.五层是怎么回事? OSI(Open System Interconnection)开放系统互连参考模型的七层协议体系结构:概念清楚,理论比较完整,但既复杂又不用 ...

  3. 2017-2-10 bash基础脚本

    练习:写一脚本,实现如下功能: 1.让用户通过键盘输入一个用户名,如果用户不存在就退出: 2.如果其UID等于其GID,就说它是个"good guy" 3.否则,就说它是个“bad ...

  4. Pytorch搭建简单神经网络 Task2

    1>建立数据集(并绘制图像) # -*- coding: utf-8 -*- #demo.py import torch import torch.nn.functional as F # 主要 ...

  5. HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?

    题目链接:https://cn.vjudge.net/problem/HDU-1257 题意 中文题咯中文题咯 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然 ...

  6. php7 memcache和memcached.so扩展

    php7安装memcache和memcached扩展 https://github.com/websupport-sk/pecl-memcache https://github.com/php-mem ...

  7. ProGuard常见问题及解决套路

    ProGuard是一个压缩.优化和混淆Java字节码的工具,非常好用.本篇文章总结一下许多人在使用ProGuard时经常遇到的问题. 我把在使用ProGuard时经常遇到的问题分为两类,分别是导致构建 ...

  8. numpy学习笔记 - numpy数组的常见用法

    # -*- coding: utf-8 -*- """ 主要记录代码,相关说明采用注释形势,供日常总结.查阅使用,不定时更新. Created on Mon Aug 20 ...

  9. ZOJ 2702 Unrhymable Rhymes

    Unrhymable Rhymes Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu De ...

  10. 洛谷 P2730 魔板 Magic Squares

    P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...