codeforces 1288E. Messenger Simulator (Fenwick tree)

Link: https: //codeforces.com/contest/1288/problem/E

The meaning of problems: p sequence length is n, the initial sequence is 1 2 3 4 ... n, and m have operations, each time a designated number of moving to a first bit sequence, and then moving all remaining sequences back one, seeking the maximum and minimum number of each location in the index of all the historical series appeared in.

Ideas: maintaining a tree with the array position of the sequence, the sequence in front of the empty positions m, m is reserved for the purpose of digital operations to move the position of the top m. Initially, when the input data, the position pos array record of all the numbers of i + m, then i + m tree array is updated at i + m + 1 represents the position put a number, each moving operation when doing the update -1 cleared position represented here, which has the position number is not placed, may then be used to query the position of the tree-array section and the front portion, in front of the number to represent the number, can be naturally the maximum position update this number appeared, and the minimum update was: If you make a move operation, the minimum value of 1 is the digital position, because this number sequence on the front, and finally to traverse through all figures, the number of queries to update some not move the maximum position of the operation appear. Specific look at the code

 

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue> 
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 3e5+5;
10 int t[maxn*2];
11 int ansMin[maxn+1],ansMax[maxn+1];
12 int n,m;
13 inline int lowbit(int x){
14     return x&(-x);
15 }
16 void add(int x,int k){
17     while(x<=n+m){
18         t[x] = t[x] + k;
19         x +=lowbit(x);
20     }
21 }
22 int get(int x){
23     int ans = 0;
24     while(x>=1){
25         ans+=t[x];
26 is          X-= lowbit (X);
 27      }
 28      return ANS;
 29  }
 30  int main () {
 31 is      Scanf ( " % D% D " , & n-, & m);
 32      int POS [n-+ m + . 1 ];
 33 is      for ( int i = . 1 ; i <= n-; i ++ ) {
 34 is          POS [i] = i + m; // initialize the position of the element, pos [i] for the location of the element i 
35          ansMin [i] = i, ansMax [I] = I;
 36          the Add (I + m, . 1 ); //Fenwick tree location update + 1'd 
37 [      }
 38 is      for ( int I = 0 ; I <m; I ++ ) {
 39          int TEMP;
 40          Scanf ( " % D " , & TEMP);
 41 is          ansMin [TEMP] = . 1 ;
 42 is          the Add (POS [TEMP], - . 1 ); // this position -1, 
43 is          the Add (mi the, . 1 ); // move to the front, Fenwick tree + 1'd 
44 is          ansMax [TEMP] = max (ansMax [TEMP] , GET (POS [the TEMP])); // query in front of how many elements do max update 
45         POS [TEMP] = m - I; // Update Location 
46 is      }
 47      for ( int I = . 1 ; I <= n-; I ++ ) {
 48          ansMax [I] = max (ansMax [I], GET (POS [I] )); // the last check no element moving operation 
49      }
 50      for ( int I = . 1 ; I <= n-; I ++ ) {
 51 is          the printf ( " % D% D \ n- " , ansMin [I], ansMax [ I]);
 52 is      }
 53 is      return  0 ;
 54 is }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12210874.html