看了网上各大神的解题,才发现不进行数学推导怎么都会超时。。。
选了个最简单粗暴的算法,对每个四边形的四个点(x,y),若max(x, y) <= t,则加上(t,t)与(x,y)组成的矩形,否则减去,最后通过合并系数来简化求值操作。。。
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef __int64 ll;
const int MAX = 50005;
struct node
{
ll x, y;
int maxnum;
void getmx()
{
maxnum = x>y?x:y;
}
}data1[MAX], data2[MAX];
int num1, num2;
bool cmp(node &a, node &b)
{
return a.maxnum < b.maxnum;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int t, n, x;
int i;
ll j;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
num1 = num2 = 0;
node tpNode1, tpNode2;
for (i = 0; i< n; i++)
{
scanf("%I64d%I64d%I64d%I64d", &tpNode1.x, &tpNode1.y, &tpNode2.x, &tpNode2.y);
tpNode1.getmx(); tpNode2.getmx();
data1[num1++] = tpNode1;
data1[num1++] = tpNode2;
j = tpNode1.y;
tpNode1.y = tpNode2.y;
tpNode2.y = j;
tpNode1.getmx(); tpNode2.getmx();
data2[num2++] = tpNode1;
data2[num2++] = tpNode2;
}
sort(data1, data1+num1, cmp);
sort(data2, data2+num2, cmp);
int bg1 = 0, bg2 = 0;
ll now;
ll res = 0;
ll aa = 0, bb = 0, tt = 0;
scanf("%d", &x);
while (x--)
{
scanf("%I64d", &now);
for (; data1[bg1].maxnum <= now && bg1 <= num1; bg1++)
{
aa -= data1[bg1].x+data1[bg1].y;
bb += data1[bg1].x*data1[bg1].y;
tt++;
}
for ( ; data2[bg2].maxnum <= now && bg2 <= num2; bg2++)
{
aa += data2[bg2].x+data2[bg2].y;
bb -= data2[bg2].x*data2[bg2].y;
tt--;
}
res = tt*now*now+aa*now+bb;
printf("%I64d\n", res);
}
}
return 0;
}