Discrete segment tree

The main idea is to put right in front of discrete points ++, right after discrete point 1.

 

1. CF 610D Vika and Segments

Effect: a given segment $ $ n-coordinate axis parallel to the strip, the number of points required together represent

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head




const int N = 4e5+10;
int n, cas, cnt, tot;
struct _ {int l,r,h,v;} e[N];
int b[N], sum[N<<2];
int tag[N<<2];
void pu(int o, int l, int r) {
    if (tag[o]) sum[o] = b[r+1]-b[l];
    else sum[o] = sum[lc]+sum[rc];
}
void update(int o, int l, int r, int ql, int qr, int v) {
    if (ql<=l&&r<=qr) return tag[o]+=v,pu(o,l,r);
    if (mid>=ql) update(ls,ql,qr,v);
    if (mid<qr) update(rs,ql,qr,v);
    pu(o,l,r);
}
int main() {
    scanf("%d", &n);
    REP(i,1,n) {
        int x1, x2, y1, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if (x1>x2) swap(x1,x2);
        if (y1>y2) swap(y1,y2);
        --x1,--y1;
        e[++cnt]={y1,y2,x1,1};
        e[++cnt]={y1,y2,x2,-1};
        b[++tot]=y1,b[++tot]=y2;
    }
    sort(b+1,b+1+tot),tot=unique(b+1,b+1+tot)-b-1;
    sort(e+1,e+1+cnt,[](_ a,_ b){return a.h<b.h;});
    ll ans = 0;
    REP(i,1,cnt-1) {
        int l=lower_bound(b+1,b+1+tot,e[i].l)-b;
        int r=lower_bound(b+1,b+1+tot,e[i].r)-b-1;
        update(1,1,tot,l,r,e[i].v);
        ans += (ll)sum[1]*(e[i+1].h-e[i].h);
    }
    printf("%lld\n", ans);
}
View Code

 

2. CF 817F MEX Queries

Effect: Flip the interval, the interval assignment, seeking $ 0 $ leftmost position

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 2e5+10;
int n, cnt, T, tot;
ll b[N];
struct _ {
    int tag,v[2];
    void upd(int x,int y) {
        if (y==1) v[0]=cnt,v[1]=x;
        else v[0]=x,v[1]=cnt;
    }
    void flip() {swap(v[0],v[1]);tag^=1;}
} tr[N<<2];

void pd(int o, int l, int r) {
    if (tr[o].tag) {
        tr[lc].flip();
        tr[rc].flip();
        tr[o].tag = 0;
    }
    if (tr[o].v[0]==cnt) {
        tr[lc].upd(l,1);
        tr[rc].upd(mid+1,1);
    }
    if (tr[o].v[1]==cnt) {
        tr[lc].upd(l,0);
        tr[rc].upd(mid+1,0);
    }
}

void add(int o, int l, int r, int ql, int qr, int v) {
    if (ql<=l&&r<=qr) return tr[o].upd(l,v);
    pd(o,l,r);
    if (mid>=ql) add(ls,ql,qr,v);
    if (mid<qr) add(rs,ql,qr,v);
    tr[o].v[0]=min(tr[lc].v[0],tr[rc].v[0]);
    tr[o].v[1]=min(tr[lc].v[1],tr[rc].v[1]);
}
void flip(int o, int l, int r, int ql, int qr) {
    if (ql<=l&&r<=qr) return tr[o].flip();
    pd(o,l,r);
    if (mid>=ql) flip(ls,ql,qr);
    if (mid<qr) flip(rs,ql,qr);
    tr[o].v[0]=min(tr[lc].v[0],tr[rc].v[0]);
    tr[o].v[1]=min(tr[lc].v[1],tr[rc].v[1]);
}
void build(int o, int l, int r) {
    tr[o].upd(l,0);
    if (l!=r) build(ls),build(rs);
}

struct {char op;ll l,r;} q[N];
int main() {
    scanf("%d", &n);
    REP(i,1,n) { 
        int op;
        ll l,r;
        scanf("%d%lld%lld",&op,&l,&r);
        q[i].op=op,q[i].l=l,q[i].r=r;
        b[++cnt]=q[i].l;
        b[++cnt]=++q[i].r;
    }
    b[++cnt]=1,b[++cnt]=2e18;
    sort(b+1,b+1+cnt),cnt=unique(b+1,b+1+cnt)-b-1;
    build(1,1,cnt-1);
    REP(i,1,n) {
        q[i].l=lower_bound(b+1,b+1+cnt,q[i].l)-b;
        q[i].r=lower_bound(b+1,b+1+cnt,q[i].r)-b-1;
        if (q[i].op<=2) add(1,1,cnt-1,q[i].l,q[i].r,q[i].op==1);
        else flip(1,1,cnt-1,q[i].l,q[i].r);
        printf("%lld\n",b[tr[1].v[0]]);
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/uid001/p/11613983.html