zoukankan      html  css  js  c++  java
  • Poj 3695-Rectangles 矩形切割

    Rectangles
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 3846   Accepted: 1124

    Description

    You are developing a software for painting rectangles on the screen. The software supports drawing several rectangles and filling some of them with a color different from the color of the background. You are to implement an important function. The function answer such queries as what is the colored area if a subset of rectangles on the screen are filled.

    Input

    The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 20) and M(1 ≤ M ≤ 100000), indicating the number of rectangles on the screen and the number of queries, respectively.
    The i-th line of the following N lines contains four integers X1,Y1,X2,Y2 (0 ≤ X1 < X2 ≤ 1000, 0 ≤ Y1 < Y2 ≤ 1000), which indicate that the lower-left and upper-right coordinates of the i-th rectangle are (X1, Y1) and (X2,Y2). Rectangles are numbered from 1 to N.
    The last M lines of each test case describe M queries. Each query starts with a integer R(1<=R ≤ N), which is the number of rectangles the query is supposed to fill. The following list of R integers in the same line gives the rectangles the query is supposed to fill, each integer of which will be between 1 and N, inclusive.

    The last test case is followed by a line containing two zeros. 

    Output

    For each test case, print a line containing the test case number( beginning with 1).
    For each query in the input, print a line containing the query number (beginning with 1) followed by the corresponding answer for the query. Print a blank line after the output for each test case. 

    Sample Input

    2  2
    0 0 2 2
    1 1 3 3
    1 1
    2 1 2
    2 1
    0 1 1 2
    2 1 3 2
    2 1 2
    0 0

    Sample Output

    Case 1:
    Query 1: 4
    Query 2: 7
    
    Case 2:
    Query 1: 2

    Source

    题意:给你一些矩形,编号为1~n。有一些询问,每次问你其中的一些矩形的面积并。

    题解:

    矩形切割

    矩形切割裸题。。。当然也可以用线段树(大坑。。。)

    直接挂个模版就过了。

    看到Discuss中有人说矩形切割会TLE。可能是常数太大了吧。但我没有TLE呀,而且貌似跑的飞快(时间好像排32名,内存也很小。。。)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct node
     4 {
     5     int X1,Y1,X2,Y2;
     6 }R[26];
     7 int s1,cc[26],ans[26];
     8 int read()
     9 {
    10     int s=0,fh=1;char ch=getchar();
    11     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    12     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    13     return s*fh;
    14 }
    15 void Cover(int X1,int Y1,int X2,int Y2,int k,int k1)
    16 {
    17     while(k<=s1&&(X1>=R[cc[k]].X2||X2<=R[cc[k]].X1||Y1>=R[cc[k]].Y2||Y2<=R[cc[k]].Y1))k++;
    18     if(k>=s1+1){ans[k1]+=(X2-X1)*(Y2-Y1);return;}
    19     if(X1<R[cc[k]].X1){Cover(X1,Y1,R[cc[k]].X1,Y2,k+1,k1);X1=R[cc[k]].X1;}
    20     if(X2>R[cc[k]].X2){Cover(R[cc[k]].X2,Y1,X2,Y2,k+1,k1);X2=R[cc[k]].X2;}
    21     if(Y1<R[cc[k]].Y1){Cover(X1,Y1,X2,R[cc[k]].Y1,k+1,k1);Y1=R[cc[k]].Y1;}
    22     if(Y2>R[cc[k]].Y2){Cover(X1,R[cc[k]].Y2,X2,Y2,k+1,k1);Y2=R[cc[k]].Y2;}
    23 }
    24 int main()
    25 {
    26     int n,m,i,case1=0,C=0,j,sum;
    27     while(1)
    28     {
    29         n=read();m=read();if(n==0&&m==0)break;
    30         for(i=1;i<=n;i++){R[i].X1=read();R[i].Y1=read();R[i].X2=read();R[i].Y2=read();}
    31         printf("Case %d:
    ",++case1);
    32         C=0;
    33         for(i=1;i<=m;i++)
    34         {
    35             s1=read();
    36             for(j=1;j<=s1;j++)cc[j]=read();
    37             for(j=s1;j>=1;j--)Cover(R[cc[j]].X1,R[cc[j]].Y1,R[cc[j]].X2,R[cc[j]].Y2,j+1,j);//矩形切割法
    38             sum=0;
    39             for(j=1;j<=s1;j++){sum+=ans[j];ans[j]=0;}
    40             printf("Query %d: %d
    ",++C,sum);
    41         }
    42         printf("
    ");
    43     }
    44     fclose(stdin);
    45     fclose(stdout);
    46     return 0;
    47 }
  • 相关阅读:
    Global Vectors forWord Representation
    Latent Semantic Analysis(LSA/ LSI)原理简介
    目前最快速的多线程Kmeans算法,java实现
    UndertowServer+SpringMVC+Thymeleaf模板引擎构建轻量级的web项目
    中文分词之逆向最大匹配算法结合时间位置进行分词
    Lucene 索引与检索架构图
    搜索算法
    聚类
    动态规划--国王挖金矿问题
    JS获取后台返回的JSON数据
  • 原文地址:https://www.cnblogs.com/Var123/p/5377508.html
Copyright © 2011-2022 走看看