Sort HEOI2016 / TJOI2016 binary tree line determination +

  LINK: sort  this problem very good point I have no idea if I was the provincial election this question I'm afraid I can not think in addition to the analog-half can also segment tree. . .

Write a little too good to consider ex violence apparently every time we are asked to give a direct sort of terrain can ignore ignore all direct sort. Complexity mnlogn 30 minutes hand.

Consider more excellent practice every sort bucket sort is O (n) overall complexity nm 50 minutes hand

Do not consider the problem continues to simulate the sort is certainly not enough to change how the model is the crux of the problem.

However Goose hard to think of ideas to guide solution at 01 sequence segment tree is easily modified to support the sort of range of range query. Consider again the answer is monotonic from the numerical size of the answer though it is certain, but apparently monotonic (though a bit far-fetched

But we can continue to narrow ranges in order to find answers to the answer which is the key, range from the point of view of the answer is indeed half. To find ways to continue to narrow the range of answers, how to do?
Consider making a good final sequence queue> = mid to 1 and 0 otherwise then we realize the whole operation is actually the reverse of 0 1 constantly sorting. And finally asked a certain position is 1. Because> = Current answer.

If the answer is larger then consider ordering after the last inquiry of 0 1 1 position must not shrink so our success is determined so as to achieve a range of effects mid-half 01 string feasible to sort out just fine using the above method.

Finished code may force recently dropped a lot of code when energy is not enough to fight it.

//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<cctype>
#include<utility>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<stack>
#include<string>
#include<cstring>
#define INF 1000000000
#define ll long long
#define db double
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define sum(x) t[x].sum
#define cnt(x) t[x].cnt
#define l(x) t[x].l
#define r(x) t[x].r
#define tag(x) t[x].tag
#define op(i) s[i].op
#define x(i) s[i].x
#define y(i) s[i].y
#define zz p<<1
#define yy p<<1|1
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int MAXN=100010;
int n,m,pos,sum0,sum1;
int a[MAXN],b[MAXN];
struct wy
{
    int l,r;
    int sum,cnt;
    int tag;
}t[MAXN<<2];
struct data{int op,x,y;}s[MAXN];
inline void build(int p,int l,int r)
{
    l(p)=l;r(p)=r;tag(p)=-1;
    if(l==r){sum(p)=b[l];cnt(p)=b[l]^1;return;}
    int mid=(l+r)>>1;
    build(zz,l,mid);
    build(yy,mid+1,r);
    sum(p)=sum(zz)+sum(yy);
    cnt(p)=cnt(zz)+cnt(yy);
}
inline void pushdown(int p)
{
    int mid=(l(p)+r(p))>>1;
    if(tag(p)==1)
    {
        sum(zz)=(mid-l(p)+1);
        cnt(zz)=0;
        sum(yy)=r(p)-mid;
        cnt(yy)=0;
        tag(zz)=tag(yy)=1;
        tag(p)=-1;return;
    }
    sum(zz)=0;
    cnt(zz)=(mid-l(p)+1);
    sum(yy)=0;
    cnt(yy)=r(p)-mid;
    tag(yy)=tag(zz)=2;
    tag(p)=-1;return;
}
inline void change(int p,int l,int r,int x)
{
    if(l>r)return;
    if(l<=l(p)&&r>=r(p))
    {
        if(x==1)
        {
            sum(p)=r(p)-l(p)+1;
            cnt(p)=0;tag(p)=1;
            return;
        }
        cnt(p)=r(p)-l(p)+1;
        sum(p)=0;tag(p)=2;
        return;
    }
    if(tag(p)!=-1)pushdown(p);
    int mid=(l(p)+r(p))>>1;
    if(l<=mid)change(zz,l,r,x);
    if(r>mid)change(yy,l,r,x);
    sum(p)=sum(zz)+sum(yy);
    cnt(p)=cnt(zz)+cnt(yy);
}
inline void ask(int p,int l,int r)
{
    if(l<=l(p)&&r>=r(p))
    {
        sum0+=cnt(p);
        sum1+=sum(p);
        return;
    }
    if(tag(p)!=-1)pushdown(p);
    int mid=(l(p)+r(p))>>1;
    if(l<=mid)ask(zz,l,r);
    if(r>mid)ask(yy,l,r);
    return;
}
inline int check(int x)
{
    for(int i=1;i<=n;++i)b[i]=a[i]>=x?1:0;
    build(1,1,n);
    for(int i=1;i<=m;++i)
    {
        sum0=sum1=0;
        ask(1,x(i),y(i));
        if(!op(i))//0升序
        {
            //cout<<x(i)<<' '<<x(i)+sum0-1<<endl;
            //cout<<x(i)+sum0<<' '<<y(i)<<endl;
            change(1,x(i),x(i)+sum0-1,2);
            change(1,x(i)+sum0,y(i),1);
        }
        else
        {
            change(1,x(i),x(i)+sum1-1,1);
            change(1,x(i)+sum1,y(i),2);
        }
    }
    sum0=sum1=0;
    ask(1,pos,pos);
    return sum1;
}
int main()
{
    //freopen("1.in","r",stdin);
    n=read();m=read();
    for(int i=1;i<=n;++i)a[i]=read();
    for(int i=1;i<=m;++i)
    {
        int op,x,y;
        op=read();x=read();y=read();
        s[i]=(data){op,x,y};
    }
    pos=read();
    int l=1,r=n;
    while(l+1<r)
    {
        int mid=(l+r)>>1;
        if(check(mid))l=mid;
        else r=mid;
    }
    if(check(r))printf("%d\n",r);
    else printf("%d\n",l);
    return 0;
}
View Code

The last review of this question once again the correctness of such practices 1 we seem to find a better way to solve this problem in addition to analog

2 forced to consider monotonic answers found answers range is clearly monotone find ways to reduce the range until you find the answer.

Method narrow range of accuracy for the 3> = 1, then as digital answer is 0 otherwise considered final sequence we can be abstracted into a string where the bit 0 is 1 then the final answer. Every time we are sort of in front of the 1 discharged or discharged back eventually form a final sequence, and thus we follow the example of utilization identified a string of 01 as long as the last sort of 01 series has all the answers then the current position is 1 mid legitimate then it is possible to continue to find that half of success and unsuccessful border boundary successful we are looking for the answer, the correctness can be understood as equivalent method is clearly right to judge the correctness apparently monotonous (the answer can be done the answer to that is smaller than the number of 1s more can be done. Therefore the right way. so he successfully solved the problem that seemingly can not be resolved.

Guess you like

Origin www.cnblogs.com/chdy/p/11424426.html