统计,优化(Counting Intersections,HDU 5862)
来源:互联网 发布:淘宝化妆品小样靠谱点 编辑:程序博客网 时间:2024/06/05 08:27
类似的统计问题有很多,大概都是用某种循序枚举一个量,然后用数据结构(线段树或树状数组)或堆(set,map或优先队列)等来维护一些东西,然后快速计算出结果。
想起了很久以前没做出来的一道题目:http://blog.csdn.net/xl2015190026/article/details/52679699
具体就是横竖线分开考虑,对所有纵坐标离散化。
对横线按最左端升序排序。
对竖线也一样。
然后从左到右枚举竖线,对于一个竖线,把所有横跨过这条竖线的横线的纵坐标(已离散化)用树状数组维护,然后直接询问对应区间有几个即可。
维护的方式就是如果横线左端小于等于竖线,就加到树状数组里,直到右端小于竖线就删除。
具体就是用一个指针尽量往后扫,全部加到树状数组里,同时将其右端点放到优先队列里,然后通过优先队列来删除。
代码
#include<stdio.h>#include<vector>#include<algorithm>#include<queue>using namespace std;typedef long long ll;typedef pair<int,int> pii;const int maxn = 100010;/////////////////////////////树状数组int N;int BIT[maxn<<2];int low_bit(int x){ return x&-x;}void add(int p,int val){ while(p<=N) { BIT[p]+=val; p+=low_bit(p); }}int qry(int p){ int ret=0; while(p) { ret+=BIT[p]; p-=low_bit(p); } return ret;}/////////////////////////////int n;struct line{ pii a,b; int x,y; bool operator < (const line& rhs) const { return a.first<rhs.a.first; }}heng[maxn],shu[maxn];int ch,cs;vector<int>vec;ll ans;void read(){ scanf("%d",&n); ch=cs=0; vec.clear(); ans=0; pii a,b; for(int i=1;i<=n;i++) { scanf("%d %d",&a.first,&a.second); scanf("%d %d",&b.first,&b.second); if(a.second==b.second) { if(a.first>b.first) swap(a,b); ch++; heng[ch].a=a; heng[ch].b=b; vec.push_back(a.second); } else { if(a.second>b.second) swap(a,b); cs++; shu[cs].a=a; shu[cs].b=b; vec.push_back(a.second); vec.push_back(b.second); } } sort(vec.begin(),vec.end()); vec.resize(unique(vec.begin(),vec.end())-vec.begin()); for(int i=1;i<=ch;i++) heng[i].x=lower_bound(vec.begin(),vec.end(),heng[i].a.second)-vec.begin()+1; for(int i=1;i<=cs;i++) { shu[i].x=lower_bound(vec.begin(),vec.end(),shu[i].a.second)-vec.begin()+1; shu[i].y=lower_bound(vec.begin(),vec.end(),shu[i].b.second)-vec.begin()+1; } sort(heng+1,heng+1+ch); sort(shu+1,shu+1+cs); N=vec.size(); for(int i=1;i<=N;i++) BIT[i]=0;}void debug(){ //puts("---"); //puts("横"); //for(int i=1;i<=ch;i++) printf("%d %d %d %d\n",heng[i].a.second,heng[i].a.first,heng[i].b.first,heng[i].x); //puts("竖"); //for(int i=1;i<=cs;i++) printf("%d %d %d %d %d\n",shu[i].a.first,shu[i].a.second,shu[i].b.second,shu[i].x,shu[i].y); //puts("---");}void solve(){ read(); //debug(); int k=1; priority_queue<pii,vector<pii>,greater<pii> >q; for(int i=1;i<=cs;i++) { while(k<=ch&&heng[k].a.first<=shu[i].a.first) { add(heng[k].x,1); q.push(make_pair(heng[k].b.first,heng[k].x)); k++; } while(!q.empty()&&q.top().first<shu[i].a.first) { add(q.top().second,-1); q.pop(); } ans+=qry(shu[i].y)-qry(shu[i].x-1); } printf("%lld\n",ans);}int main(){ int T; scanf("%d",&T); while(T--) solve(); return 0;}
阅读全文
0 0
- 统计,优化(Counting Intersections,HDU 5862)
- HDU 5862 Counting Intersections
- HDU - 5862 Counting Intersections
- HDU 5862 Counting Intersections
- HDU 5862 Counting Intersections
- HDU 5862Counting Intersections (思维+树状数组)
- HDU 5862 Counting Intersections (扫描线+树状数组)
- HDU 5862 Counting Intersections(离散化+树状数组)
- HDU 5862 Counting Intersections(BIT+扫描线)
- HDU 5862 Counting Intersections 解题报告
- HDU 5862 Counting Intersections 扫描线
- HDU 5862 Counting Intersections(树状数组)
- Counting Intersections HDU
- HDU-5862-Counting Intersections(树状数组+离散化+扫描线)
- HDU 5862 Counting Intersections(树状数组+扫描线+离散化)
- HDU 5862 Counting Intersections(树状数组+离散化+扫描线)
- hdu 5862 Counting Intersections多校第十场
- hdu 5862 Counting Intersections 坐标离散化+树状数组
- 母函数
- Connection refused: connect 异常问题
- range()二元元组遍历
- redis集群搭建
- LightOJ 1079Just another Robbery &&HDU 2955Robberies 01背包
- 统计,优化(Counting Intersections,HDU 5862)
- mkdir -p 参数
- Python面向对象编程(1):基类与__init__()方法
- 4.1 元器件的选型和设计
- 应用JavaScript(二):增强表单的可用性
- 计算几何
- LeetCode
- kotlin官方文档中文翻译(三) 类和对象
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)