7.20T1

Sort (sort)
【Problem Description】
There are n individuals in turn standing in front of a small A. A small turn this will be n m individual operations.
Each operation to select a location k, all these n individual height is less than equal to the current location k
A person's height who brought out from the ranks, and then follow the rise from low to high in the order from left to right to insert
These people among the original position.
A small reverse sequence of these n individual height constitute very interested. A small now want to know every
The number after the reverse sequence of operations.
[Input Format]
The first line of two integers n and m, and represents the number of operands.
The next line n integers ai, represents an initial state of each person's height from left to right.
Next, a number of m lines each, k represents this operation.
[Output format]
The output common line m + 1, line 1 represents the number of reverse when not in operation.
Except the first i-th row i-1 represents a logarithmic sequence after reverse operations.
[Sample input]
6 2
160 163 164 161 167 160
2
3
[Sample output]
6
3
1
Sample [explain]
For the first time brought out 160,163,161,160
After the operation sequence of 160 160,164,161,167,163
The second brought out 160,160,164,161,163
After the sequence of operations is 160 160,160,161,167,163
【data range】
For 30% of the data, n, m≤500
For 60% of the data, n, m≤1000
Data for 100%, n, m≤300000,1≤k≤n ,, 1≤ai≤10 ^ 9
 
sol: Fancy delivery points , probably before the current size we do not have control, because they do not affect the answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const ll N=300005;
ll n,m;
struct Node
{
    ll id,num,val;
}a[N];
inline bool cmpnum(Node a,Node b){return a.num<b.num;}
inline bool cmpid(Node a,Node b){return a.id<b.id;}
struct BIT
{
    ll S[N<<1];
    #define lowbit(x) ((x)&(-x))
    inline void Ins(int x,int v)
    {
        while(x<=n)
        {
            S[x]+=v; x+=lowbit(x);
        }
    }
    inline int Que(int x)
    {
        ll ans=0;
        while(x>0)
        {
            ans+=S[x]; x-=lowbit(x);
        }
        return ans;
    }
}T,BT;
signed main()
{
    freopen("sort.in","r",stdin);
    freopen("sort.out","w",stdout);
    ll i,cv=0,ans=0,now=0,oo;
    R(n); R(m);
    for(i=1;i<=n;i++) R(a[a[i].id=i].num);
    sort(a+1,a+n+1,cmpnum);
    a[1].val=++cv;
    for(i=2;i<=n;i++)
    {
        if(a[i].num==a[i-1].num) a[i].val=cv; else a[i].val=++cv;
    }
    sort(a+1,a+n+1,cmpid);
    for(i=n;i>=1;i--)
    {
        ans+=(oo=T.Que(a[i].val-1));
        BT.Ins(a[i].val,oo);
        T.Ins(a[i].val,1);
    }
    Wl(ans);
    for(i=1;i<=m;i++)
    {
        ll pos; R(pos);
        if(now<a[pos].val)
        {
            ans-=(BT.Que(a[pos].val)-BT.Que(now)); now=a[pos].val;
        }
        Wl(ans);
    }
    return 0;
}
/*
input
6 2
160 163 164 161 167 160
2
3
output
6
3
1
*/
View Code

 

Guess you like

Origin www.cnblogs.com/gaojunonly1/p/11222279.html