POJ 3695 Rectangles(矩形切割)

本文介绍了一种用于处理多个矩形区域填充与查询的算法实现。该算法通过一系列的坐标覆盖操作来计算特定矩形集合的填充面积,适用于图形用户界面等场景中矩形区域的动态管理和填充查询。
Rectangles
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3927 Accepted: 1150

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<=RN), 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


【题意】给N个矩形,他们可能会重叠,然后给M个询问,每个询问给出指定的矩形位置,然后分别计算每个询问中选中的矩形的并。

【解题方法】如上,直接粘模板让我WA了很多次啊,之后又TLE,看了讨论知道用C++可以过,果然1200ms过掉了。

【AC 代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
struct node{
    LL x1,y1,x2,y2;
    LL sum;
    void read()
    {
        scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
    }
}T[44];
LL n,m;
LL a[44];
LL xx;
void Cover(LL x1,LL y1,LL x2,LL y2,LL k,LL c)
{
    while(k<xx&&(x1>=T[a[k]].x2||x2<=T[a[k]].x1||y1>=T[a[k]].y2||y2<=T[a[k]].y1)) k++;
    if(k>=xx){
        T[a[c]].sum+=(x2-x1)*(y2-y1);
        return ;
    }
    if(x1<T[a[k]].x1){
        Cover(x1,y1,T[a[k]].x1,y2,k+1,c);
        x1=T[a[k]].x1;
    }
    if(x2>T[a[k]].x2){
        Cover(T[a[k]].x2,y1,x2,y2,k+1,c);
        x2=T[a[k]].x2;
    }
    if(y1<T[a[k]].y1){
        Cover(x1,y1,x2,T[a[k]].y1,k+1,c);
        y1=T[a[k]].y1;
    }
    if(y2>T[a[k]].y2){
        Cover(x1,T[a[k]].y2,x2,y2,k+1,c);
        y2=T[a[k]].y2;
    }
}
int main()
{
    LL ks1=1,ks2;
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        memset(T,0,sizeof(T));
        for(int i=0; i<n; i++) T[i].read();
        printf("Case %I64d:\n",ks1++);
        ks2=1;
        while(m--)
        {
            memset(a,0,sizeof(a));
            for(int i=0; i<n; i++) T[i].sum=0LL;
            scanf("%I64d",&xx);
            for(int i=0; i<xx; i++){
                scanf("%I64d",&a[i]);
                a[i]--;
            }
            for(LL i=xx-1; i>=0; i--){
                Cover(T[a[i]].x1,T[a[i]].y1,T[a[i]].x2,T[a[i]].y2,i+1,i);
            }
            LL ans=0;
            for(int i=0; i<xx; i++){
                ans+=T[a[i]].sum;
            }
            printf("Query %I64d: %I64d\n",ks2++,ans);
        }
        printf("\n");
    }
    return 0;
}



【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值