CDOJ 1325 卿学姐与基本法(离散化+线段树)
来源:互联网 发布:大数据新手视频 编辑:程序博客网 时间:2024/04/25 09:40
卿学姐与基本法
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
“做专题也要按照基本法”——蛤
离开了诡异的村庄,卿学姐来到了威廉·圣·乱七八糟王国,这里的国王咸鱼王是个智障。
国家涣散,盗贼四起,民不聊生。
见到这样的景象,卿学姐不禁潸然泪下,“悠悠苍天,奈何苦了苍生”。
自幼学习基本法的卿学姐决定向整个国家普及基本法,改善国家法度。
在这个国家总共有
由于整个国家的人实在是太多了,卿学姐每次只能对一个连续区间的编号的人普及基本法。
同时卿学姐还想知道在某个时刻某个区间还有多少人没有被普及基本法。
Input
第一行两个整数
接下来
Output
输出每个卿学姐想知道的答案
Sample input and output
5 31 1 21 4 52 2 4
1
Source
2016 UESTC Training for Data Structures
题解:看题目想到线段树,但是因为数据范围太大,显然不能直接使用线段树,所以这里应该加上离散化操作
代码如下:
#include <iostream>#include <bits/stdc++.h>using namespace std;const int maxn=100005;int c[2*maxn],kind[maxn],tree[8*maxn],lazy[8*maxn];int cl[maxn],cr[maxn];map<int, int> ys;void push_up(int rt){ tree[rt]=tree[rt<<1]+tree[rt<<1|1]; //cout<<rt<<' '<<tree[rt];}void push_down(int rt){ tree[rt<<1]=tree[rt<<1|1]=0; lazy[rt<<1]=lazy[rt<<1|1]=1; lazy[rt]=0;}void build(int l,int r,int rt){ if(l==r) { tree[rt]=c[l]-c[l-1]; //cout<<l<<' '<<tree[rt]<<endl; lazy[rt]=0; return; } int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); push_up(rt);}void update(int l,int r,int L,int R,int rt){ if(l>=L&&r<=R){ tree[rt]=0; lazy[rt]=1; //cout<<l<<' '<<r<<' '<<tree[rt]<<endl; return; } if(lazy[rt]) push_down(rt); int mid=(l+r)>>1; if(L<=mid) update(l,mid,L,R,rt<<1); if(R>=mid+1) update(mid+1,r,L,R,rt<<1|1); push_up(rt);}int query(int l,int r,int L,int R,int rt){ //cout<<l<<r<<endl; //cout<<tree[rt]<<endl; if(l>=L&&r<=R) return tree[rt]; if(lazy[rt]) push_down(rt); int mid=(l+r)>>1; int ans=0; if(L<=mid) ans+=query(l,mid,L,R,rt<<1); if(R>=mid+1) ans+=query(mid+1,r,L,R,rt<<1|1); push_up(rt); return ans;}int main(){ int n,q; cin>>n>>q; int cnt=0; int cc=0; int qq=1; for(int i=0;i<q;i++) { int kind1,l,r; scanf("%d %d %d",&kind1,&l,&r); c[++cnt]=l; c[++cnt]=r; cl[cc]=l; cr[cc]=r; kind[cc]=kind1; cc++; } sort(c+1,c+cnt); cnt=unique(c+1,c+cnt)-c-1; for(int i=cnt;i>0;i--) { if(c[i-1]+1!=c[i]) c[++cnt]=c[i-1]+1; //cout<<i<<' '<<c[i]<<endl; } sort(c+1,c+cnt+1); if(c[cnt]!=n) c[++cnt]=n; for(int i=1;i<=cnt;i++){ ys[c[i]]=i; } build(1,cnt,1); for(int i=0;i<q;i++) { //cout<<kind[i]<<' '<<ys[cl[i]]<<' '<<ys[cr[i]]<<endl; if(kind[i]==1) update(1,cnt,ys[cl[i]],ys[cr[i]],1); else if(kind[i]==2) { int ans=query(1,cnt,ys[cl[i]],ys[cr[i]],1); printf("%d\n",ans); } } //cout << "Hello world!" << endl; return 0;}
0 0
- CDOJ 1325 卿学姐与基本法(离散化+线段树)
- 【cdoj 1325】卿学姐与基本法 离散化+线段树
- CDOJ--卿学姐与基本法(线段树+离散化+区间查询)
- 2016 UESTC Training for Data Structures B - 卿学姐与基本法 CDOJ 1325 线段树+离散化
- CDOJ 1325 卿学姐与基本法 (离散化+区间修改)
- 离散化+线段树(CDOJ1325卿学姐与基本法)
- 卿学姐与基本法(线段树+区间更新)
- CDOJ 843 冰雪奇缘 (线段树+离散)
- CDOJ 1059 秋实大哥与小朋友(离散化)
- CDOJ 1335-求恰好k次覆盖矩形面积并- (线段树 扫描线 离散化)
- CDOJ 843 冰雪奇缘 (线段树+懒操作+离散化)
- 2016 UESTC Training for Data Structures F - 郭大侠与“有何贵干?” CDOJ 1335 线段树 扫描线 离散化
- CDOJ 1324 卿学姐与公主 (分块or线段树)
- cdoj 母仪天下(线段树)
- 线段树+离散化
- 离散化 + 线段树
- 线段树离散化
- 线段树离散化
- JavaEE 的EJB容器搭建的错误与解决方法
- 小五音乐
- LeetCode 2. Add Two Numbers
- 使用国内镜像库安装TensorFlow
- ASCII码排序
- CDOJ 1325 卿学姐与基本法(离散化+线段树)
- e舞成名模拟器下载(pc电脑版)
- 成为Java顶尖程序员 ,看这10本书就够了
- NP问题简介
- 12、centos6虚拟机简单安装redis-3.2.8
- 设计模式学习-代理模式
- 异步servlet之continuation
- OpenWrt dl
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable