题意:给定若干平行与坐标轴的线段, 求这些线段组成的图形有多少个矩形
Tutorial:
这样考虑, 针对每个水平的线段, 我们需要维护这些垂直的线段:
- 与该水平线段相交的
- 垂直线段的最高点大于水平线段的高度
于是我们就可以用权值树状数组来维护该水平线段上有多少条符合要求的线段, 然后再用一个vector装最高点, 于是我们就可以向上扫描, 若在该水平线段上方有一水平线段, 并且该水平线段与符合要求的垂直线段有n个交点, 那么,以该两水平线段与中间若干符合要求的垂直线段会围成 n × ( n − 1 ) / 2 n\times (n-1)/2 n×(n−1)/2个矩形, 最后统计一下矩形的个数就好了
Solution:
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define oo INT_MAX
#define ll long long
#define db double
#define all(a) a.begin(), a.end()
#define met(a, b) memset(a, b, sizeof(a))
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define lowbit(x) x &(-x)
#define pi acos(-1.0)
using namespace std;
using namespace __gnu_pbds;
const db eps = 1e-6;
const int maxn = 1e4 + 9;
int n;
vector<pair<int, int>> hori[maxn], ver[maxn];
vector<int> d[maxn];
struct bit
{
int b[maxn];
void add(int x, int v)
{
for (; x < maxn; x += lowbit(x))
b[x] += v;
}
int ask(int x)
{
ll ret = 0;
for (; x; x -= lowbit(x))
ret += b[x];
return ret;
}
void init()
{
met(b, 0);
}
} c;
signed main()
{
ios::sync_with_stdio(0);
cin >> n;
_rep(i, 1, n)
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1 += 5001, x2 += 5001, y1 += 5001, y2 += 5001;
if (x1 == x2)
ver[x1].push_back(make_pair(min(y1, y2), max(y1, y2)));
else
hori[y1].push_back(make_pair(min(x1, x2), max(x1, x2)));
}
ll ans = 0;
_for(cur_h, 0, maxn)
{
for (auto cur_base : hori[cur_h])
{
int l = cur_base.first, r = cur_base.second;
_for(i, 0, maxn) d[i].clear();
c.init();
_rep(i, l, r)
{
for (auto ver_seg : ver[i])
{
if (ver_seg.first <= cur_h && ver_seg.second > cur_h)
{
d[ver_seg.second].push_back(i);
c.add(i, 1);
}
}
}
_for(sec_y, cur_h + 1, maxn)
{
for (auto upper_hor_seg : hori[sec_y])
{
ll cnt = c.ask(upper_hor_seg.second) - c.ask(upper_hor_seg.first - 1);
ans += (cnt - 1) * cnt / 2;
}
for(auto x_to_Delete:d[sec_y]){
c.add(x_to_Delete, -1);
}
}
}
}
cout << ans << endl;
}