Remove One Extra weight segment tree

 Recently crazy exercise segment tree. . .

  This question is very simple question is intended, in the 1-n, to find a number that is removed after this number, for each location satisfies <= j <i && a [j] <a [i] as a position 1 many.

     We consider the contribution for each position i, if the current position of the condition has been met, then delete any number of front, this position is not actually contribute, and the current position, the position is equivalent to delete this meet a minus location conditions.

     If the current position by deleting a number, it becomes possible, then i must figure in the former position, and only one position greater than this number, we can easily be a large number of i-bit pre-inquiry than a certain value, and query the maximum value in the position (it has the right to query). Then i location, in order to become the answer, you must delete the previous maximum, we delete the previous maximum, equivalent to adding a position to meet the conditions.

   If there are two or more greater than the number in front, this number certainly can not be the answer.

   Therefore, we maintain an array, the array vis [i] for delete i, may generate a new position satisfying the condition number, the maximum value, minimum value. Is the answer! ! !

  Maintenance with weights segment tree can be friends! ! !

  

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<algorithm>
  5 #define LL long long
  6 #define lson rt<<1
  7 #define rson rt<<1|1
  8 #include<vector>
  9 using namespace std;
 10 const int maxx = 2e5+6;
 11 int vis[maxx];
 12 struct node{
 13   int l,r;
 14   int cnt;
 15 }tree[maxx<<2];
 16 int a[maxx];
 17 void buildtree(int rt,int l,int r){
 18    tree[rt].l=l;
 19    tree[rt].r=r;
 20    tree[rt].cnt=0;
 21    if(l==r){
 22      return ;
 23    }
 24    int mid=(l+r)>>1;
 25    buildtree(lson,l,mid);
 26    buildtree(rson,mid+1,r);
 27 }
 28 void update(int rt,int pos){
 29     int l=tree[rt].l;
 30     int r=tree[rt].r;
 31     if (l==r){
 32         tree[rt].cnt++;
 33         return;
 34     }
 35     int mid=(l+r)>>1;
 36     if(pos<=mid)
 37         update(lson,pos);
 38     else
 39         update(rson,pos);
 40     tree[rt].cnt=tree[lson].cnt+tree[rson].cnt;
 41 }
 42 int query_num(int rt,int w){
 43     int l=tree[rt].l;
 44     int r=tree[rt].r;
 45     if (l==r){
 46         return tree[rt].cnt;
 47     }
 48     int mid=(l+r)>>1;
 49     if(w<=mid){
 50         return tree[rson].cnt+query_num(lson,w);
 51     }else {
 52         return query_num(rson,w);
 53     }
 54 }
 55 int getmax(int rt){
 56     int l=tree[rt].l;
 57     int r=tree[rt].r;
 58     if(l==r){
 59         return l;
 60     }
 61     int mid=(l+r)>>1;
 62     if(tree[rson].cnt){
 63         return getmax(rson);
 64     }else {
 65         return getmax(lson);
 66     }
 67 }
 68 int main(){
 69    int n;
 70    while(~scanf("%d",&n)){
 71       for(int i=1;i<=n;i++){
 72         scanf("%d",&a[i]);
 73       }
 74       memset(vis,0,sizeof(vis));
 75       int mx=0;
 76       buildtree(1,1,n);
 77       for(int i=1;i<=n;i++){
 78          int s=query_num(1,a[i]);
 79          if(s==1){
 80             int pos=getmax(1);
 81             vis[pos]++;
 82          }else if(s==0){
 83             vis[a[i]]--;
 84          }
 85          update(1,a[i]);
 86       }
 87       int ans;
 88       mx=-2;
 89       for (int i=1;i<=n;i++){
 90           if(mx<vis[a[i]]){
 91             ans=a[i];
 92             mx=vis[a[i]];
 93           }
 94           else if(mx==vis[a[i]] && ans>a[i]){
 95             ans=a[i];
 96             mx=vis[a[i]];
 97           }
 98       }
 99        printf(" % D \ n " , year);
100     }
 101     return  0 ;
102 }

 

   

Guess you like

Origin www.cnblogs.com/bluefly-hrbust/p/11432783.html