malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> a(n);     vector<long long> f(2000006, -1);     for (int i = 0; i < n; i++) {       cin >> a[i];       f[a[i]] = i;     }     long long ans = 1e18, res;     for (int i = 0; i <= 2000005; i++) {       if (f[i] < 0) continue;       if (ans > f[i]) {         res = i;       }       ans = min(f[i], ans);     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n;     cin >> n;     long long int a[n], b[n], c[n - 1];     b[n - 1] = 0, c[n - 2] = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n - 1; i++) {       cin >> b[i];     }     for (int i = 0; i < n - 2; i++) {       cin >> c[i];     }     sort(a, a + n);     sort(b, b + n - 1);     sort(c, c + n - 2);     for (int i = 0; i < n; i++) {       if (a[i] != b[i]) {         cout << a[i] << endl;         break;       }     }     for (int i = 0; i < n - 1; i++) {       if (b[i] != c[i]) {         cout << b[i] << endl;         break;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e5 + 5;   int n;   int ans[inf];   int last[inf];   struct node {     int x, h, q;     bool operator<(const node &s) const { return s.x > x; }   } a[inf << 1];   inline int read() {     int a = 0;     int b = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) b = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = a * 10 + c -  0 ;       c = getchar();     }     return a * b;   }   inline void run() {     int x, y;     n = read();     for (int i = 1; i <= n; i++) {       x = read();       y = read();       a[i].x = x;       a[i].h = y;       a[i].q = i;     }     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       ans[i] = 1;     }     for (int i = n - 1; i > 0; i--) {       int now = i + 1;       while (now && ((a[i].x + a[i].h) > a[now].x)) {         ans[a[i].q] += ans[a[now].q];         now = last[now];       }       last[i] = now;     }     for (int i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int sum, limits;     vector<int> ans;     cin >> sum >> limits;     for (int i = limits; i >= 1; i--) {       if ((i & (-i)) <= sum) {         sum -= (i & (-i));         ans.push_back(i);       }     }     if (!sum) {       int len = ans.size();       cout << len << endl;       for (int i = 0; i < len; i++) {         cout << ans[i];         if (i == len - 1)           cout << endl;         else           cout <<    ;       }     } else       cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[1000005];   int par[1000005], sz[1000005];   int find(int x) {     if (par[x] != x) par[x] = find(par[x]);     return par[x];   }   void Union(int a, int b) {     a = find(a);     b = find(b);     if (a != b) {       par[a] = b;       sz[b] += sz[a];       sz[a] = 0;     }   }   int ans[1005][1005], n, m;   bool vis[1005][1005], add[1000005];   long long k;   void dfs(int x, int y) {     vis[x][y] = 1;     if (!k) return;     ans[x][y] = 1;     k--;     for (int dx = -1; dx <= 1; dx++) {       for (int dy = -1; dy <= 1; dy++) {         if (abs(dx) + abs(dy) == 1 && x + dx >= 0 && x + dx < n && y + dy >= 0 &&             y + dy < m && !vis[x + dx][y + dy] && add[(x + dx) * m + y + dy])           dfs(x + dx, y + dy);       }     }   }   int main() {     scanf( %d%d%I64d , &n, &m, &k);     for (int i = 0; i < n * m; i++) {       scanf( %d , &arr[i].first);       arr[i].second = i;       par[i] = i;       sz[i] = 1;     }     sort(arr, arr + n * m);     for (int i = n * m - 1; i >= 0; i--) {       int idx = arr[i].second;       if (idx >= m && add[idx - m]) Union(idx, idx - m);       if (idx < (n - 1) * m && add[idx + m]) Union(idx, idx + m);       if (idx % m && add[idx - 1]) Union(idx, idx - 1);       if (idx % m != m - 1 && add[idx + 1]) Union(idx, idx + 1);       if (k % arr[i].first == 0 && sz[find(idx)] >= k / arr[i].first) {         k /= arr[i].first;         dfs(idx / m, idx % m);         printf( YES n );         for (int x = 0; x < n; x++) {           for (int j = 0; j < m; j++) printf( %d  , ans[x][j] * arr[i].first);           printf(  n );         }         return 0;       }       add[idx] = 1;     }     printf( NO );   }
#include <bits/stdc++.h>   using namespace std;   int m, n, arr[200][200];   int color(int x, int y) {     int col = 1;     while (arr[x - 1][y] == col or arr[x][y - 1] == col or arr[x + 1][y] == col or            arr[x][y + 1] == col)       col++;     return col;   }   int color2(int x, int y) {     int col = 1;     while (arr[x - 1][y] == col or arr[x + 1][y] == col or arr[x][y + 1] == col)       col++;     return col;   }   int fit(int x, int y, int col) {     if (arr[x][y - 1] == col or arr[x + 1][y] == col or arr[x][y + 1] == col)       return 0;     return 1;   }   int main() {     cin >> m >> n;     int x = 1, y = 1;     while (x <= m) {       if (y == n + 1) {         x++;         y = 1;         continue;       }       if (arr[x][y]) {         y++;         continue;       }       int xx = x, yy = y, col = color(x, y);       arr[x][y] = col;       while (xx + 1 <= m and yy + 1 <= n and !arr[xx + 1][y] and              !arr[x][yy + 1] and fit(xx + 1, y, col) and color2(x, yy + 1) == col)         xx++, yy++;       for (int i = x; i <= xx; i++)         for (int j = y; j <= yy; j++) arr[i][j] = col;     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= n; j++) printf( %c , (char)(arr[i][j] +  A  - 1));       puts(  );     }   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   double dp[205][35][35];   vector<int> p;   pair<int, int> rev(int x, int y, int rx, int ry) {     if (x >= rx && x <= ry) {       x = rx + (ry - rx) - (x - rx);     }     if (y >= rx && y <= ry) {       y = rx + (ry - rx) - (y - rx);     }     return make_pair(x, y);   }   int main() {     cin >> n >> k;     p.resize(n);     for (int i = 0; i < n; i++) {       cin >> p[i];     }     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) {         dp[0][i][j] = 1.0;       }     }     double pp = 2.0 / (n * (n + 1));     for (int q = 1; q <= k; q++) {       for (int i = 0; i < n; i++) {         for (int j = i; j < n; j++) {           for (int k = 0; k < n; k++) {             for (int l = k; l < n; l++) {               pair<int, int> xy = rev(i, j, k, l);               if (xy.first < xy.second) {                 dp[q][i][j] += dp[q - 1][xy.first][xy.second] * pp;               } else {                 dp[q][i][j] += (1.0 - dp[q - 1][xy.second][xy.first]) * pp;               }             }           }         }       }     }     double ans = 0;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if (p[i] < p[j]) {           ans += 1.0 - dp[k][i][j];         } else {           ans += dp[k][i][j];         }       }     }     printf( %.9lf n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long modular(long long a, long long b, long long c) {     if (b == 0) {       return 1;     }     if (b % 2 == 0) {       return modular(((a * a) % c), b / 2, c);     } else {       return ((a % c) * (modular(((a * a) % c), b / 2, c) % c)) % c;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       string arr[n];       for (long long i = 0; i < n; i++) {         cin >> a[i];         arr[i] = bitset<32>(a[i]).to_string();       }       long long turn = 0;       long long flag = 0;       for (long long i = 0; i < 32; i++) {         long long c1 = 0;         long long c2 = 0;         for (long long j = 0; j < n; j++) {           if (arr[j][i] ==  1 ) {             c1++;           } else {             c2++;           }         }         if ((c1 % 4 == 1) || ((c1 % 4 == 3) && (n % 2 == 0))) {           if (turn == 0) {             flag = 1;             break;           } else {             flag = 2;             break;           }         }         if ((c1 % 4 == 3) && (n % 2 != 0)) {           if (turn == 0) {             flag = 2;             break;           } else {             flag = 1;             break;           }         }       }       if (flag == 0) {         cout <<  DRAW               <<   n ;       } else {         if (flag == 1) {           cout <<  WIN                 <<   n ;         } else {           cout <<  LOSE                 <<   n ;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     char grid[n][m];     bool flag1 = 0, flag2 = 0;     int cnt[100] = {};     for (int i = 0; i < n; i++) {       string s;       cin >> s;       if (s.find( - ) < 0 || s.find( - ) >= s.size()) continue;       int x = s.find( G );       int y = s.find( R );       if (y == -1 && x == -1) continue;       if (y >= s.size() || y < 0) flag1 = 1;       if (x >= s.size() || x < 0) flag2 = 1;       if (x < s.size() && x >= 0 && y < s.size() && y >= 0) {         int tmp = abs(x - y) - 1;         int cur = 0;         while (tmp) {           cnt[cur] += tmp % 2;           tmp /= 2;           cur++;         }       }     }     if (flag1 & flag2)       puts( Draw );     else if (flag1)       puts( First );     else if (flag2)       puts( Second );     else {       int ans = 0;       for (int i = 0; i < 100; i++)         if (cnt[i] % (k + 1)) {           puts( First );           return 0;         }       puts( Second );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     while (1 == scanf( %d , &N)) {       vector<int> A(N);       for (int i = 0; i < (int)(N); i++) scanf( %d , &A[i]);       sort((A).begin(), (A).end());       int need_to_fill = 0, need_to_move = 0;       for (int i = 0; i < (int)(N); i++) need_to_fill += max(0, 3 - A[i]);       int total = accumulate((A).begin(), (A).end(), 0);       sort((A).begin(), (A).end());       int ans = INT_MAX;       for (int i = 0; i < (int)(N); i++) {         int num_rooms = N - i;         if (4 * num_rooms >= total && 3 * num_rooms <= total) {           int tmp = max(need_to_move, need_to_fill);           ans = min(ans, tmp);         }         need_to_fill -= max(0, 3 - A[i]);         need_to_move += A[i];       }       if (ans == INT_MAX)         puts( -1 );       else         printf( %d n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     if (t % 2 == 0) {       cout <<  Mahmoud  << endl;     } else {       cout <<  Ehab  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, n;     cin >> a >> b;     n = a + b + 1;     for (int i = 0; i < a; i++) cout << i + 1 <<    ;     for (int i = 0; i <= b; i++) cout << a + b + 1 - i <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     register int x = 0, f = 1;     register char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar();     return x * f;   }   namespace Graph {   int head[1000005], cnt;   struct edge {     int v, next, w;   } e[1000005 << 1];   inline void add(int u, int v, int w) {     e[++cnt] = (edge){v, head[u], w}, head[u] = cnt;   }   }  // namespace Graph   int n, m, k, Ans, fac, sum, g[1000005];   char c[1000005];   inline int qpow(int a, int b) {     int r = 1;     while (b) {       if (b & 1) r = 1ll * a * r % 1000000007;       a = 1ll * a * a % 1000000007, b >>= 1;     }     return r;   }   signed main() {     Ans = n = read(), k = read(), g[0] = fac = 1;     for (register int i = (1); i <= (n); ++i) {       sum = (sum + g[i - 1]) % 1000000007;       if (i > k) sum = (sum - g[i - k - 1] + 1000000007) % 1000000007;       g[i] = 1ll * sum * qpow(i, 1000000007 - 2) % 1000000007;       Ans = (Ans - g[i - 1] + 1000000007) % 1000000007;       if (i != 1) fac = 1ll * fac * (i - 1) % 1000000007;     }     cout << 1ll * fac * Ans % 1000000007;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[200000 + 9];   long long com[200000 + 9];   double f1(int i, int k) {     long long sum1 = a[i] + com[n] - com[n - k] + com[i - 1] - com[i - 1 - k];     double av1 = (double)sum1 / (2 * k + 1);     return av1;   }   double f2(int i, int k) {     long long sum2 =         a[i] + com[n] - com[n - (k - 1)] + com[i - 1] - com[i - 1 - (k - 1)];     double av2 = (double)sum2 / (2 * (k - 1) + 1);     return av2;   }   long long ValAt(int i, int k) {     long long sum = a[i] + com[n] - com[n - k] + com[i - 1] - com[i - 1 - k];     return sum;   }   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     cin >> n;     for (int i = (1); i <= (n); i++) cin >> a[i];     sort(a + 1, a + n + 1);     if (n <= 2) {       cout << n <<   n ;       for (int i = (1); i <= (n); i++) cout << a[i] <<    ;       cout <<   n ;       return 0;     }     com[0] = 0;     for (int i = (1); i <= (n); i++) com[i] = com[i - 1] + a[i];     double bestVal = 0;     int besti = 1, bestSize = 1;     for (int i = 2; i < n; i++) {       int s = 1, e = min(i - 1, n - i);       double bestAv = 0;       int size;       while (s <= e) {         int k = (s + e) / 2;         long long sum1 = ValAt(i, k);         double av1 = (double)sum1 / (2 * k + 1);         long long sum2 = ValAt(i, k - 1);         double av2 = (double)sum2 / (2 * (k - 1) + 1);         if (av1 > av2) {           s = k + 1;           bestAv = av1;           size = 2 * k + 1;         } else           e = k - 1;       }       double val = bestAv - a[i];       if (val > bestVal) {         bestVal = val;         besti = i;         bestSize = size;       }     }     vector<int> v;     v.push_back(a[besti]);     int k = (bestSize - 1) / 2;     for (int i = besti - 1; i >= besti - k; i--) v.push_back(a[i]);     for (int i = n; i > n - k; i--) v.push_back(a[i]);     sort(v.begin(), v.end());     cout << bestSize <<   n ;     for (auto x : v) cout << x <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double n, m, k;   int main() {     while (scanf( %lf %lf %lf , &n, &m, &k) != EOF) {       if (m > n + k) {         printf( %.6lf n , 0.0);         continue;       }       double resp = 1.0;       for (int i = 0; i <= k; i++) {         resp *= ((m - i) / (n + k + 1 - i));       }       printf( %.6lf n , 1.0 - resp);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 9, M = 3e5 + 9, OO = 0x3f3f3f3f;   const double EPS = 0.00000000001;   const int mod = 1000003;   int n, m, d[3005];   bool flage = 1;   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i) {       scanf( %d , d + i);       if (d[i] == 1 || d[i] == n) flage = 0;     }     sort(d, d + m);     int mx = min(1, m);     for (int i = 0, j = 1; j < m; ++j)       if (d[j] == d[j - 1] + 1)         mx = max(mx, j - i + 1);       else         i = j;     if (mx >= 3) flage = 0;     if (flage)       puts( YES );     else       puts( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   bool a[100100];   void go() {     long long int n;     cin >> n;     memset(a, true, sizeof(a));     for (int i = 2; i <= n; i++) {       for (int j = i + 1; i * j <= n; j++) {         if (j % i == 0) continue;         a[j * i] = false;       }     }     vector<int> ans;     for (int i = 2; i <= n; i++) {       if (a[i]) ans.push_back(i);     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout << endl;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long int t = 1;     while (t--) go();   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 10;   const int inf = 0x3f3f3f3f;   int a[maxn];   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= k; ++i) scanf( %d , &a[i]);     int x = 2 * n, y = n;     for (int i = 1; i <= k; ++i) {       if (a[i] >= 4 && y > 0) {         int m = a[i] / 4;         a[i] -= min(m, y) * 4;         y -= min(m, y);       }     }     for (int i = 1; i <= k; ++i) {       if (x > 0) {         int m = a[i] / 2;         a[i] -= min(m, x) * 2;         x -= min(x, m);       }     }     int tmp = y;     for (int i = 1; i <= k; ++i) {       if (y > 0) {         int m = a[i] / 2;         a[i] -= min(m, y) * 2;         y -= min(m, y);       }     }     tmp = tmp - y + x;     for (int i = 1, j = k; i < j;) {       if (y > 0) {         int m = min(min(a[i], a[j]), y);         a[i] -= m, a[j] -= m, y -= m;       }       if (a[i] == 0) ++i;       if (a[j] == 0) --j;       if (y == 0) break;     }     tmp += y;     for (int i = 1; i <= k; ++i) {       if (tmp > 0) {         int m = min(tmp, a[i]);         a[i] -= m;         tmp -= m;       }     }     int f = 1;     for (int i = 1; i <= k; ++i) {       if (a[i] > 0) f = 0;     }     if (f)       printf( YES n );     else       printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a;     cin >> n >> a;     vector<int> checkpt;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       checkpt.push_back(t);     }     sort(checkpt.begin(), checkpt.end());     long long distance = 0;     if (n == 1) {       cout << 0 << endl;       return 0;     }     if (checkpt[n - 1] <= a) {       distance += a - checkpt[1];       cout << distance << endl;       return 0;     } else if (a <= checkpt[0]) {       distance += checkpt[n - 2] - a;       cout << distance << endl;       return 0;     }     int pointer;     for (int i = 0; i < n - 1; i++) {       if (checkpt[i] <= a && checkpt[i + 1] >= a) {         checkpt.insert(checkpt.begin() + (i + 1), a);         pointer = i + 1;         break;       }     }     long long dis1 = 0;     long long dis2 = 0;     long long dis3 = 0;     long long dis4 = 0;     dis1 += checkpt[pointer] - checkpt[0];     if (pointer + 1 != n) {       dis1 += checkpt[n - 1] - checkpt[0];       dis3 += (checkpt[n - 1] - checkpt[pointer]) * 2;     }     dis3 += checkpt[pointer] - checkpt[0];     dis4 += checkpt[n] - checkpt[pointer];     if (pointer - 1 != 0) {       dis2 += (checkpt[pointer] - checkpt[1]) * 2;       dis4 += checkpt[n] - checkpt[1];     }     dis2 += checkpt[n] - checkpt[pointer];     long long min1 = min(dis1, dis2);     long long min2 = min(min1, dis3);     distance = min(min2, dis4);     cout << distance << endl;   }
#include <bits/stdc++.h>   int main() {     int x, y, x1 = 0, y1 = 0, n, k = 0;     do {       scanf( %d%d , &x, &n);     } while (x < 1 || n < 1 || n > 100000 || x > 100000);     y = x;     if ((x == 4 && n == 2) || (x > 5 && n < 4) || x <= n || n < 4) {       printf( %d n , (x == n && x != 1)                           ? k = 5                      : n / x >= 2                                 ? k = 1                      : x < n                                      ? k = 3                      : (x == 4 && n == 3)                         ? k = 9                      : ((x == 5 && n == 3) || (x == 3 && n == 2)) ? k = 11                                                                   : k = -1);       exit(0);     }     y = x -= n - 2;     y1 = x1 += n - 2;     k = 4;     if (x <= n / 2) {       y1 = x1 += x;       y = x -= x;       printf( %d , k + 1);       exit(0);     }     while (y > n) {       y = x -= n / 2 - 1;       y1 = x1 += n / 2 - 1;       k += 2;     }     y == n ? k += 5 : k += 3;     y1 = x1 += x;     y = x -= x;     printf( %d , k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, bool> fuck;   int n, m;   char s[5];   int x, y;   long long ans;   int dx, dy;   int main() {     scanf( %d%d , &n, &m);     scanf( %d%d , &x, &y);     scanf( %s , s + 1);     if (s[1] ==  U )       dx = -1;     else       dx = 1;     if (s[2] ==  L )       dy = -1;     else       dy = 1;     int fis = 0;     int test = 1000000;     while (test--) {       pair<int, int> now = pair<int, int>(x, y);       if (!fuck[now]) {         fuck[now] = 1;         fis++;       }       if (fis == n + m - 2) {         cout << ans + 1;         return 0;       }       int xl, yl, l;       int ex, ey;       if (dx == -1)         xl = x - 1;       else         xl = n - x;       if (dy == -1)         yl = y - 1;       else         yl = m - y;       if (xl < yl) {         ex = -dx;         ey = dy;         l = xl;       } else {         if (xl > yl) {           ey = -dy;           ex = dx;           l = yl;         } else {           ex = -dx;           ey = -dy;           l = yl;         }       }       x += dx * l;       y += dy * l;       ans += 1ll * l;       dx = ex;       dy = ey;     }     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int cost(long long int h[], long long int n, long long int a,                      long long int r, long long int m, long long int x) {     long long int plus = 0, minus = 0;     for (int i = 0; i < n; i++) {       if (h[i] > x) {         minus += h[i] - x;       } else if (h[i] < x) {         plus += x - h[i];       }     }     if (m > a + r) {       return (a * plus + r * minus);     } else {       return (min(plus, minus) * m + (plus - min(plus, minus)) * a +               (minus - min(plus, minus)) * r);     }   }   int main() {     long long int n, a, r, m;     cin >> n >> a >> r >> m;     long long int h[n], start = 0, end = 0;     for (int i = 0; i < n; i++) {       cin >> h[i];       end = max(end, h[i]);     }     long long int ans = 1e18;     while (start <= end) {       long long int mid = (start + end) >> 1, mid_1;       long long int c_mid = cost(h, n, a, r, m, mid);       if (c_mid < ans) {         ans = c_mid;       }       mid_1 = mid + 1;       long long int c_mid_1 = cost(h, n, a, r, m, mid_1);       if (c_mid_1 > c_mid) {         end = mid - 1;       } else {         start = mid + 1;       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-9, PI = acos(-1.);   const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;   const int N = 2e5 + 5, SQ = 500;   int n, q, a[N], l[N], r[N], p[N];   long long ans[N], c[1000005], s;   void add(int x) {     int v = a[x];     s -= c[v] * c[v] * v;     c[v]++;     s += c[v] * c[v] * v;   }   void rem(int x) {     int v = a[x];     s -= c[v] * c[v] * v;     c[v]--;     s += c[v] * c[v] * v;   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 0; i < q; ++i) scanf( %d%d , &l[i], &r[i]), p[i] = i;     sort(p, p + q, [](int a, int b) {       return l[a] / SQ == l[b] / SQ ? r[a] < r[b] : l[a] < l[b];     });     int cl = 0, cr = 0;     for (int i = 0; i < q; ++i) {       int cur = p[i];       while (cl < l[cur]) rem(cl++);       while (cl > l[cur]) add(--cl);       while (cr < r[cur]) add(++cr);       while (cr > r[cur]) rem(cr--);       ans[cur] = s;     }     for (int i = 0; i < q; ++i) printf( %lld n , ans[i]);     return 0;   }