[luogu3709] [my string question]

Keywords: C++ less

Title Link

meaning of the title

It's enough to do two such questions a day to describe such a fairy. Really exercise Chinese ability.

The meaning of a question is to give you a sequence and then ask for an interval at a time. Make it possible to take the data from this interval in strict ascending order. If the current acquisition number is less than or equal to the previous one, let rp --, and then restart the recording. Ask rp how much it can be at most.

thinking

Thinking about it, we can find that it is actually to find the number of times the mode occurs in the interval.

The reasons are as follows:

If there are equal numbers, they cannot avoid rp -. Since we have to reduce it anyway, we have to consider how to make it more valuable.

For example, there is a sequence: 1 11 2 2 2 5 6 7 8

Now there are more than one 1 and two. Obviously, it is the best to count them in the following order:

1 2 5 6 7 8 1 2 5 1

The rp thus subtracted is actually the number of times mode 1 occurs.

So this question is to ask the number of times the mode in the interval appears. It can be achieved with team mo.

cnt[i] is used to express the number of times the number I occurs (it needs to be discretized first). T[i] indicates the number of occurrences of I.

When moving from one interval to another, the method is obvious. See code for details

Code

/*
* @Author: wxyww
* @Date:   2018-12-17 20:07:20
* @Last Modified time: 2018-12-17 20:40:37
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
#include<map>
using namespace std;
typedef long long ll;
const int N = 200000 + 10;
map<int,int>ma;
ll read() {
   ll x=0,f=1;char c=getchar();
   while(c<'0'||c>'9') {
      if(c=='-') f=-1;
      c=getchar();
   }
   while(c>='0'&&c<='9') {
      x=x*10+c-'0';
      c=getchar();
   }
   return x*f;
}
int a[N],cnt[N],ls[N],anss[N];
int ans,L,R,belong[N],T[N];
struct node {
   int id,l,r;
}Q[N];
bool cmp(node x,node y) {
   return belong[x.l] == belong[y.l] ? x.r < y.r : x.l < y.l;
}
void update(int pos,int c) {
   T[cnt[a[pos]]]--;
   if(T[cnt[a[pos]]] == 0 && ans == cnt[a[pos]]) {
      while(T[ans] == 0 && ans) ans--;
   }
   cnt[a[pos]] += c;
   T[cnt[a[pos]]]++;
   if(cnt[a[pos]] > ans) ans = cnt[a[pos]];
}

int main() {
   int n = read(),m = read();
   int blsiz = sqrt(n);
   for(int i = 1;i <= n;++i) ls[i] = a[i] = read(),belong[i] = (i - 1) / blsiz + 1;

   int lsjs = 0;
   sort(ls + 1,ls + n + 1);
   ma[ls[1]] = ++lsjs;
   for(int i = 2;i <= n;++i) if(ls[i] != ls[i - 1]) ma[ls[i]] = ++lsjs;
   for(int i = 1;i <= n;++i) a[i] = ma[a[i]];

   for(int i = 1;i <= m;++i)
      Q[i].l = read(),Q[i].r = read(),Q[i].id = i;
   sort(Q + 1,Q + n + 1,cmp);

   for(int i = 1;i <= m;++i) {
      while(L > Q[i].l) update(--L,1);
      while(R < Q[i].r) update(++R,1);
      while(L < Q[i].l) update(L++,-1);
      while(R > Q[i].r) update(R--,-1);
      anss[Q[i].id] = ans;
   }
   for(int i = 1;i <= m;++i) printf("%d\n",-anss[i]);
   return 0;
}

a brief remark

Fireworks are easy to cool and people are easy to divide.

Posted by yankeefan238 on Mon, 02 Dec 2019 11:09:24 -0800