stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

pair<long long, long long> a[101010];

main()

{

    int n;

    int x, y;

    long long o;

	cin >>n;

    for(int i = 0; i < n; i ++)

        cin >>a[i].first >>a[i].second;

    for(int k = o = 0; k < 100000; k ++)

    {

        random_shuffle(a, a + n);

        x = 0;

        y = 0;

        for(int i = 0; i < n; i ++)

        {

        	

            if((long long)(x + a[i].first) * (x + a[i].first) + (long long)(y + a[i].second) * (y + a[i].second) > (long long)x * x + (long long)y * y)

            {

                x += a[i].first ;

                y += a[i].second;

            }

        }

        o = max(o, (long long)x * x + (long long)y * y);

    }

    cout <<fixed <<setprecision(20) <<(sqrt(o));

}

// Faster C++ solution for the same problem (measured speedup: 4.850601969117289x):
#include <bits/stdc++.h>



#define Y second

#define X first

#define pb push_back

#define mp make_pair



typedef long long ll;



using namespace std;



ll n, ans;

pair<ll, ll>a[200];



ll f(ll x, ll y){

	return x * x + y * y;

}



int main(){

	cin >> n;

	for(ll i = 1; i <= n; i++){

		cin >> a[i].X >> a[i].Y;

	}

	cout << '\n';

	for(ll k = 1; k <= 1000; k++){

		random_shuffle(a + 1, a + 1 + n);

		ll x, y;

		x = y = 0;

		for(ll i = 1; i <= n; i++){

			if(f(x + a[i].X, y + a[i].Y) > f(x, y)){

				x += a[i].X;

				y += a[i].Y;

			}

		}

		ans = max(ans, f(x, y));

	}

	printf("%.10f", sqrt(ans));

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

pair<long long, long long> a[101010];

main()

{

    int n;

    int x, y;

    long long o;

	cin >>n;

    for(int i = 0; i < n; i ++)

        cin >>a[i].first >>a[i].second;

    for(int k = o = 0; k < 100000; k ++)

    {

        random_shuffle(a, a + n);

        x = 0;

        y = 0;

        for(int i = 0; i < n; i ++)

        {

        	

            if((long long)(x + a[i].first) * (x + a[i].first) + (long long)(y + a[i].second) * (y + a[i].second) > (long long)x * x + (long long)y * y)

            {

                x += a[i].first ;

                y += a[i].second;

            }

        }

        o = max(o, (long long)x * x + (long long)y * y);

    }

    cout <<fixed <<setprecision(20) <<(sqrt(o));

}

// Faster C++ solution for the same problem (measured speedup: 4.931906620027086x):
#include <bits/stdc++.h>



using namespace std;



#ifdef NeverBeRed

#include "debug.h"

#else

#define debug(...) 9715

#endif

typedef long long ll;

typedef long double ld;

typedef complex<ll> point;

#define F first

#define S second



ll cross(point a, point b) { return imag(conj(a) * b); }



int qua(point x)

{

	if (x.real() > 0 && x.imag() >= 0) return 0;

	if (x.real() <= 0 && x.imag() > 0) return 1;

	if (x.real() < 0 && x.imag() <= 0) return 2;

	return 3;

}



int main()

{

	#ifdef TurnRed

		//freopen("a.in", "r", stdin);

		//freopen("a.out", "w", stdout);

	#endif



	ios_base::sync_with_stdio(0), cin.tie(0);



	int n;

	cin >> n;

	vector<point> v(n);

	for (int i = 0, x, y; i < n; ++i)

	{

		cin >> x >> y;

		v[i] = { x, y };

	}



	ll ans = 0;

//	if (n <= 20)

//	{

//		for (int i = 1; i < 1<<n; ++i)

//		{

//			point p = { 0, 0 };

//			for (int j = 0; j < n; ++j)

//				if (i >> j & 1)

//					p += v[j];

//			ans = max(ans, norm(p));

//		}

//	}

//	else

	{

		sort(v.begin(), v.end(), [](const point &x, const point &y)

		{

			int qx = qua(x);

			int qy = qua(y);

			if (qx != qy) return qx < qy;

			if (cross(x, y) == 0) return norm(x) < norm(y);

			return cross(x, y) > 0;

		});



		for (int i = 0; i < n; ++i)

		{

			point p = { 0, 0 };

			for (int j = i, k = n; k--; j = (j+1) % n)

			{

				p += v[j];

				ans = max(ans, norm(p));

			}

		}

	}

	ld ans2 = sqrtl(ans);

//	ld l = 0, r = ans;

//	for (int it = 500; it--; )

//	{

//		ld m = (l + r) / 2.0;

//		if (m * m < ans)

//			l = m;

//		else

//			r = m;

//	}

//	ans2 = r;

	cout << setprecision(20) << fixed << ans2/*sqrtl(ans)*/ << "\n";



	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main() {



    int n;

    cin >> n;



    pair<int, int> data[n];



    for(int i = 0; i < n; i++) {

        cin >> data[i].first >> data[i].second;

    }

    



    double sol = 0;

    for(int r = 0; r < 100000; r++) {

        random_shuffle(data, data + n);

        double x = data[0].first, y = data[0].second, c = sqrt(pow(x, 2) + pow(y, 2));

        for(int j = 1; j < n; j++) {

            x += data[j].first;

            y += data[j].second;

            double length = sqrt(pow(x, 2) + pow(y, 2));

            if(length > c) c = length;

            else {

                x -= data[j].first;

                y -= data[j].second;

            }

        }

        sol = max(c, sol);

    }



    cout << fixed << setprecision(20) << sol << endl;



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.418851329936683x):
#include <bits/stdc++.h>



using namespace std;



int main() {



    int n;

    cin >> n;



    pair<int, int> data[n];



    for(int i = 0; i < n; i++) {

        cin >> data[i].first >> data[i].second;

    }

    



    double sol = 0;

    for(int r = 0; r < 100; r++) {

        random_shuffle(data, data + n);

        for(int i = 0; i < n; i++) {

            double x = data[i].first, y = data[i].second, c = sqrt(pow(x, 2) + pow(y, 2));

            for(int j = 0; j < n; j++) {

                if(i == j) continue;

                x += data[j].first;

                y += data[j].second;

                double length = sqrt(pow(x, 2) + pow(y, 2));

                if(length > c) c = length;

                else {

                    x -= data[j].first;

                    y -= data[j].second;

                }

            }

            sol = max(c, sol);

        }

    }



    cout << fixed << setprecision(20) << sol << endl;



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main() {



    int n;

    cin >> n;



    pair<int, int> data[n];



    for(int i = 0; i < n; i++) {

        cin >> data[i].first >> data[i].second;

    }

    



    double sol = 0;

    for(int r = 0; r < 100000; r++) {

        random_shuffle(data, data + n);

        double x = data[0].first, y = data[0].second, c = sqrt(pow(x, 2) + pow(y, 2));

        for(int j = 1; j < n; j++) {

            x += data[j].first;

            y += data[j].second;

            double length = sqrt(pow(x, 2) + pow(y, 2));

            if(length > c) c = length;

            else {

                x -= data[j].first;

                y -= data[j].second;

            }

        }

        sol = max(c, sol);

    }



    cout << fixed << setprecision(20) << sol << endl;



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.418851329936683x):
#include <bits/stdc++.h>

 

using namespace std;

 

int main() {

 

    int n;

    cin >> n;

 

    pair<int, int> data[n];

 

    for(int i = 0; i < n; i++) {

        cin >> data[i].first >> data[i].second;

    }

    

 

    double sol = 0;

    for(int r = 0; r < 100; r++) {

        random_shuffle(data, data + n);

        for(int i = 0; i < n; i++) {

            double x = data[i].first, y = data[i].second, c = sqrt(pow(x, 2) + pow(y, 2));

            for(int j = 0; j < n; j++) {

                if(i == j) continue;

                x += data[j].first;

                y += data[j].second;

                double length = sqrt(pow(x, 2) + pow(y, 2));

                if(length > c) c = length;

                else {

                    x -= data[j].first;

                    y -= data[j].second;

                }

            }

            sol = max(c, sol);

        }

    }

 

    cout << fixed << setprecision(20) << sol << endl;

 

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

const double PI = acos(-1);



typedef complex<double> Point;

typedef vector<Point> VP;



int main(void){

    int n;

    cin >> n;

    VP ps(n);

    rep(i,n){

        double x,y;

        cin >> x >> y;

        ps[i] = Point(x,y);

    }

    double M = 10000;

    double res = 0;

    rep(i,M){

        Point inf = polar(1e9, 2.0*PI*(i/M));

        VP used;

        Point p(0,0);

        rep(j,n){

            if (abs(ps[j]-inf) < 1e9) {

                p += ps[j];

            }

        }

        res = max(res, abs(p));

    }

    printf("%.20f\n", res);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.123752348668366x):
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

const double PI = acos(-1);



typedef complex<double> Point;

typedef vector<Point> VP;



int main(void){

    int n;

    cin >> n;

    VP ps(n);

    rep(i,n){

        double x,y;

        cin >> x >> y;

        ps[i] = Point(x,y);

    }

    double M = 1000;

    double res = 0;

    rep(i,M){

        Point inf = polar(1e9, 2.0*PI*(i/M));

        VP used;

        Point p(0,0);

        rep(j,n){

            if (abs(ps[j]-inf) < 1e9) {

                p += ps[j];

            }

        }

        res = max(res, abs(p));

    }

    printf("%.20f\n", res);

    return 0;

}
// Slower C++ solution:
// 解説後

#include <algorithm>

#include <iostream>

#include <iomanip>

#include <vector>

#include <cmath>



template <typename T>

struct Vector2 {

  Vector2& operator+=(const Vector2& other) noexcept {

    x += other.x;

    y += other.y;

    return *this;

  }



  friend Vector2 operator+(Vector2 lhs, Vector2 rhs) noexcept {

    return Vector2(lhs) += rhs;

  }



  template<typename U>

  auto dot(Vector2<U> rhs) const noexcept {

    return x * rhs.x + y * rhs.y;

  }



  auto norm2() const noexcept { return dot(*this); }



  auto norm() const noexcept { return sqrt(norm2()); }



  T x, y;

};



// L^2ノルムを最大化したいので、方向が同じベクトルを集める

int solve_F() {

  size_t N;

  std::cin >> N;



  std::vector<Vector2<double>> directions(36000);

  for (size_t i = 0; i < directions.size(); ++i) {

    const double angle = 3.14159265358979 * 2 * i / directions.size();

    directions[i].x = cos(angle);

    directions[i].y = sin(angle);

  }





  std::vector<Vector2<int64_t>> summations(directions.size());

  for (size_t i = 0; i < N; ++i) {

    Vector2<int64_t> v;

    std::cin >> v.x >> v.y;



    for (size_t j = 0; j < directions.size(); ++j) {

      if (directions[j].dot(v) >= 0.0) {

        summations[j] += v;

      }

    }

  }



  double result = 0.0;

  for (const auto& v : summations) {

    result = std::max(result, v.norm());

  }



  std::cout << std::setprecision(20) << result;

  return 0;

}



int main() { return solve_F(); }


// Faster C++ solution for the same problem (measured speedup: 4.219581270775706x):
// 解説後

#include <algorithm>

#include <iostream>

#include <iomanip>

#include <vector>

#include <cmath>



template <typename T>

struct Vector2 {

  Vector2& operator+=(const Vector2& other) noexcept {

    x += other.x;

    y += other.y;

    return *this;

  }



  friend Vector2 operator+(Vector2 lhs, Vector2 rhs) noexcept {

    return Vector2(lhs) += rhs;

  }



  template<typename U>

  auto dot(Vector2<U> rhs) const noexcept {

    return x * rhs.x + y * rhs.y;

  }



  auto norm2() const noexcept { return dot(*this); }



  auto norm() const noexcept { return sqrt(norm2()); }



  T x, y;

};



// L^2ノルムを最大化したいので、方向が同じベクトルを集める

int solve_F() {

  size_t N;

  std::cin >> N;



  std::vector<Vector2<double>> directions(360);

  for (size_t i = 0; i < directions.size(); ++i) {

    const double angle = 3.14159265358979 * 2 * i / directions.size();

    directions[i].x = cos(angle);

    directions[i].y = sin(angle);

  }





  std::vector<Vector2<int64_t>> summations(directions.size());

  for (size_t i = 0; i < N; ++i) {

    Vector2<int64_t> v;

    std::cin >> v.x >> v.y;



    for (size_t j = 0; j < directions.size(); ++j) {

      if (directions[j].dot(v) >= 0.0) {

        summations[j] += v;

      }

    }

  }



  double result = 0.0;

  for (const auto& v : summations) {

    result = std::max(result, v.norm());

  }



  std::cout << std::setprecision(20) << result;

  return 0;

}



int main() { return solve_F(); }
// Slower C++ solution:
// F.



#include <iostream>

#include <sstream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long long LL;

typedef long double LD;



int n;

vector<LD> x, y;

vector<LD> angle;



double calc(LD r) {

	LD xs = 0, ys = 0;

	for (int i = 0; i < n; ++i) {

		if (r - M_PI/2 <= angle[i] && angle[i] <= r + M_PI/2) {

			xs += x[i], ys += y[i];

		}

	}

	return sqrt(xs * xs + ys * ys);

}





int main(int argc, char* argv[]) {

	cout.precision(12);



	cin >> n;

	x.resize(n);

	y.resize(n);

	angle.resize(n);

	for (int i = 0; i < n; ++i) {

		cin >> x[i] >> y[i];

		angle[i] = atan2(y[i], x[i]);

		if (angle[i] < 0) {

			angle[i] += 2 * M_PI;

		}

	}

	LD left = 0, right = 2 * M_PI;



	for (int t = 0; t < 1000; ++t) {

		if (calc((left * 2 + right) / 3) > calc((left + right * 2) / 3)) {

			right = (left + right * 2) / 3;

		} else {

			left = (left * 2 + right) / 3;

		}

	}

	LD ans = calc(left);



	for (int i = 0; i < n; ++i) {

		angle[i] = atan2(x[i], y[i]);

		if (angle[i] < 0) {

			angle[i] += 2 * M_PI;

		}

	}

	left = 0, right = 2 * M_PI;



	for (int t = 0; t < 1000; ++t) {

		if (calc((left * 2 + right) / 3) > calc((left + right * 2) / 3)) {

			right = (left + right * 2) / 3;

		} else {

			left = (left * 2 + right) / 3;

		}

	}

	LD ans2 = calc(left);

	if (ans2 > ans) {

		ans = ans2;

	}



	cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.1399434020965966x):
// F.



#include <iostream>

#include <sstream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long double LD;



int main(int argc, char* argv[]) {

	cout.precision(12);

	while (true) {

		int n = -1;

		cin >> n;

		if (n <= 0) break;

		vector<LD> x(n), y(n);

		vector<pair<LD, int>> v(n);

		for (int i = 0; i < n; ++i) {

			cin >> x[i] >> y[i];

			v[i] = {atan2(y[i], x[i]), i};

		}

		sort(v.begin(), v.end());

		LD ans = 0;

		for (int i = 0; i < n; ++i) {

			LD xs = 0, ys = 0;

			for (int j = 0; j < n; ++j) {

				int k = v[(i + j) % n].second;

				xs += x[k], ys += y[k];

				ans = max(ans, sqrt(xs * xs + ys * ys));

			}

		}

		cout << ans << endl;

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <iomanip>

#include <cmath>

#include <random>

using namespace std;

random_device rd;

mt19937 mt(rd());



int n;

double x[111], y[111];



int main() {

  cin >> n;

  for(int i = 0; i < n; ++i) cin >> x[i] >> y[i];



  double ans = 0.0;

  uniform_real_distribution<> rand(-1.0, 1.0);

  for(int a = 0; a < 10000; ++a) {

    x[n] = rand(mt), y[n] = rand(mt);



    double px = 0.0, py = 0.0;

    for(int i = 0; i < n; ++i) {

      if(x[i]*x[n] + y[i]*y[n] >= 0) px += x[i], py += y[i];

    }

    ans = max(ans, px*px + py*py);

  }

  cout << fixed << setprecision(15) << sqrt(ans);

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.650803233562204x):
#include <iostream>

#include <algorithm>

#include <iomanip>

#include <cmath>

using namespace std;



int n;

pair<double, double> ps[222];



int main() {

  cin >> n;

  for(int i = 0; i < n; ++i) cin >> ps[i].first >> ps[i].second;

  sort(ps, ps + n, [](auto a, auto b) {

      return atan2(a.first, a.second) < atan2(b.first, b.second);

  });

  for(int i = 0; i < n; ++i) ps[i + n] = ps[i];



  double ans = 0.0;

  for(int L = 0; L < n; ++L) {

    for(int R = L; R < L + n; ++R) {

      double x = 0.0, y = 0.0;

      for(int i = L; i <= R; ++i) x += ps[i].first, y += ps[i].second;

      ans = max(ans, x*x + y*y);

    }

  }

  cout << fixed << setprecision(15) << sqrt(ans);

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define LL long long

#define pii pair<int,int>

#define SZ(x)  (int)x.size()

#define all(x) x.begin(),x.end()



using namespace std;



LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}

LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}

LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}

const int N = 2e5 + 11;

int n;

struct uzi

{

	int a, b;

	bool operator <(const uzi & t)const {

		return 	atan2(a, b) < atan2(t.a, t.b);

	}

} p[N];

double get(LL l, LL r) {

	return sqrt(l * l + r * r);

}

int main() {

	ios::sync_with_stdio(false);

	cin >> n;

	for (int i = 1; i <= n; i++)cin >> p[i].a >> p[i].b;

	sort(p + 1, p + 1 + n);

	double ans = 0;

	for (int i = 1; i <= n; i++) {

		LL x = 0, y = 0;

		for (int j = 0; j < n; j++) {

			x += p[((i + j) % n) == 0 ? n : ((i + j) % n)].a;

			y += p[((i + j) % n) == 0 ? n : ((i + j) % n)].b;

			ans = max(ans, get(x, y));

		}

	}

	cout << fixed << setprecision(20) << ans << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.577046067354113x):
#include <cmath>

#include <cstdio>

#include <algorithm>

using namespace std;

typedef long long ll;

const double pi = acos(-1);

struct data

{

	ll x , y;

	double ang;

	bool operator<(const data &a)const {return ang < a.ang;}

} a[400010];

int main()

{

	int n , i , p;

	ll sx = 0 , sy = 0 , ans = 0;

	scanf("%d" , &n);

	for (i = 1 ; i <= n ; i ++ ) scanf("%lld%lld" , &a[i].x , &a[i].y) , a[i].ang = atan2(a[i].y , a[i].x);

	sort(a + 1 , a + n + 1);

	for (p = i = 1 ; i <= n ; i ++ )

	{

		while (p < i + n && a[p].ang - a[i].ang < pi) sx += a[p].x , sy += a[p ++ ].y , ans = max(ans , sx * sx + sy * sy);

		sx -= a[i].x , sy -= a[i].y , ans = max(ans , sx * sx + sy * sy);;

		a[i + n].x = a[i].x , a[i + n].y = a[i].y , a[i + n].ang = a[i].ang + 2 * pi;

	}

	printf("%.18f\n" , sqrt(1.0 * ans));

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int blk;

struct node{

    int left,right;

    int index;

};

static bool compare(node& p1,node& p2){

    if(p1.left/blk==p2.left/blk)

    return p1.right<p2.right;

    return p1.left<p2.left;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int n,m;

    cin>>n>>m;

    blk=sqrt(n);

    vector<int> a(n,0);

    for(int i=0;i<n;i++)

    cin>>a[i];

    vector<node> q;

    q.resize(m);

    for(int i=0;i<m;i++){

        int l,r;

        cin>>l>>r;

        node p;

        p.left=l,p.right=r,p.index=i;

        q[i]=p;

    }

    sort(q.begin(),q.end(),compare);

    vector<int> freq(500001,0);

    vector<int> ans(m,0);

    int ml=0,mr=-1,count=0;

    for(int i=0;i<m;i++){

        int l=q[i].left-1,r=q[i].right-1;

        int ind=q[i].index;

        while(ml>l)

        if(++freq[a[--ml]]==1)

        count++;

        while(mr<r)

        if(++freq[a[++mr]]==1)

        count++;

        while(ml<l)

        if(--freq[a[ml++]]==0)

        count--;

        while(mr>r)

        if(--freq[a[mr--]]==0)

        count--;

        ans[ind]=count;

    }

    for(auto &i:ans)

    cout<<i<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.9995401345991133x):
#include<bits/stdc++.h>

#define REP(i,n) for (int i = 1; i <= n; i++)

#define mod 1000000007

#define pb push_back

#define ff first

#define ss second

#define ii pair<int,int>

#define vi vector<int>

#define vii vector<ii>

#define lli long long int

#define INF 1000000000

#define endl '\n'

#define BLOCK 710

const double PI = 3.141592653589793238460;

typedef std::complex<double> Complex;

typedef std::valarray<Complex> CArray;

 

using namespace std;

struct query{

	int l;

	int r;

	int i;

};

query Q[500001];

int ar[500001] , ans[500001];

int fre[500001];

int cnt = 0;

 

bool comp(query a , query b)

{

	if(a.l / BLOCK != b.l/BLOCK)

	return a.l/BLOCK < b.l/BLOCK;

	

	return a.r < b.r;

}

 

void add(int pos)

{

	fre[ar[pos]]++;

	if(fre[ar[pos]] == 1)

	cnt++;

}

 

void remove(int pos)

{

	fre[ar[pos]]--;

	if(fre[ar[pos]] == 0)

	cnt--;

}

 

int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	

	

	int n , q;

	cin>>n>>q;

	for(int i=0;i<n;i++)

	cin>>ar[i];

	

	for(int i=0;i<q;i++){

		cin>>Q[i].l>>Q[i].r;

		Q[i].i = i , Q[i].l-- , Q[i].r--;

	}

	

	sort(Q , Q+q , comp);

	

	//why ML = 0 , and MR = -1?

	int ML = 0 , MR = -1;

	for(int i=0;i<q;i++)

	{

		int L = Q[i].l;

		int R = Q[i].r;

		

		while(ML > L)

		ML-- , add(ML);

		

		while(MR < R)

		MR++ , add(MR);

		

		while(ML < L)

		remove(ML) , ML++;

		

		while(MR > R)

		remove(MR) , MR--;

		

		ans[Q[i].i] = cnt;

	}

	

	for(int i=0;i<q;i++)

	cout<<ans[i]<<'\n';

}
// Slower C++ solution:
#pragma GCC optimize(3,"Ofast","inline")

#include<bits/stdc++.h>

using namespace std;

const int N=1e6+10;

int bl,t,n,m;

int res,a[N],cnt[N],ans[N];

int curR,curL;

struct node

{

  int l,r,p;

  bool operator <(const node &W)const

  {

    return l<W.l;

  }

}e[N];

bool cmp(const node &a,const node &b)

{

  return (a.l/bl)==(b.l/bl) ?  a.r<b.r : a.l>b.l;

}

void remove(int i)

{

  if((--cnt[a[i]])==0) res--;

}

void add(int i)

{

  if((++cnt[a[i]])==1) res++;

}

int main()

{



     memset(ans,0,sizeof(ans));

     memset(cnt,0,sizeof(cnt));

     scanf("%d %d",&n,&m);

     for(int i=1;i<=n;i++) scanf("%d",&a[i]);



     bl=sqrt(n);

     for(int i=1;i<=m;i++)

     {

       scanf("%d %d",&e[i].l,&e[i].r);

       e[i].p=i;

     }

     sort(e+1,e+1+m,cmp);

     res=0;

     curL=1;curR=0;

     for(int i=1;i<=m;i++)

     {

       int L=e[i].l,R=e[i].r;

       while(curL<L) remove(curL++);

       while(curL>L) add(--curL);

       while(curR<R) add(++curR);

       while(curR>R) remove(curR--);

       ans[e[i].p]=res;

     }

     for(int i=1;i<=m;i++) printf("%d\n",ans[i]);



}




// Faster C++ solution for the same problem (measured speedup: 9.382576393084172x):
#pragma GCC optimize(3,"Ofast","inline")

#include<bits/stdc++.h>

using namespace std;

const int N=5e5+10;

int cnt[N],ans[N],a[N];

int n,q,bl;

int res;

struct node

{

    int l,r,p;



}e[N];

bool cmp(const node &a,const node &b)

{

    return (a.l/bl)==(b.l/bl) ? a.r<b.r :a.l<b.l;

}

void remove(int x)

{

    if((--cnt[a[x]])==0) res--;

}

void add(int x)

{

    if((++cnt[a[x]])==1) res++;

}

int main()

{

    scanf("%d %d",&n,&q);

    for(int i=1;i<=n;i++) scanf("%d",&a[i]);

     bl=sqrt(n);

    for(int i=1,l,r;i<=q;i++)

    {

        scanf("%d %d",&l,&r);

        e[i]={l,r,i};

    }

  //  cout<<"--------------"<<endl;



    sort(e+1,e+1+q,cmp);

   // cout<<"*****"<<endl;

    res=0;

    int curL=1,curR=0;

    for(int i=1;i<=q;i++)

    {

        //cout<<i<<endl;

        int L=e[i].l,R=e[i].r;

        while(curL<L) remove(curL++);

        while(curL>L) add(--curL);

        while(curR<R) add(++curR);

        while(curR>R) remove(curR--);

        ans[e[i].p]=res;

    }

    for(int i=1;i<=q;i++) printf("%d\n",ans[i]);



}