题三 画线

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>

using namespace std;

template<class T>
T m_max(T a, T b) { return a > b ? a : b; }

template<class T, class ... Ts>
T m_max(T a, Ts ... ts) {
    return max(a, m_max(ts...));
}

//=================================================================
const int maxn = 100000 + 5;
int n;

struct Point
{
    int x, y;
    Point (int _x = 0, int _y = 0)
        :x(_x), y(_y) { }
};

map<int, vector<pair<int,int> > > segments[4];

int judgeSegment(const Point & a, const Point & b)
{
    if (a.y == b.y) {
        segments[0][a.y].push_back(make_pair(a.x, b.x));
        return 0;   //水平
    }
    else if (a.x == b.x) {
        segments[1][a.x].push_back(make_pair(a.y, b.y));
        return 1;  //垂直
    }
    else if (a.y < b.y) {
        segments[2][a.y - a.x].push_back(make_pair(a.x, b.x));
        return 2;   //正45
    }
    else {
        segments[3][a.y + a.x].push_back(make_pair(a.x, b.x));
        return 3;  //反45
    }
}

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < 4; ++i) segments[i].clear();
    for (int i = 0; i < n; ++i) {
        Point a, b;
        scanf("%d%d", &a.x, &a.y);
        scanf("%d%d", &b.x, &b.y);
        if (a.x > b.x || (a.x == b.x && a.y > b.y)) swap(a, b);
        judgeSegment(a, b);
    }
}

int countSegment(vector<pair<int, int> >  lrPairs)
{
    int res = 0;
    sort(lrPairs.begin(), lrPairs.end());
    int pre = -1e9;
    for (auto lrPair : lrPairs) {
        if (lrPair.first > pre) ++res;
        pre = max(pre, lrPair.second);
    }
    return res;
}


void solve()
{
    int ans = 0;
    for (int i = 0; i < 4; ++i) for (auto & lrPairs : segments[i]) {
        ans += countSegment(lrPairs.second);
    }
    printf("%d\n", ans);
}

int main()
{
    int T; cin >> T;
    while (T--) {
        input();
        solve();
        //puts("");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值