Codeforces Global Round 7 E. Bombs

  Race thinking or feeling is not very clear ah. For this first question, points race made a mistake, one false guessing algorithm for the problem may be in front of cf question is relatively simple, it is easy to develop the habit of guessing conclusion, this title is also the first to guess the false conclusion and then wrote a half hour wa once, I feel very influence the next effort, the second is for the idea of ​​the problem, I have always considered to be an iterative solution, but I've finally I need to do to come out of the operation, and has card He lived, in fact, there is no need to continue writing, and later think are garbage time, in fact, should be timely launch, think about other ideas.

  The first is the main idea of ​​this problem, we tried the simulation, you will find the answer once before and once after the answer is difficult to quickly transfer, although only the addition of a bomb, but can not guarantee what its subsequent impact is after thinking it over iteration should think about the questions correctly to give up. The main idea of ​​this problem is because the difference between the regressive answer, the answer is generally o (n), you have to do is check the current answer is likely to x, if you can quickly judge the feasibility of this, then the complexity of the issues come down.

  How to determine whether the answer may be x, x is greater than you would think the answer should be blown up, for there will be a number greater than x, there is a pattern:

    If he is to check first from the right is greater than x, it has the right of at least one bomb.

    If he is to check the second from the right is greater than x, it has the right of at least two bombs.

    If he is a third larger than the check from the right, it has the right at least three bombs.

  You will find such a thing, all you have to do is to check whether the conditions are met. But you find that it can actually be iterative, sentenced for x feasible, and sentenced to x-1 is iterative. You will find with a location, from sentence to sentence if x x-1, it is much larger than a digital x-1 and a bomb it.

  We opened a segment tree, each memory location (x is greater than the right side of the current number - the number of bombs on the right), is less than 0, the answer you need minus 1, and no time zone update just fine.

#include<iostream>
#include<string>
#include<string.h>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn=3e5+10;
int tree[maxn<<2],lazy[maxn<<2],a[maxn],p[maxn],tong[maxn];
void pushdown(int rt)
{
    tree[lson]+=lazy[rt];
    tree[rson]+=lazy[rt];
    lazy[lson]+=lazy[rt];
    lazy[rson]+=lazy[rt];
    lazy[rt]=0;
}
void pushup(int rt)
{
    tree[rt]=max(tree[lson],tree[rson]);
}
void update(int L,int R,int x,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        tree[rt]+=x;
        lazy[rt]+=x;
        return;
    }
    pushdown(rt);
    int mid=(l+r)/2;
    if(L<=mid)
        update(L, R, x,l, mid, lson);
    if(mid<R)
        update(L, R, x,mid+1, r, rson);
    pushup(rt);
}
int q(int L,int R,int l,int r,int rt)
{
    pushdown(rt);
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    int mid=(l+r)/2;
    int ans=0;
    if(L<=mid)
        ans=max(ans,q(L, R, l, mid, lson));
    if(mid<R)
        ans=max(ans,q(L, R, mid+1, r, rson));
    return ans;
}
inline int lowbit(int x){
    return x&(-x);
}
int n;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        tong[a[i]]=i;
    }
    for(int i=1;i<=n;i++)
        cin>>p[i];
    int now=n;
    update(1,tong[n], 1, 1, n ,1);
    for(int i=1;i<=n;i++)
    {
        cout<<now<<" ";
        if(i==n)
            break;
        update(1, p[i], -1, 1, n, 1);
        while(q(1, n, 1, n, 1)<=0)
        {
            now--;
            update(1,tong[now], 1, 1, n, 1);
        }
    }
    
}

 

Guess you like

Origin www.cnblogs.com/King-of-Dark/p/12538621.html