zoj 3157 Weapon 逆序数/树状数组

B - Weapon
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

In World War 3, your countries‘ scientists have invented a special weapon. Assume that the enemy‘s city can be described by rectangular coordinates and it has n roads which are all lines. None of the road is paralled with Y-axis. Besides, each road is represented by two different points (ai,bi) (ci,di) on it. Any three roads will not intersect at one point.

This special weapon can destroy all the castles whose x coordinate belongs to (l,r). After spying, you know that all the castles are set in the crossing point of two roads and in each crossing point there is a castle. In addition, each road‘s end-point‘s x coordinate does not belong to (l,r).

The scientists want to check the weapon‘s effect. If its effect can not reach army‘s expectation, they have to spend more time and more money in expanding its range. Obviously, the number of castles it can destroy plays an important role on the effect. So you are asked to calculate how many castles can be destroyed by this special weapon.

Input

Input contains multiple cases.

Every test case, the first line is an integers n (2 <= n <= 10000). Then n lines follow. The (i+1)-th line contains four integers ai,bi,ci,di (-1E8 <= ai,bi,ci,di <= 1E8). The (n+2)-th line contains two doubles l,r (-1E8 <= l,r <= 1E8) There is a blank line between two cases.

Output

For each case, output the number of castles that can be destroyed by the weapon.

Sample Input

3
0 0 1 1
2 0 1 1
0 0 2 0
0 2.5

Sample Output

2
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 10001
#define eps 1e-6
const int inf=0x7fffffff;   //无限大
int N;
struct node
{
    double x;
    double y;
};
double d[maxn];
node point1[maxn],point2[maxn];
node kiss[maxn],kill[maxn];
int lowbit(int x)
{
    return x&(-x);
}

void update1(int x)
{
    while(x<=N)
     {
         d[x]++;
         x+=lowbit(x);
     }
}

void update2(int x,int num)
{
    while(x>0)
     {
         d[x]+=num;
         x-=lowbit(x);
     }
}

int getSum1(int x)
{
    int s=0;
    while(x>0)
     {
         s+=d[x];
         x-=lowbit(x);
     }
    return s;
}

bool cmp(node x,node y)
{
    if(x.x==y.x)
        return x.y<y.y;
    return x.x<y.x;
}
int main()
{
    sspeed;
    int n;
    while(cin>>n)
    {
        memset(d,0,sizeof(d));
        N=n;
        for(int i=0;i<n;i++)
        {
            cin>>point1[i].x>>point1[i].y>>point2[i].x>>point2[i].y;
        }
        double l,r;
        cin>>l>>r;
        l+=eps;
        r-=eps;
        for(int i=0;i<n;i++)
        {
            double k;
            k=(point2[i].y-point1[i].y)/(point2[i].x-point1[i].x);
            kill[i].x=k*(l-point2[i].x)+point2[i].y;
            kill[i].y=k*(r-point2[i].x)+point2[i].y;//求出
        }
        ll ans=0;
        sort(kill,kill+n,cmp);
        for(int i=0;i<n;i++)
        {
            kiss[i].x=kill[i].y;
            kiss[i].y=i+1;
        }
        sort(kiss,kiss+n,cmp);
        for(int i=n-1;i>=0;i--)
        {
            ans+=getSum1(kiss[i].y);
            update1(kiss[i].y);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。