CF#The Union of k-Segments -离散化+树状数组
来源:互联网 发布:windows商店 下载 编辑:程序博客网 时间:2024/05/17 01:00
真是写得太挫了。。
离散化+树状数组
3S。。。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>#include <stack>#include <vector>#include <iostream>using namespace std;struct node{ int l,r; node(){} node(int a,int b){l=a,r=b;}};const int N=1000005*4; int n,k;node tm[1000005];int cmp(node a,node b){ return a.l<b.l;}map <int ,int > to_num;map <int ,int > to_val;int tmp_num[N];int tree[N];int lowbit(int x){ return x&-x;}void add(int x,int value){ for (int i=x;i<=4000000;i=i+lowbit(i)) { tree[i]+=value; }}int get(int x){ int sum=0; for (int i=x;i;i-=lowbit(i)) { sum+=tree[i]; } return sum;}int pos[N];int main(){ int i; cin>>n>>k; int ok=0; for (i=1;i<=n;i++) { scanf("%d%d",&tm[i].l,&tm[i].r); tmp_num[ok++]=tm[i].l; tmp_num[ok++]=tm[i].r; } sort(tmp_num,tmp_num+ok); int len=0; for (i=0;i<ok;i++) { if (i==0) { tmp_num[len++]=tmp_num[i]; } else if (tmp_num[i]==tmp_num[i-1]) continue; else tmp_num[len++]=tmp_num[i]; } int tol=1; for (i=0;i<len;i++) { to_num[tmp_num[i]]=tol; to_val[tol]=tmp_num[i]; tol+=2; } for (i=1;i<=n;i++) { int tmpx=to_num[tm[i].l]; int tmpy=to_num[tm[i].r]; add(tmpx,1); add(tmpy+1,-1); } for (i=1;i<=4000000;i++) { int x=get(i); if (x>=k) pos[i]=1; } int sum=0; int last; int line=1; for (i=1;i<=4000000+2;i++) { if (pos[i]==1&&line) last=i,line=0; if (pos[i]==1) continue; if (pos[i]==0&&pos[i-1]==1) { tm[++sum]=node(to_val[last],to_val[i-1]); last=i;line=1; } } printf("%d\n",sum); for (i=1;i<=sum;i++) { printf("%d %d\n",tm[i].l,tm[i].r); } return 0; }
0 0
- CF#The Union of k-Segments -离散化+树状数组
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- 排序 Codeforces612D The Union of k-Segments
- The Union of k-Segments CodeForces
- The Union of k-Segments CodeForces
- codeforces D. The Union of k-Segments 排序
- Codeforces 612D The Union of k-Segments 【思维】
- Codeforces 612D The Union of k-Segments
- CodeForces 612 D. The Union of k-Segments(水~)
- codeforces 612D The Union of k-Segments (sorting)
- codeforces_612D. The Union of k-Segments(排序)
- Educational Codeforces Round 4 D. The Union of k-Segments
- CodeForces 612D The Union of k-Segments
- CodeForces 612D - The Union of k-Segments(模拟)
- Educational Codeforces Round 4-D. The Union of k-Segments
- D. The Union of k-Segments----扫描线初步
- Codeforces 612D The Union of k-Segments
- 深度学习:推动NLP领域发展的新引擎
- JQuery判断checkbox是否被选中
- K近邻快速算法 -- KD树、BBF改进算法
- 使用makefile编译C语言文件遇到的问题
- 实战中的 Promise 和 Future
- CF#The Union of k-Segments -离散化+树状数组
- K660E I7 D4 clover 引导黑苹果完美驱动
- WINXP打开事件查看器
- Easyui弹出框
- HttpClient用HttpPost传输中文字符串乱码
- makefile简介及其简单编写示例
- iOS轮播图广告图
- C语言调用库文件后在Linux终端编译的方法
- 南阳oj 素数求和问题