zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    win7 64位下 mongodb安装及命令运行
    CYQ.Data 快速开发EasyUI
    面试杂记:三个月的面试回忆录(携程、腾讯等)
    那点你不知道的XHtml(Xml+Html)语法知识(DTD、XSD)
    .NET各大平台数据列表控件绑定原理及比较(WebForm、Winform、WPF)
    CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
    CYQ.Data V4系列全面开源(2013-08-04)
    C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
    C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
    突破瓶颈,对比学习:Eclipse开发环境与VS开发环境的调试对比
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4250312.html
Copyright © 2011-2022 走看看