题意:给出n个矩形的左下角和右上角坐标,问矩形(0,0),(ti,ti)内矩形面积是多少,重叠也算。
题解:参照http://blog.csdn.net/shiqi_614/article/details/8869676这里的题解,把所有矩形关于t的重叠面积计算式中的系数用树状数组维护,很机智。
#include <stdio.h>
#include <string.h>
#include <iostream>
#define ll long long
using namespace std;
const int N = 200005;
int n, q;
ll S[3][N], x1, y1, x2, y2;
int lowbit(int x) { return x & (-x); }
void modify(int st, int en, ll v, int flag) {
for (int i = st; i < N; i += lowbit(i))
S[flag][i] += v;
for (int i = en + 1; i < N; i += lowbit(i))
S[flag][i] -= v;
}
ll query(int x, int flag) {
ll ret = 0;
for (int i = x; i > 0; i -= lowbit(i))
ret += S[flag][i];
return ret;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(S, 0, sizeof(S));
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
ll temp1 = max(x1, y1), temp2 = min(x2, y2);
if (temp1 < temp2) {
modify(temp1, temp2, 1, 0);
modify(temp1, temp2, -x1 - y1, 1);
modify(temp1, temp2, x1 * y1, 2);
}
if (y2 > x2) {
temp1 = max(x2, y1) + 1;
modify(temp1, y2, x2 - x1, 1);
modify(temp1, y2, x1 * y1 - x2 * y1, 2);
}
if (x2 > y2) {
temp1 = max(y2, x1) + 1;
modify(temp1, x2, y2 - y1, 1);
modify(temp1, x2, x1 * y1 - x1 * y2, 2);
}
modify(max(x2, y2) + 1, N, (y2 - y1) * (x2 - x1), 2);
}
scanf("%d", &q);
while (q--) {
ll tt;
scanf("%lld", &tt);
printf("%lld\n", tt * tt * query(tt, 0) + tt * query(tt, 1) + query(tt, 2));
}
}
return 0;
}