您的位置:首页 > 其它

【汕头市选2014】数数

2018-02-01 20:21 162 查看


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
;

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: