Bzoj 3333 Queue Plan tree array + segment tree

Source: Internet
Author: User

Topic: given a sequence. Each time you select a position, 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 because the number of the order and the previous number position relationship is not changed, so the inverse of these numbers will not change

For this position the number is larger than this number because the number of changes in position is smaller than the number of these numbers, so the inverse of these numbers will not change

The inverse logarithm of the number of the sort that changed the order in which the numbers started.

So we can do it. We use a tree-like array to count the logarithm of the sequence starting with each number and then set the minimum value of the segment tree maintenance interval by the size of the keyword.

For each inquiry p, we take out the minimum value in [P,n] a[x]. The a[x] is cleared to infinity. To A[x] The beginning of the reverse order to lose, continue to find, until a[p] is positive infinity

Each number will be found only 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&AMP;&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.