CCPC-Wannafly & Comet OJ 夏季歡樂賽(2019)茶顏悅色(掃描線)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define endl '\n'
#define clr(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define PB push_back
#define POP pop_back
const double pi = acos(-1);
const int maxn = 2e5 + 10;
const int maxm = (maxn<<5) + 5;
const ll mod = 1e9 + 7;
const int hash_mod = 19260817;
int n, k, cnt, ans;
struct node{
    int x, ly, ry, flag;
    friend bool operator < (node a, node b){
        return a.x == b.x ? a.flag > b.flag : a.x < b.x;
    }
}line[maxn];
int Y[maxn], mx[maxn<<2], lazy[maxn<<2];
void pushdown(int rt){
    if(lazy[rt]){
        lazy[rt<<1] += lazy[rt]; lazy[rt<<1|1] += lazy[rt];
        mx[rt<<1] += lazy[rt]; mx[rt<<1|1] += lazy[rt];
        lazy[rt] = 0;
    }
}
void update(int rt, int l, int r, int L, int R, int val){
    if(L <= l && r <= R){
        mx[rt] += val;
        lazy[rt] += val;
        return;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(lson, L, R, val);
    if(R > mid) update(rson, L, R, val);
    mx[rt] = max(mx[rt<<1], mx[rt<<1|1]);
}
int main()
{
    //freopen("E://one.txt","r",stdin); //輸入重定向,輸入數據將從in.txt文件中讀取
    //freopen("E://oneout.txt","w",stdout); //輸出重定向,輸出數據將保存在out.txt文件中
    int x, y;
    scanf("%d %d", &n, &k);
    for(int i = 1 ; i <= n ; ++ i){
        scanf("%d %d", &x, &y);
        line[++cnt] = node{x, y, y + k, 1};
        Y[cnt] = y;
        line[++cnt] = node{x + k, y, y + k, -1};
        Y[cnt] = y + k;
    }
    sort(Y + 1, Y + 1 + cnt);
    int len = unique(Y + 1, Y + 1 + cnt) - Y - 1;
    sort(line + 1, line + 1 + cnt);
    for(int i = 1 ; i <= cnt ; ++ i){
        int l = lower_bound(Y + 1, Y + 1 + len, line[i].ly) - Y;
        int r = lower_bound(Y + 1, Y + 1 + len, line[i].ry) - Y;
        //cout << l << ' ' <<r <<endl;
        update(1, 1, len, l, r, line[i].flag);
        //cout << mx[1] << endl;
        ans = max(ans, mx[1]);
    }
    cout << ans;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章