North School rail segment tree clearing

North School rail segment tree clearing

Description [title]
rail R country often be rebuilt.
R country is a country of slender, a total of n cities in a row, the capital city located on the 1st, adjacent to the railway connects the two cities.
Every new tracks, it must be from the capital began to build until one city, during which the railway will become the new version of the established
facilities, and the old facilities will be demolished. However, due to the country's engineers R brain is not functioning, between any two different versions of the railway can not even
connected, it is necessary to transfers.
Now you build the rail operation is given, from time to time on a small R will ask you, if randomly selected among the first cities to x of y city a
city bus destined for the capital, the number of transfers expectations of how many times?
All cities are connected by the same railway facilities initially.

[Input format
of the first row two integers n and m, the number of operations and represents the number of cities in
the next m rows, each row represents an operation
when the first number is 0, then the next of a number x, representatives from No. 1 city to x number of railway between the city rebuilt.
If the first number is 1, followed by two numbers x and y, representing small R asked a random selection of the city between the cities of x to the y-th City
city bus destined for the capital, transfer of expectations frequency.

[Output format]
For each inquiry, output a line on behalf of a real answer, to retain four decimal places

Easy to think of maintenance intervals and calculates the number of interchanges tree line with expectations, for each build, we will intervals \ ([1, x] \ ) coverage for \ (0 \) , for the remainder of the interval \ ([x + . 1, n-] \) , we found that the number of transfers change must be synchronized, so we subtracted interval \ (val [x + 1] -1 \) to (ie \ (x + 1 \) modify \ ( 1 \) ).

Note that the wording of the overlay mark decentralization

void push_down(int x, int l, int r){
    if(lazy_cover[x]){
        tre[sl]=tre[sr]=0;
        lazy[sl]=lazy[sr]=0;
        lazy_cover[sl]=lazy_cover[sr]=1;
        lazy_cover[x]=0;
    }
    if(lazy[x]==0) return;
    int mid=(l+r)>>1;
    tre[sl]+=lazy[x]*(mid-l+1);
    lazy[sl]+=lazy[x];
    tre[sr]+=lazy[x]*(r-(mid+1)+1);
    lazy[sr]+=lazy[x];
    lazy[x]=0;
}

Note the accuracy, JXOJ data is doublerunning out, so use double)

#include <cstdio>
#define MAXN 100010
#define sl (x<<1)
#define sr (x<<1|1)
using namespace std;
inline int read(){
    char ch=getchar();int s=0;
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s;
}
int tre[MAXN*4];
int lazy[MAXN*4];
bool lazy_cover[MAXN*4];
void push_down(int x, int l, int r){
    if(lazy_cover[x]){
        tre[sl]=tre[sr]=0;
        lazy[sl]=lazy[sr]=0;
        lazy_cover[sl]=lazy_cover[sr]=1;
        lazy_cover[x]=0;
    }
    if(lazy[x]==0) return;
    int mid=(l+r)>>1;
    tre[sl]+=lazy[x]*(mid-l+1);
    lazy[sl]+=lazy[x];
    tre[sr]+=lazy[x]*(r-(mid+1)+1);
    lazy[sr]+=lazy[x];
    lazy[x]=0;
}
void cover(int x, int l, int r, int cl, int cr){
    if(cl<=l&&r<=cr){
        tre[x]=0;
        lazy[x]=0;
        lazy_cover[x]=1;
        //printf("cover[%d,%d]=%d\n", l, r, tre[x]);
        return;
    }
    push_down(x, l, r);
    int mid=(l+r)>>1;
    if(cl<=mid) cover(sl, l, mid, cl, cr);
    if(mid<cr) cover(sr, mid+1, r, cl, cr);
    tre[x]=tre[sl]+tre[sr];
    //printf("cover[%d,%d]=%d\n", l, r, tre[x]);
}
void change(int x, int l, int r, int cl, int cr, int val){
    if(cl<=l&&r<=cr){
        tre[x]+=val*(r-l+1);
        lazy[x]+=val;
        return;
    }
    push_down(x, l, r);
    int mid=(l+r)>>1;
    if(cl<=mid) change(sl, l, mid, cl, cr, val);
    if(mid<cr) change(sr, mid+1, r, cl, cr, val);
    tre[x]=tre[sl]+tre[sr];
}
int query(int x, int l, int r, int ql, int qr){
    if(ql<=l&&r<=qr){
        //printf("query[%d,%d]=%d\n", l, r, tre[x]);
        return tre[x];
    }
    push_down(x, l, r);
    int mid=(l+r)>>1;
    int res=0;
    if(ql<=mid) res+=query(sl, l, mid, ql, qr);
    if(mid<qr) res+=query(sr, mid+1, r, ql, qr);
    //printf("query[%d,%d]=%d\n", l, r, res);
    return res;
}
inline void myswap(int &a, int &b){
    int t=a;
    a=b;
    b=t;
}
int n,m;
int s[MAXN],top;
int main(){
    scanf("%d %d", &n, &m);
    while(m--){
        int opt,x,y;
        scanf("%d", &opt);
        if(opt==0){
            scanf("%d", &x);
            cover(1, 1, n, 1, x);
            change(1, 1, n, x+1, n, 1-query(1, 1, n, x+1, x+1));
        }else{
            scanf("%d %d", &x, &y);
            if(x>y) myswap(x, y);
            printf("%.4lf\n", (double)query(1, 1, n, x, y)*1.0/(y-x+1));
        } 
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/santiego/p/11671101.html