1. 程式人生 > >【SDOI2009】【BZOJ1227】虔誠的墓主人

【SDOI2009】【BZOJ1227】虔誠的墓主人

Description

小W 是一片新造公墓的管理人。公墓可以看成一塊N×M 的矩形,矩形的每個格點,要麼種著一棵常青樹,要麼是一塊還沒有歸屬的墓地。當地的居民都是非常虔誠的基督徒,他們願意提前為自己找一塊合適墓地。為了體現自己對主的真誠,他們希望自己的墓地擁有著較高的虔誠度。一塊墓地的虔誠度是指以這塊墓地為中心的十字架的數目。一個十字架可以看成中間是墓地,墓地的正上、正下、正左、正右都有恰好k 棵常青樹。小W 希望知道他所管理的這片公墓中所有墓地的虔誠度總和是多少

Input

第一行包含兩個用空格分隔的正整數N 和M,表示公墓的寬和長,因此這個矩形公墓共有(N+1) ×(M+1)個格點,左下角的座標為(0, 0),右上角的座標為(N, M)。第二行包含一個正整數W,表示公墓中常青樹的個數。第三行起共W 行,每行包含兩個用空格分隔的非負整數xi和yi,表示一棵常青樹的座標。輸入保證沒有兩棵常青樹擁有相同的座標。最後一行包含一個正整數k,意義如題目所示。

Output

包含一個非負整數,表示這片公墓中所有墓地的虔誠度總和。為了方便起見,答案對2,147,483,648 取模。

Sample Input

5 6

13

0 2

0 3

1 2

1 3

2 0

2 1

2 4

2 5

2 6

3 2

3 3

4 3

5 2

2
Sample Output

6
HINT

圖中,以墓地(2, 2)和(2, 3)為中心的十字架各有3個,即它們的虔誠度均為3。其他墓地的虔誠度為0。 對於30%的資料,滿足1 ≤ N, M ≤ 1,000。對於60%的資料,滿足1 ≤ N, M ≤ 1,000,000。對於100%的資料,滿足1 ≤ N, M ≤ 1,000,000,000,0 ≤ xi ≤ N,0 ≤ yi ≤ M,1 ≤ W ≤ 100,000, 1 ≤ k ≤ 10。存在50%的資料,滿足1 ≤ k ≤ 2。存在25%的資料,滿足1 ≤ W ≤ 10000。

Source

智商不足,這題真的好難….
黃學長題解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define MAXN 100010
#define P 2147483648ll
#define lowbit(x)   (x&(-x))
#define GET (ch>='0'&&ch<='9')
using namespace std;
int n,m,k,w;
int sta[MAXN<<1
],cnt; LL c[MAXN<<1],C[MAXN][11],ans; int cntx[MAXN<<1],cnty[MAXN<<1],now[MAXN<<1]; void in(int &x) { char ch=getchar();x=0; while (!GET) ch=getchar(); while (GET) x=x*10+ch-'0',ch=getchar(); } struct node { int x,y; bool operator <(const node& a)const { return y==a.y?x<a.x:y<a.y; } }s[MAXN]; void add(int x,LL delta) { for (;x<=(w<<1);x+=lowbit(x)) (c[x]+=delta)%=P; } LL query(int x) { LL ret=0; for (;x;x-=lowbit(x)) (ret+=c[x])%=P; return ret; } int main() { in(n);in(m);in(w); for (int i=1;i<=w;i++) in(s[i].x),in(s[i].y),sta[++cnt]=s[i].x,sta[++cnt]=s[i].y; sort(sta+1,sta+cnt+1);cnt=unique(sta+1,sta+cnt+1)-sta; for (int i=1;i<=w;i++) s[i].x=lower_bound(sta+1,sta+cnt+1,s[i].x)-sta, s[i].y=lower_bound(sta+1,sta+cnt+1,s[i].y)-sta, cntx[s[i].x]++,cnty[s[i].y]++; in(k);cnt=0;C[0][0]=1; for (int i=1;i<=w;i++) { C[i][0]=1; for (int j=1;j<=min(i,k);j++) (C[i][j]=C[i-1][j-1]+C[i-1][j])%=P; } sort(s+1,s+w+1); for (int i=1;i<=w;i++) { if (i>
1&&s[i].y==s[i-1].y) cnt++,(ans+=(query(s[i].x-1)-query(s[i-1].x))*C[cnt][k]*C[cnty[s[i].y]-cnt][k])%=P; else cnt=0; now[s[i].x]++; LL t=C[now[s[i].x]][k]*C[cntx[s[i].x]-now[s[i].x]][k]-C[now[s[i].x]-1][k]*C[cntx[s[i].x]-now[s[i].x]+1][k]; t%=P;add(s[i].x,t); } (ans+=P)%=P;cout<<ans<<endl; }