Codeforces Round #220 (Div. 2) D tree-like array && two-part

Source: Internet
Author: User

/* Topic */


Test instructions: Given to n,m, and then an array containing the number of M nnum, the array default from small to large, then N operations, enter a number x, if x is 0, add 0 to the end of the string, if X is 1, add 1 to the end of the string, if X is 1, then the string subscript and Nnum the elements in the array are equal to the deletion, the string is empty at first, ask you what is in the last string, if it is empty, the output POOR STACK

This topic see this operation is generally easy to associate with the line tree, tree-like array, at the beginning I built a tree-like array, but timed out, after all, operation a lot, and in the deletion operation, if the Nnum array is large, equivalent to the complexity of 10^12, can only be optimized, is generally used two points, directly to the string of whether a location is empty to establish a tree array, every time add in the end, needless to say, the key is to delete, first delete, to the first step to find out the last one to delete, such as the length of the string is 7, and the Nnum array has a 8, in fact, 8 this position does not need to delete, But this still time out, so think of in the tree array to delete the time also need two points, but wrote has been wrong, later borrowed from the discovery of Jiege, originally WA here, such as the current string is 01010, need to delete the 1,3,4 position character, to turn, when you delete position 1th, The string becomes 1010, you are in the tree array is to delete the state of the 1 node, so the original 3rd position in the current becomes the 2nd position, when you delete 3rd number becomes 110, the original 4th position became 2nd position, so every deletion, the next bidding clubs change, but nothing, Found the previous operation several times and your original subscript is less than the current reduction of a few, so nnum[i]-I actually is the current need to delete the element in the tree array position


int N,m;int c[1000000 + 55];int nnum[1000000 + 55];int aa[1000000 + 55];int sum[1000000 + 55];int len;void init () {memset ( C,0,sizeof (c)); Memset (aa,-1,sizeof (AA));} bool Input () {while (cin>>n>>m) {for (int i=0;i<m;i++) Cin>>nnum[i];return false;} return true;} int lowbit (int x) {return x& (-X);} void Add (int i,int val) {while (I <= N) {c[i] + = Val;i + lowbit (i);}} int get_sum (int i) {int sum = 0;while (i > 0) {sum + = c[i];i-= Lowbit (i);} return sum;} void Binary_find (int pos,int id) {int l = 1;int r = n;while (L <= r) {int mid = (L + r) >>1;int ret = Get_sum (mid); if (aa[mid] = = 1) {if (ret >= Nnum[pos]-id) r = mid-1;else L = mid + 1;} else if (ret = = Nnum[pos]-ID) {Add (mid,-1); Aa[mid] = -1;len--;break;} else if (Ret > Nnum[pos]-id) r = Mid-1;else if (Ret < NNUM[POS]-id) L = mid + 1;}} void Cal () {int q = N;len = 0;int cnt = 1;while (q--) {int type;cin>>type;if (type = =-1) {if (len = = 0) continue;if (nnu M[0] > Len) continue;int now = Lower_bound (Nnum,nnum + M,len)-nnum;for (int i=0;i<=now;i++) binary_find (i,i);} else {aa[cnt] = Type;add (cnt,1); cnt++;len++;}} if (len <= 0) {puts ("Poor stack!"); return;} for (int i=1;i<=n;i++) if (aa[i]! =-1) printf ("%d", Aa[i]);p UTS (""); void output () {}int main () {while (true) {init (); if (input ()) return 0;cal (); output ();} return 0;}



Codeforces Round #220 (Div. 2) D tree-like array && two-part

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.