首页 > 其他 > 详细

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

时间:2016-03-27 01:36:26      阅读:187      评论:0      收藏:0      [点我收藏+]
 
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;
}

 

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

原文:http://www.cnblogs.com/AcIsFun/p/5324557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!