求若干矩形的不重叠面积 —— 模拟 + 优化

本文介绍了一个模拟情绪评估的小实验,通过在N*N的棋盘上随机染色来模拟快乐值的变化,探讨了如何通过编程实现这一过程,并提供了一个具体的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
Description                                Time Limit: 1sec    Memory Limit:256MB

Dr. R is a very nice man. He is very interested in social emotion classification. As a researcher in this field, he needs some metrics to evaluate the emotion of people. And he finds an interesting method to evaluate happy emotion of people and he do a small experiment on himself. He owns a check board, which has N*N grids. N represents the row number and the column number. The colors of the grids are all white. In the beginning, he thinks his happy value is maximized, which is the grid number the check board has. However, Dr. R may sometimes meet something unhappy and his happy value will decrease. When he meets an unhappy thing, he will evaluate the unhappy value and decrease the happy value. How much he decease the happy value will be decided by the check board. Each time he feels unhappy, he will randomly dye some grids black. These grids will be in a small rectangular region. An interesting thing is that if a grid is dyed twice if will white color again. The happy value of Dr. R is measured by the number of white grids on the check board. So the happy value may sometimes increase but not decrease. Now give you the unhappy event, you should calculate the final happy value of Dr. R.

Input

The input has several test cases.

The first line contains two integers N and M (1 <=N<= 60, 1<=M<=100000), respectively represents the size of the check board and the number of unhappy events. In the next M lines, each line contains four integers x1, x2, y1, y2 (1 <= x1, x2, y1, y2 <= 60), representing the position of the rectangular region. (x1, y1) is the left-bottom position of the rectangle and (x2, y2) is the right-top position of the rectangle.

 

Output

An integer represents the happy value of Dr. R.

Sample Input
2 1
1 1 1 1
3 2
1 2 1 2
2 3 1 3
Sample Output
3
3
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int a[65][65];

int main ()
{    
    int n, m, x1, x2, y1, y2;
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i=0; i<m; i++) {
            scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
            for(int j=x1; j<=x2; j++) {
                a[j][y1] ^= 1;
                a[j][y2+1] ^= 1;
            }
        }
        int state, ans = 0;
        for(int i=1; i<=n; i++) {
            state = 1;
            for(int j=1; j<=n; j++) {
                if(a[i][j] == 1) {
                    state ^= 1;
                }
                if(state==1)    ++ans;
            }
        }
        printf("%d\n", ans);
        memset(a, 0, sizeof(a));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值