线段树离散化

主要思路是在离散化前把右端点++, 离散化后右端点-1.

1. CF 610D Vika and Segments

大意: 给定$n$条与坐标轴平行的线段, 求一共占了多少点

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

2. CF 817F MEX Queries

大意: 区间翻转,区间赋值,求最左侧的$0$的位置

#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]]);
    }
}

原文地址:https://www.cnblogs.com/uid001/p/11613983.html

时间: 2024-10-08 13:33:21

线段树离散化的相关文章

POJ_2528 Mayor&#39;s poster(线段树+离散化)

题目请点我 题解: 这道题与之前的题目相比重点在于一个映射的预处理,题目所给的区间达到10000000,而最多只有10000个点,如果直接建树的话太过于空旷.把这些区间的左右节点一一对应,最多有4×10000个点,远小于之前的10000000,而且区间之间的对应关系也不会改变. 举个例子: 区间:[2,6],[4,8],[6,10] 我们进行下面对应: 2 4 6 8 10 1 2 3 4 5 则原区间变为[1,3],[2,4],[3,5].可以发现它们之间的覆盖关系并没有改变,但是却紧凑了很多

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

hdu1828 Picture(线段树+离散化+扫描线)两种方法

C - Picture Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

hdu 5124 lines (线段树+离散化)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 620    Accepted Submission(s): 288 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

poj2528 线段树+离散化

1 //Accepted 1960K 110MS 2 //线段树+离散化 3 //把所有的坐标排序,从小到大编号,建立线段树 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 /** 1

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj2528--Mayor&#39;s posters(线段树+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41785   Accepted: 12164 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

【线段树+离散化】POJ2528 Mayor&#39;s posters

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 64939   Accepted: 18770 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post