标签:des style color io os ar strong for div
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4130 | Accepted: 1511 |
Description
Input
Output
Sample Input
1 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3
Sample Output
1
——————————————————分割线————————————————
题目大意:
如果两条线段相交,则叫做水平可见,给出n条这样的线段,求有多少组的3条线段两两可见
思路:
对每条线段的横坐标从小到大排序,然后一边插入,一边统计跟当前要插入的线段是否相交,存放到vector中,才能保证不重复不遗漏。插入的时候给每条线段一个颜色,来区分不同的线段,相当于区间染色
求出哪些线段可以两两相交之后,暴力枚举3条线段两两可见
如果线段x能见到y,z,再判断线段y能不能见到z,如果能,则符合3条线段两两可见
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define vi vector<int>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=16001;
using namespace std;
vi v[maxn+5];
int cover[maxn<<2];
int hash[maxn+5];
struct v_seg
{
int s,t,x;
bool operator<(const v_seg&A) const
{
return x<A.x;
}
}ss[maxn+5];
void push_down(int rt)
{
if(cover[rt]!=-1){
cover[rt<<1]=cover[rt<<1|1]=cover[rt];
cover[rt]=-1;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R){
cover[rt]=c;
return;
}
push_down(rt);
int m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(m<R) update(L,R,c,rson);
}
void query(int L,int R,int c,int l,int r,int rt)
{
if(cover[rt]!=-1){
if(hash[cover[rt]]!=c){
v[cover[rt]].push_back(c);
hash[cover[rt]]=c;
}
return ;
}
if(l==r) return ;
int m=(l+r)>>1;
if(L<=m) query(L,R,c,lson);
if(m<R) query(L,R,c,rson);
}
int main()
{
int T;
cin>>T;
while(T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d %d %d",&ss[i].s,&ss[i].t,&ss[i].x);
ss[i].s<<=1,ss[i].t<<=1;
v[i].clear();
}
sort(ss,ss+n);
memset(hash,-1,sizeof(hash));
memset(cover,-1,sizeof(cover));
for(int i=0;i<n;++i){
query(ss[i].s,ss[i].t,i,0,maxn,1);
update(ss[i].s,ss[i].t,i,0,maxn,1);
}
int ans=0;
for(int i=0;i<n;++i){
for(int j=0;j<v[i].size();++j){
int x=v[i][j];
for(int k=0;k<v[i].size();++k){
for(int t=0;t<v[x].size();++t){
if(v[i][k]==v[x][t]){
ans++;
break;
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)
标签:des style color io os ar strong for div
原文地址:http://blog.csdn.net/u014141559/article/details/39209141