3年没写线段树题了,今天帮小学弟水了棵线段树,想不到现在依然有看到Accepted的鸡冻哈哈哈
来源:互联网 发布:西门子s7200软件下载 编辑:程序博客网 时间:2024/04/29 07:57
题目大意:给一个区间1~10^10,然后Q个操作有更新,有询问,Q的范围是1~10^5,明显是一个离散化+区间更新的线段树水题传送门,这里更新需要注意lay的思想,也就是延迟更新,具体就是push down和push up操作,具体看代码;
#include <cstdio>#include <algorithm>#include <iostream>using namespace std;const int MAX=204002;int a[MAX*2];int op[MAX][3];struct node{ int l,r,ok;}tree[MAX*8];void build(int pos,int l,int r) { tree[pos].l=l; tree[pos].r=r; tree[pos].ok = a[tree[pos].r-1] - a[tree[pos].l-1]+1; if(l==r) return; int mid=(l+r)>>1; build(pos*2,l,mid); build(pos*2+1,mid+1,r); tree[pos].ok = tree[pos*2].ok+ tree[pos*2+1].ok;}void update(int pos,int l,int r){ if(l<=tree[pos].l&&tree[pos].r<=r) { tree[pos].ok = 0; return; } if(tree[pos].ok==0) { //lay :push down tree[pos*2].ok = 0; tree[pos*2+1].ok = 0; } int mid=(tree[pos].l+tree[pos].r)>>1; if(r<=mid) update(pos*2,l,r); else if(l>mid) update(pos*2+1,l,r); else { update(pos*2,l,mid); update(pos*2+1,mid+1,r); } tree[pos].ok = tree[pos*2].ok + tree[pos*2+1].ok; //lay : push up}int query(int pos,int l,int r){ if(l<=tree[pos].l&&tree[pos].r<=r) { return tree[pos].ok; } int mid=(tree[pos].l+tree[pos].r)>>1; if(tree[pos].ok==0) { tree[pos*2].ok = 0; tree[pos*2+1].ok = 0; } if(r<=mid) return query(pos*2,l,r); else if(l>mid) return query(pos*2+1,l,r); else { return query(pos*2,l,mid)+ query(pos*2+1,mid+1,r); }}int main(){ int N,Q; cin>>N>>Q; int index = 0; for(int i=0;i<Q;i++) { scanf("%d%d%d",&op[i][0],&op[i][1],&op[i][2]); a[index++]=op[i][1]; a[index++]=op[i][2]; } //两次离散化,防止有相邻的数据 sort(a,a+index); int n=unique(a,a+index)-a; int t = 0; for(int i=1;i<n;i++){ if(a[i]-a[i-1]!=1){ a[n+t] = a[i-1]+1; t++; } } sort(a,a+n+t); n=unique(a,a+n+t)-a; build(1,1,n); for(int i=0;i<Q;i++) { if(op[i][0]==1) { update(1, lower_bound(a,a+n,op[i][1])-a+1 , lower_bound(a,a+n,op[i][2])-a+1 ); }else { printf("%d\n",query(1, lower_bound(a,a+n,op[i][1])-a+1, lower_bound(a,a+n,op[i][2])-a+1 )); } } return 0;}
0 0
- 3年没写线段树题了,今天帮小学弟水了棵线段树,想不到现在依然有看到Accepted的鸡冻哈哈哈
- 【线段树】今天我种了一棵线段树
- 哈哈哈!!有BLOG了
- 啊好久没写了- -。。。 现在又开始系统的刷题了,所以还是写写吧- - 首先附上第一道线段树模板题- -。。。
- 哈哈哈,我有新的帐号了
- 哈哈哈,今天开博了
- 今天开博客了,哈哈哈!
- 悲剧,我的线段树有一个地方一直写繁了!!!
- 今天看到了车祸
- 二叉树二叉树虐我千万遍劳资今天终于搞明白了哈哈哈哈哈
- 线段树的简单应用;火车订票;线段树用起来太灵活了!;
- UVALive 3938 一道被我WA了的线段树
- 炸飞我了的线段树练习题
- 数据结构之看了也不会懂的>>线段树<<
- BZOJ 3339: Rmq Problem 穿了棉袄的线段树
- 线段树什么的最讨厌了
- HDU1754 动态线段树,超内存了~
- 线段树插队。。题目有点忘了、、
- SDAU 练习三 1013 小牛生孩子问题
- 面试题4
- C++ primer读书笔记 第十章 关联容器
- Generate Parentheses
- activemq+spring 持久化发送消息
- 3年没写线段树题了,今天帮小学弟水了棵线段树,想不到现在依然有看到Accepted的鸡冻哈哈哈
- 10个有关RESTful API良好设计的最佳实践
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- 一道csf认证题”最大的矩形“
- POJ 2965-The Pilots Brothers' refrigerator(枚举&&DFS&&输出过程)
- C语言常用的代码组织形式--多文件
- Linux-内存管理-待补充
- android自定义view之地图(一)
- C++拷贝构造函数的参数为什么必须使用引用类型(无限递归)