pyy entire fleet segment tree

pyy entire fleet segment tree

Problem Description:

As we all know pyy when the squad leader, service to the people. PE one day, while physical education teacher has not come yet, pyy make first class of n students lined up. The teacher is not, the students began playing the phone. Stood in the front ranks of play phone, in front of fewer people who can not stand. So after another team was the last to go hide, but we have become obsessed with a cheat krypton hand travel, forget the teacher said front position vacancies to be filled requirements. Some students also occasionally asked to bow to the command team squad pyy, who came in front of his own best performance of the students is that such own peace of mind to boldly continue to play phone. Then the teacher came, the students receive good cell phone in negligible time. Looked everywhere is full of vacant team, a physical education teacher was furious and seize upon this opportunity to raise the prestige sports groups, limit pyy with the fastest time to rectify the team. Because it is a physical education teacher, and do not see the position of the team after the shift, the teacher only care team is neat no vacancies. The teacher gave the students a pyy a mobile power, and therefore can not use skills pyy "look ahead together." pyy brother mandatory pyy help you answer questions before the students and tell pyy after the teacher, the students can move at least how many teams make tidy.

Input Format

The first two acts of integers \ (n-, m (. 1 \ n-Le, m \ Le 1E5) \) , expressed \ (n-\) bits students, the teacher conducted prior to \ (m \) times trick.

The second line \ (n-\) by spaces separated integers \ (A_1, A_2, \ cdots A_N (. 1 \ Le a_i \ Le 1E7) \) , initially represented by the first team \ (I \) bits students Year standings (data guarantee there will be no repeat two grades).

Next \ (m \) row describes the behavior of the students, each line of a character \ (A \) or \ (S \) and an integer \ (X \) configuration. If it is \ (the X-A \) , said the year standings for the \ (x \) in front of the students to ask their performance is best to pyy Which students; if it is \ (the X-M \) , said the year standings as \ (x \) students at this time hid most end of the current team (there is no team to hide the tail end of the students).

Output Format

Before \ (m \) th operation for each interrogation students, student score grades sequentially outputs the interrogation position, separated by line feed. Ask students if there is no output \ (--1 \) .
The last line of output moving at least How many students so neat ranks.

Segment tree is relatively simple problem.

For the operation it wants to engage directly with the segment tree, to maintain an index tree line queue position, to \ (A \) operating us directly query \ ([1, pos [x ]] \) to, for \ ( M \) we modify the original location of a single point \ (INF \) , as modified in the tail \ (x \) can be.

The trouble is the last one to ask, first of all certainly can not count the number of (nonsense) space, but to use a length \ (n \) window sweep again, the answer is updated each time counting the number of students to be moved.Killed during examinations I could not think, that also sets a DP, I was too konjac up

All in the final query queue position status, write directly to violence query,\ (O (nlog_n) \) anyway will not TLE, In fact, can direct a \ (O (n) \) to query all the leaf nodes,Too lazy to write

#include <cstdio>
#include <algorithm>
#define MAXN 100010
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 n,m,s;
int a[MAXN],a_sort[MAXN];
int idx[MAXN];
int qlast;
int pos[MAXN*2];
#define INF 0x3f3f3f3f
#define sl (x<<1)
#define sr (x<<1|1)
int tre[MAXN*2*4];
void buildt(int x, int l, int r){
    tre[x]=INF;
    if(l==r) return;
    int mid=(l+r)>>1;
    buildt(sl, l, mid);
    buildt(sr, mid+1, r);
}
void change(int x, int l, int r, int pos, int val){
    if(l==r){
        tre[x]=val;
        return;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) change(sl, l, mid, pos, val);
    else change(sr, mid+1, r, pos, val);
    tre[x]=min(tre[sl], tre[sr]);
}
int query(int x, int l, int r, int ql, int qr){
    if(ql>qr) return INF;
    if(ql<=l&&r<=qr){
        return tre[x];
    }
    int mid=(l+r)>>1;
    int res=INF;
    if(ql<=mid) res=min(res, query(sl, l, mid, ql, qr));
    if(mid<qr) res=min(res, query(sr, mid+1, r, ql, qr));
    return res;
}
int cnt[MAXN];
int val[MAXN*2];
int main(){
    //freopen("queue.in", "r", stdin);
    //freopen("queue.out", "w", stdout);
    n=read(),m=read();
    for(int i=1;i<=n;++i) a[i]=a_sort[i]=read();
    sort(a_sort+1, a_sort+1+n);
    for(int i=1;i<=n;++i)
        idx[i]=lower_bound(a_sort+1, a_sort+1+n, a[i])-a_sort,
        pos[idx[i]]=i;
    buildt(1, 1, n*2);
    qlast=n+1;
    for(int i=1;i<=n;++i) change(1, 1, n*2, i, a[i]);
    while(m--) {
        char opt=getchar();
        while(opt!='A'&&opt!='M') opt=getchar();
        int t=read();
        if(opt=='A'){
            int tmp=lower_bound(a_sort+1, a_sort+1+n, t)-a_sort;
            int res=query(1, 1, n*2, 1, pos[tmp]-1);
            if(res==INF) puts("-1");
            else printf("%d\n", res);
        }else if(opt=='M'){
            int tmp=lower_bound(a_sort+1, a_sort+1+n, t)-a_sort;
            change(1, 1, n*2, qlast, t);
            change(1, 1, n*2, pos[tmp], INF);
            pos[tmp]=qlast;
            ++qlast;
        }else puts("ERRO");
    }
    for(int i=1;i<=n*2;++i) val[i]=query(1, 1, n*2, i, i);
    int cnt=0;
    int ans=0x3f3f3f3f;
    for(int i=1;i<=n;++i)
        if(val[i]!=INF) ++cnt;
    for(int i=2;i+n<=n*2;++i) {
        if(val[i-1]!=INF) --cnt;
        if(val[i+n-1]!=INF) ++cnt;
        ans=min(ans, n-cnt);
    }
    printf("%d", ans);
    return 0;
}

Guess you like

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