poj 1436 Horizontally Visible Segments - 線段樹區間更新


Horizontally Visible Segments
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5717   Accepted: 2083

Description

There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments. Three different vertical segments are said to form a triangle of segments if each two of them are horizontally visible. How many triangles can be found in a given set of vertical segments?


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 the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow.

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

The output should consist of exactly d lines, one line for each data set. Line i should contain exactly one integer equal to the number of triangles in the i-th data set.

Sample Input

1
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3

Sample Output

1

/*
題意:
有很多垂直的線段,兩個線段之間存在連線且和其他的線段沒有交點爲可見,
給出一組數據,求三條線段兩兩可見的組數。
題解:
將輸入的的線段按x從小到大排列,然後先查詢再更新,
就是每次插入線段之前,先查詢即將插入的那條線段和以前插入的線段是否可見,
每條線段都有一種顏色,所以用一個vis[i][j]表示i與j可見
最後直接暴力枚舉就可以過了
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=16100;
bool vis[MAXN>>1][MAXN>>1];
struct node{
    int l,r;
    int color;
}tree[MAXN<<2];

struct Node{
    int x,y1,y2;
}p[MAXN>>1];

bool cmp(Node a,Node b){
    return a.x<b.x;
}

void build(int l,int r,int node){
    tree[node].l=l;
    tree[node].r=r;
    tree[node].color=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(l,mid,node<<1);
    build(mid+1,r,node<<1|1);
}
void pushdown(int node){
    if(!tree[node].color) return;
    tree[node<<1].color=tree[node<<1|1].color=tree[node].color;
    tree[node].color=0;///取消標記
}
void query(int l,int r,int node,int val){
    if(tree[node].color!=0){
        vis[tree[node].color][val]=1;
        return;
    }
    if(tree[node].l==tree[node].r)
        return;
    pushdown(node);
    int mid=(tree[node].l+tree[node].r)>>1;
    if(l<=mid)
        query(l,r,node<<1,val);
    if(r>mid)
        query(l,r,node<<1|1,val);
}
void update(int l,int r,int node,int val){
    if(tree[node].l>=l&&tree[node].r<=r){
        tree[node].color=val;
        return;
    }
    pushdown(node);
    int mid=(tree[node].l+tree[node].r)>>1;
    if(l<=mid)
        update(l,r,node<<1,val);
    if(r>mid)
        update(l,r,node<<1|1,val);
}
int main(){
    int t,n,i,j,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int maxn = 0;
        for(i=1;i<=n;i++){
            scanf("%d%d%d",&p[i].y1,&p[i].y2,&p[i].x);
            p[i].y1=2*p[i].y1;
            p[i].y2=2*p[i].y2;
            maxn = max(maxn,p[i].y1);
            maxn = max(maxn,p[i].y2);
        }
        build(0,maxn,1);
        sort(p+1,p+n+1,cmp);
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++){
            query(p[i].y1,p[i].y2,1,i);
            update(p[i].y1,p[i].y2,1,i);
        }
        int ans=0;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            if(vis[i][j])
            for(k=1;k<=n;k++){
                if(vis[i][k]&&vis[j][k])
                    ans++;
            }
        printf("%d\n",ans);
    }
    return 0;
}



發佈了229 篇原創文章 · 獲贊 252 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章