线段树区间更新——POJ 1436

来源:互联网 发布:现代汉语词典软件 编辑:程序博客网 时间:2024/04/30 09:31

对应POJ题目:点击打开链接


Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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

150 4 40 3 13 4 20 2 20 2 3

Sample Output

1

Source


题意:在一个平面内,有一些竖直的线段,若两条竖直线段之间可以连一条水平线,这条水平线不与其他竖直线段相交,称这两条竖直线段为相互可见的。若存在三条竖直线段,两两“相互可见”,则构成“线段三角形”。给出一些竖直的线段,问一共有多少“线段三角形”。


思路:求两两“相互可见”的所有可能,暴力求解,复杂度n^3;线段树结点维护区间有多少条可见线段


比较低效的方法╮(╯_╰)╭,有空改善。。。


#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))#define N 8001<<1const int MAXN=1000+10;const int INF=1<<30;using namespace std;int color[N<<2];bool vis[N>>1][N>>1];struct Line{int y1,y2,x;}line[N>>1];void down(int rt){if(color[rt]){color[rt<<1]=color[rt];color[rt<<1|1]=color[rt];color[rt]=0;}}void updata(int rt, int left, int right, int l, int r, int c){if(left==l && right==r){color[rt]=c;return;}down(rt);int mid=(left+right)>>1;if(mid>=r) updata(rt<<1, left, mid, l, r, c);else if(mid<l) updata(rt<<1|1, mid+1, right, l, r, c);else{updata(rt<<1, left, mid, l, mid, c);updata(rt<<1|1, mid+1, right, mid+1, r, c);}}void query(int rt, int left, int right, int l, int r, int id){if(color[rt]){vis[id][color[rt]]=vis[color[rt]][id]=1;return;}if(left==right) return;down(rt);int mid=(left+right)>>1;if(mid>=r) query(rt<<1, left, mid, l, r, id);else if(mid<l) query(rt<<1|1, mid+1, right, l, r, id);else{query(rt<<1, left, mid, l, mid, id);query(rt<<1|1, mid+1, right, mid+1, r, id);}}bool cmp(Line l1, Line l2){return l1.x<l2.x;}int main(){//freopen("in.txt","r",stdin);int T;scanf("%d", &T);while(T--){ms(vis,0);ms(color,0);int n;scanf("%d", &n);for(int i=0; i<n; i++){scanf("%d%d%d", &line[i].y1, &line[i].y2, &line[i].x);line[i].y1<<=1;line[i].y2<<=1;}sort(line, line+n, cmp);int cnt=0;for(int i=0; i<n; i++){++cnt;query(1, 0, N, line[i].y1, line[i].y2, cnt);updata(1, 0, N, line[i].y1, line[i].y2, cnt);}int ans=0;for(int i=1; i<=n; i++){for(int j=i+1; j<=n; j++){if(!vis[i][j]) continue;for(int k=j+1; k<=n; k++)if(vis[j][k] && vis[i][k]) ans++;}}printf("%d\n", ans);}return 0;}




0 0
原创粉丝点击