Niuke.com wyh's swan (weight line segment tree)

Link: https://www.nowcoder.com/acm/contest/93/L
Source : Niuke.com

You, senior wyh, lived by the river when you were young. Because the surrounding ecological environment is very good, there are often swans floating on the lake. Each swan looks different. Going to other riverbanks, in order to count their number, senior wyh wrote a program to give them a "cute" value, but these swans are very disobedient. One or two in the middle, now please help him complete the statistical task. 
Input description:
There are T (T<=10) groups of data, the first row of each group of data is two numbers N, M (N, M <= 500000), which means there are N swans and M operations, the next row is N A number, the following M line will first enter a string S, and then there will be three types of operations, if S is "insert", then enter a positive integer a, which means inserting a swan whose "cute" value is a, if S is "delete", then enter a positive integer a, which means to delete a swan whose "moe" value is a. If S is "query", then enter a positive integer k, which means to query the swan with the kth largest "moe" value. The moe value is [1,1000000000], and it is guaranteed that there must be the kth largest
output description:
corresponding to each query, output the query result.

 

Example 1

 

enter

1
5 4
6 4 2 9 1
query 2
insert 7
delete 6
query 2

 

output

6
7

 

Analysis: The three operations in the question are to insert and delete a fixed number, and to ask the largest number. You can use a weighted segment tree or a balanced tree.

If you use a weighted segment tree, because the range of numbers is very large, you need to put all the numbers that appear in the question into a set for discretization processing

code show as below:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
using namespace std;
const int N=5e5+100;
int T[N<<2];
int maxx;
int rec[N];
int a[N];
int t,n,m,x;
int cnt;

struct node
{
  char a[10];
  int x;
} qes [N];

void BuildTree(int rt,int l,int r)
{
  T[rt]=0;
  if(l==r)return;
  int mid=(l+r)>>1;
  BuildTree(rt<<1,l,mid);
  BuildTree(rt<<1|1,mid+1,r);
}

void Updata( int p, int v, int rt, int l, int r)   // Insert v numbers whose value is p. If v is negative, it is deleted. 
{
  T[rt]+=v;
  if(l==r)return;
  int mid=(l+r)>>1;
  if(p<=mid)Updata(p,v,rt<<1,l,mid);
  else Updata(p,v,rt<<1|1,mid+1,r);
}

int kth(int k,int rt,int l,int r) //找到第k大的数
{
   if(l==r)return l;
   int mid=(l+r)>>1;
   if(T[rt<<1|1]>=k)return kth(k,rt<<1|1,mid+1,r);
   return kth(k-T[rt<<1|1],rt<<1,l,mid);
}

intmain ()
{
    scanf("%d",&t);
    while(t--)
    {
        cnt=1;
        vector < int >V;   // Put all the numbers that have appeared in V, and deduplicate them to facilitate discrete processing 
        map< int , int >mp;   // Used for discretization processing 
       memset(rec, 0 , sizeof ( rec)); // Used to associate, discretized numbers and previous numbers. 

       scanf( " %d%d " ,&n,& m);
        for ( int i= 1 ;i<=n;i++ )
       {
           scanf("%d",&a[i]);
           V.push_back(a[i]);
       }

       for(int i=1;i<=m;i++)
       {
           scanf("%s%d",qes[i].a,&qes[i].x);
           V.push_back(qes[i].x);
       }
        sort(V.begin(),V.end());
       V.erase(unique(V.begin(),V.end()),V.end()); // Deduplication operation

       for(int i=0;i<V.size();i++)
        {
             x=V[i];
             rec[cnt] =x;   // Record the number before discretization 
             mp[x]=cnt++;   // Discrete 
        }
        BuildTree(1,1,cnt);
       for(int i=1;i<=n;i++)
        Updata(mp[a[i]],1,1,1,cnt);

       for(int i=1;i<=m;i++)
       {
          if(qes[i].a[0]=='i')
          Updata(mp[qes[i].x],1,1,1,cnt);
          else if(qes[i].a[0]=='d')
          Updata(mp[qes[i].x],-1,1,1,cnt);
          else
          printf("%d\n",rec[kth(qes[i].x,1,1,cnt)]);
       }
    }
    return 0;
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469748&siteId=291194637