其他分享
首页 > 其他分享> > CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)茶颜悦色(扫描线)

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;
}

标签:rt,OJ,颜悦色,long,maxn,扫描线,include,mx,define
来源: https://blog.csdn.net/zufesatoshi/article/details/99692603