【汕头市选2014】数数

Description
行M 列的矩形上有K 个宝藏。统计包含至少1 个宝藏的子矩形数量。
Input
第1 行,3 个整数N;M;K。接下来K 行,每行2 个整数Xi,Yi,表示第i 个宝藏位于第Xi 行第Yj 列。
Output
1 个整数,表示所求的值除以(10^9 + 7) 的余数。
Sample Input
输入1:

2 2 1

1 1

输入2:

2 2 4

1 1

1 2

2 1

2 2
Sample Output
输出1:

4

输出2:

9
Data Constraint
• 对于20% 的数据,N,M <= 50;

• 对于50% 的数据,N,M <= 1000;

• 对于80% 的数据,N <=1000;M <= 10^9;

• 对于100% 的数据,1 <= N,M <= 10^9; 1 <= K <= 1000; 1 <= Xi <= N,1 <= Yi <= M。


The Solution

由于矩阵很大,但是宝藏分布是很稀疏的,只要做一遍离散化,暴力处理就好了

~~原谅我太懒了~~

CODE
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define fd(i,a,b) for (int i=a;i>=b;i--)
#define N 1005 
#define mo 1000000007

using namespace std;

struct node
{
	int x,y;
}a[N];

int n,m,k,Ans,Sum;

typedef long long ll;

bool cmp(node a,node b)
{
	return a.x < b.x;	
}

int read(int &n)
{
	char ch = ' ';
	int q = 0,w = 1;
	for (;(ch != '-') && ((ch < '0' || ch > '9'));ch = getchar());
	if (ch == '-') w = -1,ch = getchar(); 
	for (;ch >= '0' && ch <= '9';ch = getchar()) q = q * 10 + ch - 48;
	n = q * w;
	return n;
}


int main()
{
	freopen("count.in","r",stdin);
	freopen("count.out","w",stdout);
	read(n),read(m),read(k);	
	fo(i,1,k) read(a[i].x),read(a[i].y);
	sort(a + 1,a + k + 1,cmp);
	a[0].x = 0;
	a[k + 1].x = n + 1;
	fo(i,1,k)
	{
		int l = 0,r = m + 1;
		fd(j,i - 1,0)
		{
			Sum = (Sum + (ll)(a[i].y - l) * (r - a[i].y) % mo * (a[j + 1].x - a[j].x) % mo) % mo;
			if (a[j].y <= a[i].y) l = max(l,a[j].y);
			if (a[i].y <= a[j].y) r = min(r,a[j].y);
		}
		Ans = (Ans + (ll)Sum * (a[i + 1].x - a[i].x) % mo) %mo;
	}
    printf("%d\n" , Ans % mo);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值