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;
}
时间: 2024-11-07 02:11:35

zoj 3157 Weapon 逆序数/树状数组的相关文章

POJ_3067 Japan[ 逆序数 树状数组 or 归并排序)

传送门:POJ_3067 题目:n,m,k;左右两列数,数的范围分别1-n,1-m,然给k个连线. Sample Input 1 3 4 4 1 4 2 3 3 2 3 1 Sample Output Test case 1: 5 思路:逆序数 代码: //树状数组版 //块状数组 #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<algorit

HPU1291 小朋友排队 【逆序数+树状数组】

1291: 小朋友排队 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 1 [提交][状态][讨论版] [Edit] 题目描述 n 个小朋友站成一排.现在要把他们按身高从低到高的顺序排列,但是每次只能交换位置相邻的两个小朋友. 每个小朋友都有一个不高兴的程度.开始的时候,所有小朋友的不高兴程度都是0. 如果某个小朋友第一次被要求交换,则他的不高兴程度增加1,如果第二次要求他交换,则他的不高兴程度增加2(即不高兴程度为3),依次类推.当要求某个小朋友第k次交换时,他的不高

hdu 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai

POJ 2299 Ultra-QuickSort(逆序数 树状数组)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 45960   Accepted: 16702 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ - 2299 - Ultra-QuickSort = 归并排序 + 逆序对 / 树状数组

http://poj.org/problem?id=2299 求逆序对最简单的绝对不会是树状数组,一定是归并排序(认真),不过树状数组会不会快一点呢?理论上应该是树状数组快一点(假如不进行离散化). #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set

zoj 3635 Cinema in Akiba (树状数组求第K大)

Cinema in Akiba Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies wit

BZOJ3295 CQOI2011 动态逆序对 树状数组套线段树

离线倒着做,每次加入一个节点后新增的逆序对数量就是其左边大于它的数的个数(左边数的总数-左边小于它的数的个数)+右边小于它的数的个数 用树状数组维护求和,对于树状数组中每个节点v所对应的区间线段树维护区间[l,r]中大于v的数的个数. 最后唯一的问题就是指针版线段树MLE-- #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <alg

求逆序对[树状数组] jdoj

求逆序对 题目大意:给你一个序列,求逆序对个数. 注释:n<=$10^5$. 此题显然可以跑暴力.想枚举1到n,再求在i的后缀中有多少比i小的,统计答案即可.这显然是$n^2$的.这...显然过不去,我们思考如何优化?显然,这里的有些过程是重复的.我们将这个序列设为a序列,对于两个1到n中的整数i<j,在j后面的数我们进行了多次重复枚举,我们思考如何优化.容易想到用一个桶来记录.只需要记录对于每一个数来讲,我后面有多少个数是比我小的,只需要将桶中的数累加即可.但是,我们必须记录是这个数之后的桶

【2018.12.15】【考试总结】【模拟+逆序对+树状数组+贪心+multiset】爆零之旅

这是我悲惨的接近爆零的一次考试,但是本蒟蒻不能放弃,还是要总结的QAQ 答题卡 [题目背景] 八月是个悲惨的月份.先不谈炎热的天气,对于新生来说,八月意味着军训: 而对于高二高三的同学来说,八月意味着开学考试.而考试就意味着改卷,改卷 也就意味着答题卡.不幸的是,学校读答题卡的机器的评分软件坏了,wyx 就被 老师要求写一个评分的程序. [问题描述] 软件需要读入学生的姓名.试题答案以及学生的答题卡信息. 学生姓名 学校的信息管理系统中存储了所有学生的姓名,一共 名学生.每个学生的 名字的组成只