Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- ifstream cin("rangemode.in");
- ofstream cout("rangemode.out");
- int v[100005];
- int rez[100005];
- int f[100005];
- struct qu
- {
- int st,dr,poz,b;
- } q[100005];
- int max1,val;
- bool xort(qu a, qu b)
- {
- if(a.b<b.b)
- return true;
- if(a.b>b.b)
- return false;
- if(a.dr<b.dr)
- return true;
- return false;
- }
- void act(int poz, int care)
- {
- f[v[poz]]++;
- if(f[v[poz]] == max1 && v[poz] < val)
- val = v[poz];
- if(f[v[poz]]>max1)
- {
- max1 = f[v[poz]];
- val = v[poz];
- }
- }
- int main()
- {
- int n,m,i,k,poz,j,pozcazan;
- cin>>n>>m;
- for(i = 1; i <= n; i++)
- cin >> v[i];
- k = 320;
- for(i = 1; i <= m; i++)
- {
- cin >> q[i].st >> q[i].dr;
- q[i].poz = i;
- q[i].b = q[i].st/k+1;
- }
- sort(q + 1, q + m + 1, xort);
- i=1;
- while(i<=m)
- {
- poz = q[i].b*k;
- j=i;
- while(j<=m&&q[j].b == q[i].b)
- {
- max1=0;
- val=0;
- while(poz <= q[j].dr)
- {
- act(poz,1);
- poz++;
- }
- pozcazan = min(q[j].b*k - 1, q[j].dr);
- for(int x = q[j].st; x <= pozcazan; x++)
- act(x,2);
- for(int x = q[j].st ; x <= pozcazan; x++)
- f[v[x]]--;
- rez[q[j++].poz]=val;
- }
- i=j;
- for(j = 1; j <= 100000; j++)
- f[j]=0;
- }
- for(i = 1; i <= m; i++)
- cout<<rez[i]<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement