其他分享
首页 > 其他分享> > P2154 [SDOI2009]虔诚的墓主人 树状数组

P2154 [SDOI2009]虔诚的墓主人 树状数组

作者:互联网

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>
#include  <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ノ

*/

using namespace 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;
}
View Code

 

标签:P2154,le,树状,int,ll,maxn,SDOI2009,include,mod
来源: https://www.cnblogs.com/ckxkexing/p/10404089.html