Solution to a problem DTOJ # 1438. Dwarf queue (lineup)

Welcome to My Luogu Space .


[Title] effect

There \ (n-\) a height of \ ([1, \ n] \) and different people in a queue.

There are two modes of operation:

  1. Let the position \ (x, \ y \) who swap places.
  2. Given a range of \ ([L, \ R & lt] \) , asking everyone within the height range lined up whether a contiguous sequence.

Two output operation request.


【answer】

Segment tree .

A very simple operation.

For Operation II:

We found that the height \ ([l, \ r] \) of a human consensus \ (k = (r-l + 1) \) a.

We just need to find the height in this range, and stood in the far left and far right of the people of coordinates.

\ ((\) The right coordinates \ (- \) left coordinates \ (+ 1) \) value if equal to \ (k \) , then that \ (k \) individuals just lined up a continuous sequence.

If it is larger than \ (k \) individual is inserted among a number of other people, there can be no smaller than appears.

So we need to support the maximum and minimum coordinates inquiries.

To build a tall target for the next segment tree, height value corresponding to the coordinates, the coordinates of the query interval maximum and minimum values.


[Code]

// output format !!
// long long !!
#include <bits/stdc++.h>
#define H puts("HYX")
#define ls (x<<1)
#define rs (x<<1|1)
const int MAXN = 200000+10;
using std::max; using std::min; using std::swap;
struct TREE{int Max, Min;}t[MAXN*4];

int n, m, h[MAXN], loc[MAXN], L, R;

void build(int x, int l, int r){
    if(l == r) return t[x].Max = t[x].Min = loc[l], void();
    int mid = (l+r)>>1;
    build(ls, l, mid), build(rs, mid+1, r);
    t[x].Max = max(t[ls].Max, t[rs].Max);
    t[x].Min = min(t[ls].Min, t[rs].Min);
}
void query(int x, int l, int r, int ql, int qr){
    if(ql<=l && r<=qr){
        L = min(L, t[x].Min);
        R = max(R, t[x].Max);
        return;
    }
    int mid = (l+r)>>1;
    if(ql <= mid) query(ls, l, mid, ql, qr);
    if(qr > mid) query(rs, mid+1, r, ql, qr);
}
void modify(int x, int l, int r, int p, int v){
    if(l == r) return t[x].Max = t[x].Min = v, void();
    int mid = (l+r)>>1;
    if(p <= mid) modify(ls, l, mid, p, v);
    else modify(rs, mid+1, r, p, v);
    t[x].Max = max(t[ls].Max, t[rs].Max);
    t[x].Min = min(t[ls].Min, t[rs].Min);
}
int main(){
    scanf("%d%d", &n, &m);
    for(int i=1; i<=n; ++i) scanf("%d", h+i), loc[h[i]] = i;
    build(1, 1, n);
    while(m--){
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1){
            modify(1, 1, n, h[y], x);
            modify(1, 1, n, h[x], y);
            swap(h[x], h[y]), swap(loc[h[x]], loc[h[y]]);
        }
        else{
            L = 1e9, R = 0;
            query(1, 1, n, x, y);
            if(R-L+1 == y-x+1) puts("YES");
            else puts("NO");
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/bosswnx/p/10988258.html