Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)
来源:互联网 发布:ubuntu怎么新建文件夹 编辑:程序博客网 时间:2024/05/21 05:42
题目大意:
给出很多条分布在 x 轴上的线段。
然后给出很多点集,问这些点集分布在多少条不同的线段上。
思路分析:
把点集分散成若干条线段。
如果点集做出的线段包含了某一条给出的线段的话,也就是说这个点集上不会有点在这条线段上。
所以我们就是求出 点集做出的线段包含了多少个给出的线段就可以了。
那么也就是比较l r的大小,排序之后用BIT
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define lowbit(x) (x&(-x))#define maxn 1000005using namespace std;struct node{ int l,r,p; bool operator < (const node &cmp)const { if(l!=cmp.l)return l>cmp.l; if(r!=cmp.r)return r<cmp.r; return p<cmp.p; }} line[maxn];int bit[maxn],ans[maxn],n,m,S=1e6+1;int sum(int x){ int res=0; for(;x;x-=lowbit(x))res+=bit[x]; return res;}int add(int x){ for(;x<=S;x+=lowbit(x))bit[x]++;}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d%d",&line[i].l,&line[i].r); int T=n,cnt,p,x; for(int i=1;i<=m;i++) { ans[i]=n; scanf("%d%d",&cnt,&p); if(p>1)line[++T].l=1,line[T].r=p-1,line[T].p=i; for(;--cnt;p=x) { scanf("%d",&x); if(x>p+1)line[++T].l=p+1,line[T].r=x-1,line[T].p=i; } line[++T].l=p+1,line[T].r=S,line[T].p=i; } sort(line+1,line+1+T); for(int i=1;i<=T;i++) if(line[i].p) ans[line[i].p]-=sum(line[i].r); else add(line[i].r); for(int i=1;i<=m;i++)printf("%d\n",ans[i]); return 0;}
0 0
- Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)
- Codeforces Round #216 (Div. 2) E. Valera and Queries (树状数组)
- Codeforces Round #216_div2_E.Valera and Queries
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round #216 (Div. 2) C. Valera and Elections
- Codeforces Round #216 (Div. 2) D. Valera and Fools
- Codeforces Round #216 (Div. 2) D. Valera and Fools
- Codeforces Round #216 (Div. 2) A. Valera and Plates
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round #216 (Div. 2)---C. Valera and Elections
- Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
- Codeforces Round #252 (Div. 2) C. Valera and Tubes
- Codeforces Round #252 (Div. 2) 441C. Valera and Tubes
- Codeforces Round #252 (Div. 2)441D. Valera and Swaps
- Codeforces Round #252 (Div. 2)A. Valera and Antique Items
- Codeforces Round #252 (Div. 2)B. Valera and Fruits
- Codeforces Round #252 (Div. 2)-B. Valera and Fruits
- Codeforces Round #252 (Div. 2) A. Valera and Antique Items
- android 将对象存储于本地(内存)
- eclipse在已创建好的目录下创建新项目
- [LeetCode] Reverse Integer
- 唯一的游戏世界
- google protocl buffer 序列化和反序列化的一个例子
- Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)
- 经纬财富:德兴白银午后操作建议
- ios7,使用boundingRectWithSize方法计算文本正确的size
- 导航栈逻辑上的错误与单例方法
- Java IO流读写文件的几个注意点
- JS-URI处理
- 【struts2重定向】中文乱码解决方案
- 403格子取数问题
- 深入理解Redis主键失效原理及实现机制