POJ 3695 Rectangles 1 W ask for 20 matrix areas and reject
The question is to give several rectangles (n <= 20) and then m queries (m <= 100000)
Each query will give numbers for some Rectangles and ask the area and size of these rectangles.
When talking about rectangles, maybe the first response is a line segment tree.
However, n is very small and m is very large.
Using a line segment tree may not work.
So let's change our thinking. n is very small. We can take all possible combinations into account, and then pre-process them. In this way, we will query O (1 ).
But 1 <20 is obviously far greater than 100000
That is to say, we do not need to take all the situations into consideration.
You only need to consider the situation in the m inquiry.
So we first read all the information in the query and store it in binary.
Then there is DFS. Based on the refresh principle
The area of a rectangle-the area of the intersection of the two rectangles + the area of the intersection of the three rectangles ...... That's it.
Therefore, DFS can have two types of branches. One is to take this rectangle, and the other is not to take
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const int inf = 1e8;const double eps = 1e-8;const double pi = acos(-1.0);template
inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template
inline void pt(T x) { if (x <0) { putchar('-');x = -x; } if(x>9) pt(x/10); putchar(x%10+'0'); }using namespace std;typedef long long ll;typedef pair
pii; int n, m; struct node{int x1, x2, y1, y2;int area(){return abs(x1-x2)*abs(y1-y2);}}a[30];int ask[100005];int st[1<<21];void dfs(int xa, int ya, int xb, int yb, int deep, int flag, int sta){if(xa >= xb || ya >= yb)return;if(deep == n){if(sta){for(int i = 1; i <= m; i++)if((ask[i]|sta) == ask[i])st[ask[i]] += flag*(xb-xa)*(yb-ya);}return ;}dfs(xa, ya, xb, yb, deep+1, flag, sta);dfs(max(xa, a[deep+1].x1), max(ya, a[deep+1].y1), min(xb, a[deep+1].x2), min(yb, a[deep+1].y2), deep+1, -flag, sta|(1<
>n>>m, n+m){ printf(Case %d:, Cas++);for(int i = 1; i <= n; i++){rd(a[i].x1); rd(a[i].y1); rd(a[i].x2); rd(a[i].y2);}for(int i = 1, siz, num; i <= m; i++){ask[i] = 0;rd(siz);while(siz-->0){rd(num); ask[i]|=1<<(num-1);}}memset(st, 0, sizeof st);dfs(0,0,inf,inf, 0,-1,0);for(int i = 1; i <= m; i++)printf(Query %d: %d, i, st[ask[i]]);puts();} return 0;}