POJ 3695 rectangles 1w ask for 20 matrix area and

Source: Internet
Author: User

Topic Link: Click to open the link

A detailed explanation: Click the Open link

The main idea is to give a number of rectangles (n <= 20) and then M queries (M <= 100000)

Each query gives a number of rectangles, asking how large the area of these rectangles is.

When it comes to rectangles, maybe the first reaction is a line tree.

But this problem has a feature, that is, n is very small, M is very large

It's probably not going to work with a line tree.

So change the idea, n is very small, we can consider all possible combinations, and then preprocessing out, so the inquiry is O (1) query

But 1<<20 is clearly more than 100000.

That means we don't have to think about all of this.

You just have to think about the situation in M-queries.

So we first read the situation in the inquiry in, with the binary system to save up.

Then there's DFS, according to the principle of repulsion.

The area of a rectangle-two rectangles intersect the area + three rectangles intersect the area ... That's it

So DFS can have two branches, one is to take this rectangle, the other is not to take


#include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include < stdlib.h> #include <string.h> #include <limits.h> #include <vector> #include <string># Include <time.h> #include <math.h> #include <queue> #include <stack> #include <set># Include <map>const int inf = 1e8;const double eps = 1e-8;const double pi = ACOs ( -1.0); template <class t> Inli      ne 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 <class t> 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<int,int> 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) {(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);d FS (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<<deep));}    int main () {int Cas = 1;  while (Cin>>n>>m, n+m) {printf (' Case%d:\n ', 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);d FS (0,0,inf,inf, 0,-1,0), for (int i = 1; I <= m; i++) printf ("Query%d:%d\n", I, St[ask[i]]);p u    TS ("");} return 0;}


POJ 3695 rectangles 1w ask for 20 matrix area and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.