Codeforces Global Round 7E (segment tree, greedy)

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 long long a[300007],q[300007],b[300007];
 5 long long mx[1200007],mxid[1200007];
 6 long long lz[1200007];
 7 void up(long long rt){
 8     if(mx[rt<<1]>=mx[rt<<1|1]){
 9         mx[rt]=mx[rt<<1];
10         mxid[rt]=mxid[rt<<1];
11     }
12     else{
13         mx[rt]=mx[rt<<1|1];
14         mxid[rt]=mxid[rt<<1|1];
15     }
16 }
17 void down(long long rt){
18     lz[rt<<1]+=lz[rt];
19     lz[rt<<1|1]+=lz[rt];
20     mx[rt<<1]+=lz[rt];
21     mx[rt<<1|1]+=lz[rt];
22     lz[rt]=0;
23 }
24 void build(long long rt,long long l,long long r){
25     if(l==r){
26         mx[rt]=0;
27         mxid[rt]=l;
28         return ;
29     }
30     build(rt<<1,l,(l+r)>>1);
31     build(rt<<1|1,((l+r)>>1)+1,r);
32     up(rt);
33 }
34 void update(long long rt,long long l,long long r,long long L,long long R,long long k){
35     if(L>R)
36         return ;
37     if(L<=l&&r<=R){
38         lz[rt]+=k;
39         mx[rt]+=k;
40         return ;
41     }
42     down(rt);
43     if(L<=((l+r)>>1))
44         update(rt<<1,l,(l+r)>>1,L,R,k);
45     if(R>((l+r)>>1))
46         update(rt<<1|1,((l+r)>>1)+1,r,L,R,k);
47     up(rt);
48 }
49 long long cmx,cid;
50 void query(long long rt,long long l,long long r,long long L,long long R){
51     if(L<=l&&r<=R){
52         if(mx[rt]>cmx){
53             cmx=mx[rt];
54             cid=mxid[rt];
55         }
56         return ;
57     }
58     down(rt);
59     if(L<=((l+r)>>1))
60         query(rt<<1,l,(l+r)>>1,L,R);
61     if(R>((l+r)>>1))
62         query(rt<<1|1,((l+r)>>1)+1,r,L,R);
63 }
64 int main(){
65     ios::sync_with_stdio(false);
66     cin.tie(NULL);
67     cout.tie(NULL);
68     int n;
69     cin>>n;
70     for(int i=1;i<=n;++I) {
 71 is          CIN >> A [I];
 72          B [A [I]] = I; // store the number of occurrences of each location 
73 is      }
 74      for ( int I = . 1 ; I <= n-; ++ I )
 75          CIN >> Q [I];
 76      Build ( . 1 , . 1 , n-);
 77      int ANS = n-;
 78      Update ( . 1 , . 1 , n-, . 1 , B [n-], . 1 ); // maximum occurs position and left all of +1, meaning these locations and to the right there is a small number than them (this time not a member of the bomb) 
79      for ( int= I . 1 ; I <= n-; ++ I) {
 80          COUT ANS << << "  " ;
 81          Update ( . 1 , . 1 , n-, . 1 , Q [I], - . 1 ); // each addition a bomb, and the bomb left of all positions -1, if not all of the points is positive, indicating that the current ans illegal, because it is equal to the number of all particles larger than the right position and has at least bomb x (x being the number is the first major number) 
82          the while (MX [ . 1 ] <= 0 ) {
 83              --ans; // answer -1 redetection legality 
84              Update ( . 1 , . 1 , n-, . 1 , B [ANS], . 1 ); //After all the number of positions -1 and the left and right ans have more than a small number of its own 
85          }
 86      }
 87      return  0 ;
 88 }

Guess you like

Origin www.cnblogs.com/ldudxy/p/12554901.html