This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#define N 300000
#define A 300000
#define N_ 23
long long min(long long a, long long b) { return a < b ? a : b; }
long long f(int l, int r) {
return (long long) (r - l + 1) * (r - l + 2) / 2;
}
int aa[N];
long long solve(int l, int r) {
static int kk[A + 1], nn[1 << N_];
static long long dp[1 << N_];
int n, i, a, b;
memset(kk, 0, (A + 1) * sizeof *kk);
for (i = l; i <= r; i++)
kk[aa[i]]++;
n = 0;
for (a = 0; a <= A; a++)
if (kk[a] > 0)
kk[n++] = kk[a];
nn[0] = 0;
for (i = 0; i < n; i++)
nn[1 << i] = kk[i];
for (b = 1; b < 1 << n; b++)
nn[b] = nn[b & b - 1] + nn[b & -b];
memset(dp, 0x3f, (1 << n) * sizeof *dp), dp[0] = 0;
for (b = 0; b < 1 << n; b++)
for (i = 0; i < n; i++)
if ((b & 1 << i) == 0)
dp[b | 1 << i] = min(dp[b | 1 << i], dp[b]
+ (f(l, r) - f(l, l + nn[b] - 1) - f(l + nn[b | 1 << i], r)));
return dp[(1 << n) - 1];
}
int main() {
int n, q, i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
while (q--) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
printf("%lld\n", solve(l, r));
}
return 0;
}
Compilation message (stderr)
diversity.c: In function 'solve':
diversity.c:32:20: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
32 | nn[b] = nn[b & b - 1] + nn[b & -b];
| ~~^~~
diversity.c: In function 'main':
diversity.c:45:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
diversity.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
diversity.c:51:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &l, &r), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |