hihocoder [Offer收割]编程练习赛8 矩形计数

考虑k比较小,想到容斥原理,枚举,容斥求和。

//http://www.cnblogs.com/IMGavin/
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <algorithm>
using namespace std;

typedef long long LL;
#define gets(A) fgets(A, 1e8, stdin)
const int INF = 0x3F3F3F3F, N = 1008, MOD = 1003;

const double EPS = 1e-6;
int x[N], y[N];
int tot;
int r[N], c[N], n, m, k;
LL ans;

void dfs(int d, int cnt, int x1, int y1){
	if(d == tot){
		if(cnt & 1){
			ans -= x1 * y1;
		}else{
			ans += x1 * y1;
		}
		return ;
	}
	dfs(d + 1, cnt, x1, y1);
	dfs(d + 1, cnt + 1, min(x1, x[d]), min(y1, y[d]));
}

int main(){

	while(cin >> n >> m >> k){
		for(int i = 0; i < k; i++){
			scanf("%d %d", &r[i], &c[i]);
		}
		ans = 0;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				tot = 0;
				bool ok = 1;
				for(int l = 0; l < k; l++){
					if(i == r[l] && j == c[l]){
						ok = 0;
						break;
					}
					if(i >= r[l] && j >= c[l]){
						x[tot] = r[l];
						y[tot] = c[l];
						tot++;
					}

				}
				if(ok){
					dfs(0, 0, i, j);
					
				}

			}
		}
		cout<<ans<<endl;

	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/IMGavin/p/6505755.html