Topic: Given a sequence, select a position each time, the position after all the number is less than equal to draw out, sort, and then plug back, for each operation after the reverse logarithm
First of all we operate for this position in front of the number due to the number of orders and the previous number position relationship is not changed, so the inverse of these numbers will not change
For this position, the number is smaller than this number because the number of changes in the position is less than the number, so the inverse of these numbers will not change.
In the final analysis, the inverse logarithm of the number of sorts changes the reverse order that starts with these numbers.
So we can do it. We use a tree-like array to count the number of reverse-order logarithm starting with each digit and then establish a segment tree maintenance interval minimum with the size of the original number as the keyword.
For each inquiry p, we take out [p,n] The minimum value a[x], will a[x] clear to the positive infinity, the reverse order to the beginning of the a[x] to lose, continue to find, until a[p] is positive infinity
Each number will only be found 1 times so the averaging complexity O (NLOGN)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 500500# Define LS tree[p].lson#define rs tree[p].rsonusing namespace std;struct abcd{int lson,rson;int *num;} Tree[m<<1];int tree_tot;int n,m,tot,a[m];p air<int,int>b[m];int c[m],f[m];long long ans;int* _min (int *x, int *y) {return *x>=*y?y:x;} void Build_tree (int p,int x,int y) {int mid=x+y>>1;if (x==y) {Tree[p].num=a+mid;return;} Ls=++tree_tot;rs=++tree_tot; Build_tree (Ls,x,mid); Build_tree (rs,mid+1,y); Tree[p].num=_min (tree[ls].num,tree[rs].num);} int* Get_ans (int p,int x,int y,int l,int r) {int mid=x+y>>1;if (X==L&&Y==R) return tree[p].num;if (R<=mid Return Get_ans (Ls,x,mid,l,r), if (L>mid) return Get_ans (Rs,mid+1,y,l,r), Return _min (Get_ans (ls,x,mid,l,mid), get_ Ans (Rs,mid+1,y,mid+1,r));} inline void Modify (int p,int x,int y,int pos) {int mid=x+y>>1;if (x==y) return; if (Pos<=mid) Modify (Ls,x,mid,pos) ; elsemodify (Rs,mid+1,y,pos); Tree[p].num=_min (Tree[ls].Num,tree[rs].num);} inline void Update (int x) {for (; x<=tot;x+=x&-x) c[x]++;} inline int Get_ans (int x) {int re=0;for (; x;x-=x&-x) Re+=c[x];return re;} int main () {int i,p;cin>>n>>m;for (i=1;i<=n;i++) scanf ("%d", &b[i].first), B[i].second=i;sort (B+1, B+N+1); for (i=1;i<=n;i++) {if (i==1| | B[i].first!=b[i-1].first) ++tot;a[b[i].second]=tot;} for (i=n;i;i--) Update (A[i]), Ans+=f[i]=get_ans (a[i]-1); Build_tree (0,1,n);p rintf ("%lld\n", ans); for (i=1;i<=m;i++) {int *temp;scanf ("%d", &p); if (a[p]!=0x3f3f3f3f) does {Temp=get_ans (0,1,n,p,n); ans-=f[temp-a];*temp=0x3f3f3f3f; Modify (0,1,N,TEMP-A);} while (temp!=a+p);p rintf ("%lld\n", ans);}}
Bzoj 3333 Queue Plan tree array + segment tree