LA3905-Meteor(扫描线)

题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1906

思路

只需要考虑点落在矩形内的时间段内,于是可以将时间看做坐标轴,求点落在矩形内的区间,然后想象一个时间t从左到右扫描,使得与最多区间相交
并且将遇到左端点和右端点分别看做事件,扫描的过程就是处理事件的过程

细节

  1. 因为点落在矩形边上不算,因此区间应该为开区间
  2. 区间1和区间2的左右端点重合时应先处理右端点事件,再处理左端点事件

代码

开始一直WA是因为a == 0的时候,一直写的!a,还有a为负数的情况

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) cout << #x << " = " << x << endl
#define lc o << 1
#define rc o << 1 | 1
#define pl() cout << endl

const int maxn = 100000 + 10;
struct Event {
    double x;
    int type;
    bool operator < (const Event &a) const {
        return x < a.x || (x == a.x && type > a.type);
    }
} event[maxn * 2];

void update(int x, int a, int w, double &L, double &R) {
    //!a is wrong because a might be negative
    if (a == 0) {
        if (x <= 0 || x >= w) R = L - 1;
    } else {
        if (a > 0) {
            L = max(L, (double)-x / a);
            R = min(R, (double)(w - x) / a);
        } else {
            L = max(L, (double)(w - x) / a);
            R = min(R, (double)-x / a);
        }
    }
}

int main() {
    int T;
    scan(T);
    while (T--) {
        int w, h, n, x, y, a, b;
        int cnt = 0;
        scan2(w, h);
        scan(n);
        for (int i = 0; i < n; i++) {
            scanf("%d %d %d %d", &x, &y, &a, &b);
            double L = 0, R = 1e9;
            update(x, a, w, L, R);
            update(y, b, h, L, R);
            if (R > L) {
                event[cnt++] = (Event){L, 0};
                event[cnt++] = (Event){R, 1};
            }
        }
        sort(event, event + cnt);
        int tot = 0, ans = 0;
        for (int i = 0; i < cnt; i++) {
            if (event[i].type == 0) ans = max(ans, ++tot);
            else tot--;
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值