POJ 1436 Horizontally Visible Segments
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 2188 | Accepted: 818 | 
Description
Task
Write a program which for each data set:
reads the description of a set of vertical segments,
computes the number of triangles in this set,
writes the result.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8 000, equal to the number of vertical line segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
yi', yi'', xi - y-coordinate of the beginning of a segment, y-coordinate of its end and its x-coordinate, respectively. The coordinates satisfy 0 <= yi' < yi'' <= 8 000, 0 <= xi <= 8 000. The segments are disjoint.
Output
Sample Input
1 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3
Sample Output
1
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
#define N 20000
using namespace std;
int st[N<<2];
int n;
struct node
{
    int y1,y2,x1;
};
node xy[8003];
int sot[N],id;
bool visit[8003];
vector<int> hash[8003];
bool cmp(const node&a,const node&b)
{
    return a.x1<b.x1;
}
void build(int l,int r,int k)
{
     st[k]=-1;
    if(l==r)
     return ;
     int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void lookfor(int L,int R,int l,int r,int k)
{
    // printf("id=%d k= %d %d\n",l,r,st[k]);
    if(st[k]!=-1)
    {
        if(!visit[st[k]])
        {
            visit[st[k]]=1;
            hash[id].push_back(st[k]);
        }
        return ;
    }
    if(l==r) return ;
    int m=(l+r)>>1;
    if(L<=m) lookfor(L,R,lson);
    if(R>m) lookfor(L,R,rson);
}
void down(int &k)
{
    st[k<<1]=st[k<<1|1]=st[k];
    st[k]=-1;
}
void updata(int L,int R,int l,int r,int k)
{
    if(L<=l&&R>=r)
    {
       st[k]=id;
       //printf("f=%d %d %d\n",l,r,id);
       return ;
    }
    if(st[k]!=-1)
      down(k);
      int m=(l+r)>>1;
    if(L<=m) updata(L,R,lson);
    if(R>m)  updata(L,R,rson);
}
int main()
{
   int i,j,d;
   int m;
   scanf("%d",&d);
   while(d--)
   {
       scanf("%d",&m);
       n=0;
       for(j=i=0;i<m;i++)
       {
          scanf("%d%d%d",&xy[i].y1,&xy[i].y2,&xy[i].x1);
          sot[j++]=xy[i].y1;sot[j++]=xy[i].y2;
          n=n>xy[i].y1?n:xy[i].y1;
          n=n>xy[i].y2?n:xy[i].y2;
          hash[i].clear();
       }
      n=n<<1;
      build(0,n,1);
      sort(xy,xy+m,cmp);
      for(i=0;i<m;i++)
      {
          memset(visit,0,m*sizeof(bool));
          id=i;
          lookfor(xy[i].y1<<1,xy[i].y2<<1,0,n,1);
          updata(xy[i].y1<<1,xy[i].y2<<1,0,n,1);
      }
      int rc=0;
     // printf("ss");
     for(i=m-1;i>0;i--)//暴力呀
     {
         for(j=0;j<hash[i].size();j++)
            {
                int s=hash[i][j];
                for(int k=0;k<hash[s].size();k++)
                 {
                     for(int l=0;l<hash[i].size();l++)
                       if(hash[s][k]==hash[i][l])
                           rc++;
                 }
            }
     }printf("%d\n",rc);
   }
    return 0;
}
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号