P2154 [SDOI2009]虔誠的墓主人 樹狀數組
阿新 • • 發佈:2019-02-20
fir 計算 fin alt nod emp bool cer with
https://www.luogu.org/problemnew/show/P2154
題意
在一個坐標系中,有w(1e5)個點,這個圖中空點的權值是正上,正下,正左,正右各取k個的排列組合情況。
計算整個圖的空點權值和
思路
由於每個點的坐標是1e9級別的,所以需要先離散化。
我們考慮w個點,先按x軸排序,按y軸次排序。
從左到右枚舉這w個點,
對於一個點的x值的影響,我們把它加到樹狀數組中做前綴和,
對於y值的影響,我們直接通過i和i+1兩個點的y值計算答案。
#include <algorithm> #include <iterator> #includeView Code<iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include<queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 來了老弟 \(‘?‘) > ⌒ヽ / へ\ / / \\ ? ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / ‘ノ ) L? */ usingnamespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const ll mod = 2147483648; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar(); while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 2e5+9; struct node { int x,y; }p[maxn]; bool cmp(node a,node b){ if(a.x == b.x) return a.y < b.y; return a.x < b.x; } vector<int>v; int getid(int x){ return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } ll c[maxn][20]; ll sum[maxn],cntx[maxn],cnty[maxn]; int lowbit(int x){ return x & (-x); } void add(int x,ll s){ while(x < maxn){ sum[x] = ((sum[x] + s) % mod + mod) % mod;; x += lowbit(x); } } ll getsum(int x){ ll res = 0; while(x > 0){ res = ((res + sum[x])%mod + mod )% mod; x -= lowbit(x); } return res; } ll Left[maxn],le_num[maxn]; int main(){ int n,m,k; int tot; scanf("%d%d%d", &n, &m, &tot); for(int i=1; i<=tot; i++) { scanf("%d%d", &p[i].x, &p[i].y); v.pb(p[i].x); v.pb(p[i].y); } scanf("%d", &k); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); c[0][0] = 1; for(int i=1; i<200009; i++){ for(int j=0; j<=min(i, k); j++){ if(j==0||j==i) c[i][j] = 1; else c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod; } } sort(p+1, p+1+tot,cmp); for(int i=1; i<=tot; i++){ p[i].x = getid(p[i].x); p[i].y = getid(p[i].y); cntx[p[i].x]++; cnty[p[i].y]++; } int colnum = 0; ll ans = 0; for(int i=1; i<=tot; i++){ if(p[i].x != p[i-1].x) colnum = 0; colnum++; int le = p[i].y; le_num[le] ++; ll val = 0; if(le_num[le] >= k && cnty[le] - le_num[le] >= k){ val = c[le_num[le]][k] * c[cnty[le] - le_num[le]][k]%mod; } add(le, val - Left[le]); Left[le] = val; if(p[i+1].x == p[i].x &&colnum >= k && cntx[p[i].x] - colnum >= k) { ans = (ans + c[colnum][k] * c[cntx[p[i].x] - colnum][k] % mod *(((getsum(p[i+1].y - 1) - getsum(p[i].y))%mod+mod)%mod)%mod)%mod; } } printf("%lld\n", ans); return 0; }
P2154 [SDOI2009]虔誠的墓主人 樹狀數組