题解 P3372 【【模板】线段树 1】(珂朵莉树解法)
这道题可以用珂朵莉树做,但是由于数据比较不随机,而我也没有手写一颗平衡树,所以就被卡掉了,只拿了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】(珂朵莉树解法)的更多相关文章
- [转]我的数据结构不可能这么可爱!——珂朵莉树(ODT)详解
参考资料: Chtholly Tree (珂朵莉树) (应某毒瘤要求,删除链接,需要者自行去Bilibili搜索) 毒瘤数据结构之珂朵莉树 在全是珂学家的珂谷,你却不知道珂朵莉树?来跟诗乃一起学习珂朵 ...
- 洛谷$P2572\ [SCOI2010]$ 序列操作 线段树/珂朵莉树
正解:线段树/珂朵莉树 解题报告: 传送门$w$ 本来是想写线段树的,,,然后神仙$tt$跟我港可以用珂朵莉所以决定顺便学下珂朵莉趴$QwQ$ 还是先写线段树做法$QwQ$? 操作一二三四都很$eas ...
- LOJ#557. 「Antileaf's Round」你这衣服租来的吗(FHQ Treap+珂朵莉树)
题面 传送门 题解 好吧我是不太会复杂度分析-- 我们对于每种颜色用一个数据结构维护(比方说线段树或者平衡树,代码里写的平衡树),那么区间询问很容易就可以解决了 所以现在的问题是区间修改,如果区间颜色 ...
- 洛谷AT2342 Train Service Planning(思维,动态规划,珂朵莉树)
洛谷题目传送门 神仙思维题还是要写点东西才好. 建立数学模型 这种很抽象的东西没有式子描述一下显然是下不了手的. 因为任何位置都以\(k\)为周期,所以我们只用关心一个周期,也就是以下数都在膜\(k\ ...
- 洛谷P2572 [SCOI2010]序列操作(珂朵莉树)
传送门 珂朵莉树是个吼东西啊 这题线段树代码4k起步……珂朵莉树只要2k…… 虽然因为这题数据不随机所以珂朵莉树的复杂度实际上是错的…… 然而能过就行对不对…… (不过要是到时候noip我还真不敢打… ...
- CF915E Physical Education Lessons(珂朵莉树)
中文题面 据说正解是动态开点线段树而且标记也不难下传的样子 然而这种区间推平的题目还是喜欢写珂朵莉树啊……码量小…… 虽然真要构造的话随便卡…… //minamoto #include<cstd ...
- CF915E Physical Education Lessons 珂朵莉树
问题描述 CF915E LG-CF915E 题解 \(n \le 10^9\) 看上去非常唬人. 但是这种区间操作的题,珂朵莉树随便跑啊. \(\mathrm{Code}\) #include< ...
- LG4979 矿洞:坍塌 珂朵莉树
问题描述 LG4979 题解 珂朵莉树+O2简直就是绝配 对于操作 A ,直接 \(\mathrm{assign}\) 推平就完事了. 对于操作 B ,如果它左右端点有在边界上的,直接把区间 \([l ...
- 『珂朵莉树 Old Driver Tree』
珂朵莉树 珂朵莉树其实不是树,只是一个借助平衡树实现的数据结构,主要是对于有区间赋值的数据结构题,可以用很暴力的代码很高效地完成任务,当然这是建立在数据随机的基础上的. 即使数据不是随机的,写一个珂朵 ...
随机推荐
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16425 Accepted: 5797 Descr ...
- angular 报错笔记
1.错误信息: Failed to instantiate module app due to: Error: [$injector:unpr] http://errors.angularjs.org ...
- wordpress 后台登录增加访问效验
目前已知的增加 wordpress 后台登录安全的方案有三种: 安全插件:如Limit Login Attempts Reloaded.WPS Hide Login 等等: 登录 URL 增加自定义k ...
- 注解@SuppressWarnings
在JAVA中注解@SuppressWarnings("deprecation")的Deprecation是什么意思 过期的 @SuppressWarnings("depr ...
- hdu5321 beautiful set(莫比乌斯反演)
设\(cnt[i]\)为权值为i的倍数的数的数量. \(f0[i],f1[i]\)分别为两种方法\(gcd=i\)的贡献是i的多少倍. \(F0[i],F1[i]\)分别为两种方法\(gcd\)为\( ...
- c++常见操作的模板
1.统计时间 #include<ctime> clock_t startTime = clock(); code(); clock_t endTime = clock(); cout &l ...
- CAD二次开发(01)-绘制直线
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Eclipse集成Git插件及使用
目录(?)[+] 1 Git插件安装 11 下载插件 2 Git提交代码 21 建git的本地仓库 第一种 第二种 项目创建本地仓库后 如图 22 提交本地仓库 23 提交远程仓库 3 Git下载代码 ...
- JAVA关于byte数组与String转换的问题
1 public class ToString{ public static void main(String[] args){ String aa = "hellow"; byt ...
- Qt之QCryptographicHash
简述 QCryptographicHash类提供了生成密码散列的方法.该类可以用于生成二进制或文本数据的加密散列值.目前支持MD4.MD5.SHA-1.SHA-224.SHA-256.SHA-384和 ...