[P4198] Luo Gu building reconstruction (segment tree)

Portal

analysis

  Segment tree is ground by friction   first converted to slope left, then this problem can be converted into a modified value of each point, the output of the maximum number of prefixes

  Consider a query (l, r, x), l represents the maximum value of the foregoing is x, the maximum value of the prefix in the interval (l, r), it is necessary to output each inquiry (1, n, 0)

  Dp transfer equation with the idea of ​​asking split

  Provided mid = (l + r) / 2, mx (l, r) represents the maximum value of the r, l

  If x> mx (l, mid), then the left half of the maximum interval can not prefix exists, the result (l, r, x) is equal to (mid, r, x)

  If x <= mx (l, mid), then the query (l, r, x) can be split into (l, mid, x) with (mid + 1, r, mx (l, mid)).

  Noting (mid + 1, r, mx (l, mid)) This thing independent of x, it can be inserted on the process value when

  So for each interrogation (l, r, x) is a real need to find the site (l, mid, x) or (mid, r, x), each split half interval length, complexity can be done in logn

  As the value of each modified insert (mid + 1, r, mx (l, mid)) , the same method can be used, a total of logn intervals, each section of the processing complexity of logn, so as long ^ 2n complexity

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m,vr[maxn<<2];double mx[maxn<<2];
int que(int id,int l,int r,double L)
{
    if(l==r)return mx[id]>L?1:0;
    int mid=(l+r)>>1;
    if(L<=mx[id<<1])return que(id<<1,l,mid,L)+vr[id];
    else return que(id<<1|1,mid+1,r,L);
}
void fix(int id,int l,int r,int k,double v)
{
    if(l==r){mx[id]=v;return;}
    int mid=(l+r)>>1;
    k<=mid?fix(id<<1,l,mid,k,v):fix(id<<1|1,mid+1,r,k,v);
    mx[id]=max(mx[id<<1],mx[id<<1|1]);vr[id]=que(id<<1|1,mid+1,r,mx[id<<1]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;i++)
    scanf("%d%d",&x,&y),
    fix(1,1,n,x,double(y)/double(x)),
    printf("%d\n", that ( 1 , 1 , n, 0 )); 
}

 

Guess you like

Origin www.cnblogs.com/firecrazy/p/11780074.html