Bzoj4636] [column number of Tacca [dynamic prescription segment tree]

bzoj4636 konjac specific sequence

bzoj4636

DCrusher has a number of columns, the initial values ​​are 0, N times operation, each time the number of columns [a, b) in this section, all smaller than k number to k, he wants to know the number of columns N operations all elements and.

The first line of an integer N, then there are N rows, each row of three positive integers a, b, k.
N <= 40000, a, b , k <= 10 ^ 9

A template?

== dynamic prescription and maintenance mark after completing all operations within this range are certainly all equal to the maximum number that is equivalent to a k to make a mark

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Max(x,y) ((x)>(y)?(x):(y))
#define lson (o<<1)
#define rson (o<<1|1)
const int N=40000+10,M=1e9,inf=0x3f3f3f3f;
int n,m,rt,tot;
ll ans=0;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct node{int ls,rs,dat;}t[N<<2];

void upd(int &o,int l,int r,int x,int y,int k){
    if(l>y||r<x) return;
    if(!o) o=++tot;
    if(x<=l&&r<=y){t[o].dat=Max(t[o].dat,k);return;}
    int mid=l+r>>1;
    upd(t[o].ls,l,mid,x,y,k),upd(t[o].rs,mid+1,r,x,y,k);
}
void query(int o,int l,int r){
    if(!t[o].ls&&!t[o].rs){ans+=(ll)(r-l+1)*t[o].dat;return;}
    if(t[o].ls) t[t[o].ls].dat=Max(t[t[o].ls].dat,t[o].dat);
    if(t[o].rs) t[t[o].rs].dat=Max(t[t[o].rs].dat,t[o].dat);
    int mid=l+r>>1;
    if(t[o].ls) query(t[o].ls,l,mid);
    if(t[o].rs) query(t[o].rs,mid+1,r);
    if(!t[o].ls) ans+=(ll)(mid-l+1)*t[o].dat;
    if(!t[o].rs) ans+=(ll)(r-mid)*t[o].dat;
}

int main(){
    freopen("in.txt","r",stdin);
    //freopen("and.out","w",stdout);
    rd(n);
    for(int i=1,x,y,k;i<=n;++i){
        rd(x),rd(y),rd(k);
        if(x<=--y) upd(rt,1,M,x,y,k);
    }
    query(rt,1,M);
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11461449.html