[SGU 532]Building Foundation[几何]

来源:互联网 发布:ubuntu u盘系统 编辑:程序博客网 时间:2024/04/30 16:13
题目链接:[SGU 532]Building Foundation[几何]

题意分析:

给出n个要么平行于x轴要么平行于y轴的线段,问:当前给出的图中一共有多少个矩形?

解题思路:

枚举竖直方向上的线段,然后根据每次枚举的两个线段确定它们之间能有多少个合格水平线段,可以先用二分大致找出范围,然后具体细找符合要求的线段,假设有x个,那么当前这两条竖直线段和这些水平线段能构成的矩形数为:x * (x - 1) / 2。最终答案就是将所有加起来即可。

个人感受:

看到找矩形就方了= =没有想到的就是枚举完可行的水平线段后答案可以那么计算。。。。。。

具体代码如下:

#include<algorithm>#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<iomanip>#include<iostream>#include<map>#include<queue>#include<set>#include<sstream>#include<stack>#include<string>#define lowbit(x) (x & (-x))#define root 1, n, 1#define lson l, m, rt << 1#define rson m + 1, r, rt << 1  1#define pii pair<int, int>#define ll long long#define pr(x) cout << #x << " = " << (x) << '\n';using namespace std;const int INF = 0x7f7f7f7f;const int MAXN = 611;struct P {    int mi, mx, x;    P(int a, int b, int c): mi(a), mx(b), x(c){}    bool operator < (const P &t)const {        return x < t.x;    }};vector<P> hor, ver;int main(){    #ifdef LOCAL    freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);    #endif    int x1, y1, x2, y2;    int n; scanf("%d", &n);    for (int i = 0; i < n; ++i) {        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);        if (x1 == x2) ver.push_back(P(min(y1,y2),max(y1,y2), x1));        else hor.push_back(P(min(x1,x2), max(x1,x2), y1));    }    sort(hor.begin(), hor.end());    ll ans = 0;    for (int i = 0; i < ver.size(); ++i) {        for (int j = i + 1; j < ver.size(); ++j) {            int l = min(ver[i].x, ver[j].x);            int r = max(ver[i].x, ver[j].x);            int up = min(ver[i].mx, ver[j].mx);            int down = max(ver[i].mi, ver[j].mi);            if (l == r || up == down) continue;            //pr(l);pr(r);pr(up);pr(down);            //cout << "begin:\n";            int lef = lower_bound(hor.begin(), hor.end(), P(0, 0, down)) - hor.begin();            int rig = upper_bound(hor.begin(), hor.end(), P(0, 0, up)) - hor.begin();            ll ret = 0;            for (int k = lef; k < rig; ++k) {                P &t = hor[k];               // cout << t.x << ',' << t.mi << ',' << t.mx << endl;                if (t.x <= up && t.x >= down && t.mi <= l && t.mx >= r) ++ret;            }            ans += ret * (ret - 1) / 2;        }    }    printf("%lld\n", ans);    return 0;}


0 0
原创粉丝点击