답안 #1034284

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1034284 2024-07-25T11:45:48 Z vjudge1 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 25136 KB
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;

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

const int N = 3e5;

int n, q, a[N];
map<int, int> mp[N];

signed main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--; r--;

    auto it = mp[l].find(r);
    if (it != mp[l].end()) {
      cout << it->second << "\n";
      continue;
    }

    int sz = r-l+1;
    vector<int> vt(sz);
    for (int i = l; i <= r; i++) {
      vt[i-l] = a[i];
    }
    sort(all(vt));

    //for (int& x : vt) cerr << x << " ";
    //cerr << endl;

    vector<int> s;
    int cnt = 0;
    for (int i = 0; i < sz; i++) {
      if (i && vt[i] != vt[i-1]) {
        s.push_back(cnt);
        cnt = 1;
      }
      else cnt++;
    }
    s.push_back(cnt);

    sort(all(s));
  
    //for (int& x : s) cerr << x << " ";
    //cerr << endl;

    deque<int> dq;
    int szS = s.size();
    for (int i = 0; i < szS; i+=2) {
      dq.push_back(s[i]);
    }
    for (int i = szS - (szS % 2 ? 2 : 1); i >= 0; i-=2) {
      dq.push_back(s[i]);
    }

    //for (int& x : dq) cerr << x << " ";
    //cerr << endl;

    int ans = 0;
    int L = 1;
    while (!dq.empty()) {
      int x = dq.front() - 1; dq.pop_front();

      // sumar [L, L+x-1]
      ans += (L+x-1)*(L+x)/2 - (L-1)*(L)/2;
      L += x;

      ans += L * (sz-L+1);
      L++;
    }

    mp[l][r] = ans;
    cout <<  ans << "\n";
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14424 KB Output is correct
2 Correct 6 ms 14464 KB Output is correct
3 Correct 6 ms 14556 KB Output is correct
4 Correct 7 ms 14424 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14428 KB Output is correct
7 Correct 6 ms 14532 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 6 ms 14364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14484 KB Output is correct
3 Correct 8 ms 14668 KB Output is correct
4 Correct 16 ms 16216 KB Output is correct
5 Correct 31 ms 18012 KB Output is correct
6 Correct 37 ms 19804 KB Output is correct
7 Correct 35 ms 19820 KB Output is correct
8 Correct 36 ms 19720 KB Output is correct
9 Correct 35 ms 19688 KB Output is correct
10 Correct 36 ms 19632 KB Output is correct
11 Correct 35 ms 19804 KB Output is correct
12 Correct 37 ms 19804 KB Output is correct
13 Correct 40 ms 19940 KB Output is correct
14 Correct 47 ms 19804 KB Output is correct
15 Correct 38 ms 19800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14484 KB Output is correct
3 Correct 8 ms 14668 KB Output is correct
4 Correct 16 ms 16216 KB Output is correct
5 Correct 31 ms 18012 KB Output is correct
6 Correct 37 ms 19804 KB Output is correct
7 Correct 35 ms 19820 KB Output is correct
8 Correct 36 ms 19720 KB Output is correct
9 Correct 35 ms 19688 KB Output is correct
10 Correct 36 ms 19632 KB Output is correct
11 Correct 35 ms 19804 KB Output is correct
12 Correct 37 ms 19804 KB Output is correct
13 Correct 40 ms 19940 KB Output is correct
14 Correct 47 ms 19804 KB Output is correct
15 Correct 38 ms 19800 KB Output is correct
16 Correct 6 ms 14428 KB Output is correct
17 Correct 6 ms 14428 KB Output is correct
18 Correct 8 ms 14684 KB Output is correct
19 Correct 16 ms 16216 KB Output is correct
20 Correct 27 ms 18012 KB Output is correct
21 Correct 37 ms 19940 KB Output is correct
22 Correct 42 ms 19792 KB Output is correct
23 Correct 41 ms 19936 KB Output is correct
24 Correct 45 ms 19792 KB Output is correct
25 Correct 37 ms 19820 KB Output is correct
26 Correct 37 ms 19800 KB Output is correct
27 Correct 37 ms 19804 KB Output is correct
28 Correct 42 ms 20016 KB Output is correct
29 Correct 41 ms 19804 KB Output is correct
30 Correct 38 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14484 KB Output is correct
3 Correct 8 ms 14668 KB Output is correct
4 Correct 16 ms 16216 KB Output is correct
5 Correct 31 ms 18012 KB Output is correct
6 Correct 37 ms 19804 KB Output is correct
7 Correct 35 ms 19820 KB Output is correct
8 Correct 36 ms 19720 KB Output is correct
9 Correct 35 ms 19688 KB Output is correct
10 Correct 36 ms 19632 KB Output is correct
11 Correct 35 ms 19804 KB Output is correct
12 Correct 37 ms 19804 KB Output is correct
13 Correct 40 ms 19940 KB Output is correct
14 Correct 47 ms 19804 KB Output is correct
15 Correct 38 ms 19800 KB Output is correct
16 Correct 6 ms 14428 KB Output is correct
17 Correct 6 ms 14428 KB Output is correct
18 Correct 8 ms 14684 KB Output is correct
19 Correct 16 ms 16216 KB Output is correct
20 Correct 27 ms 18012 KB Output is correct
21 Correct 37 ms 19940 KB Output is correct
22 Correct 42 ms 19792 KB Output is correct
23 Correct 41 ms 19936 KB Output is correct
24 Correct 45 ms 19792 KB Output is correct
25 Correct 37 ms 19820 KB Output is correct
26 Correct 37 ms 19800 KB Output is correct
27 Correct 37 ms 19804 KB Output is correct
28 Correct 42 ms 20016 KB Output is correct
29 Correct 41 ms 19804 KB Output is correct
30 Correct 38 ms 19804 KB Output is correct
31 Correct 6 ms 14424 KB Output is correct
32 Correct 6 ms 14428 KB Output is correct
33 Correct 6 ms 14580 KB Output is correct
34 Correct 7 ms 14684 KB Output is correct
35 Correct 8 ms 14684 KB Output is correct
36 Correct 7 ms 14684 KB Output is correct
37 Correct 11 ms 15452 KB Output is correct
38 Correct 11 ms 15484 KB Output is correct
39 Correct 21 ms 16648 KB Output is correct
40 Correct 30 ms 18268 KB Output is correct
41 Correct 46 ms 20392 KB Output is correct
42 Correct 41 ms 20312 KB Output is correct
43 Correct 43 ms 20368 KB Output is correct
44 Correct 44 ms 20304 KB Output is correct
45 Correct 42 ms 20304 KB Output is correct
46 Correct 40 ms 20380 KB Output is correct
47 Correct 40 ms 20396 KB Output is correct
48 Correct 40 ms 20308 KB Output is correct
49 Correct 41 ms 20312 KB Output is correct
50 Correct 41 ms 20316 KB Output is correct
51 Correct 41 ms 20316 KB Output is correct
52 Correct 47 ms 20580 KB Output is correct
53 Correct 45 ms 20372 KB Output is correct
54 Correct 41 ms 20388 KB Output is correct
55 Correct 41 ms 20312 KB Output is correct
56 Correct 46 ms 20380 KB Output is correct
57 Correct 42 ms 20316 KB Output is correct
58 Correct 41 ms 20392 KB Output is correct
59 Correct 41 ms 20308 KB Output is correct
60 Correct 42 ms 20316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14424 KB Output is correct
2 Correct 6 ms 14464 KB Output is correct
3 Correct 6 ms 14556 KB Output is correct
4 Correct 7 ms 14424 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14428 KB Output is correct
7 Correct 6 ms 14532 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 6 ms 14364 KB Output is correct
11 Correct 6 ms 14424 KB Output is correct
12 Correct 6 ms 14484 KB Output is correct
13 Correct 8 ms 14668 KB Output is correct
14 Correct 16 ms 16216 KB Output is correct
15 Correct 31 ms 18012 KB Output is correct
16 Correct 37 ms 19804 KB Output is correct
17 Correct 35 ms 19820 KB Output is correct
18 Correct 36 ms 19720 KB Output is correct
19 Correct 35 ms 19688 KB Output is correct
20 Correct 36 ms 19632 KB Output is correct
21 Correct 35 ms 19804 KB Output is correct
22 Correct 37 ms 19804 KB Output is correct
23 Correct 40 ms 19940 KB Output is correct
24 Correct 47 ms 19804 KB Output is correct
25 Correct 38 ms 19800 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 6 ms 14428 KB Output is correct
28 Correct 8 ms 14684 KB Output is correct
29 Correct 16 ms 16216 KB Output is correct
30 Correct 27 ms 18012 KB Output is correct
31 Correct 37 ms 19940 KB Output is correct
32 Correct 42 ms 19792 KB Output is correct
33 Correct 41 ms 19936 KB Output is correct
34 Correct 45 ms 19792 KB Output is correct
35 Correct 37 ms 19820 KB Output is correct
36 Correct 37 ms 19800 KB Output is correct
37 Correct 37 ms 19804 KB Output is correct
38 Correct 42 ms 20016 KB Output is correct
39 Correct 41 ms 19804 KB Output is correct
40 Correct 38 ms 19804 KB Output is correct
41 Correct 6 ms 14424 KB Output is correct
42 Correct 6 ms 14428 KB Output is correct
43 Correct 6 ms 14580 KB Output is correct
44 Correct 7 ms 14684 KB Output is correct
45 Correct 8 ms 14684 KB Output is correct
46 Correct 7 ms 14684 KB Output is correct
47 Correct 11 ms 15452 KB Output is correct
48 Correct 11 ms 15484 KB Output is correct
49 Correct 21 ms 16648 KB Output is correct
50 Correct 30 ms 18268 KB Output is correct
51 Correct 46 ms 20392 KB Output is correct
52 Correct 41 ms 20312 KB Output is correct
53 Correct 43 ms 20368 KB Output is correct
54 Correct 44 ms 20304 KB Output is correct
55 Correct 42 ms 20304 KB Output is correct
56 Correct 40 ms 20380 KB Output is correct
57 Correct 40 ms 20396 KB Output is correct
58 Correct 40 ms 20308 KB Output is correct
59 Correct 41 ms 20312 KB Output is correct
60 Correct 41 ms 20316 KB Output is correct
61 Correct 41 ms 20316 KB Output is correct
62 Correct 47 ms 20580 KB Output is correct
63 Correct 45 ms 20372 KB Output is correct
64 Correct 41 ms 20388 KB Output is correct
65 Correct 41 ms 20312 KB Output is correct
66 Correct 46 ms 20380 KB Output is correct
67 Correct 42 ms 20316 KB Output is correct
68 Correct 41 ms 20392 KB Output is correct
69 Correct 41 ms 20308 KB Output is correct
70 Correct 42 ms 20316 KB Output is correct
71 Correct 12 ms 15448 KB Output is correct
72 Correct 12 ms 15452 KB Output is correct
73 Correct 12 ms 15452 KB Output is correct
74 Correct 12 ms 15444 KB Output is correct
75 Correct 12 ms 15452 KB Output is correct
76 Correct 23 ms 16492 KB Output is correct
77 Correct 19 ms 16476 KB Output is correct
78 Correct 19 ms 16472 KB Output is correct
79 Correct 21 ms 16872 KB Output is correct
80 Correct 20 ms 16476 KB Output is correct
81 Correct 33 ms 18716 KB Output is correct
82 Correct 33 ms 18780 KB Output is correct
83 Correct 32 ms 18772 KB Output is correct
84 Correct 33 ms 18772 KB Output is correct
85 Correct 32 ms 18648 KB Output is correct
86 Correct 39 ms 18976 KB Output is correct
87 Correct 39 ms 19028 KB Output is correct
88 Correct 37 ms 19024 KB Output is correct
89 Correct 34 ms 19028 KB Output is correct
90 Correct 34 ms 19036 KB Output is correct
91 Correct 58 ms 22360 KB Output is correct
92 Correct 55 ms 22480 KB Output is correct
93 Correct 58 ms 22476 KB Output is correct
94 Correct 52 ms 22476 KB Output is correct
95 Correct 54 ms 22476 KB Output is correct
96 Correct 63 ms 24264 KB Output is correct
97 Correct 65 ms 24312 KB Output is correct
98 Correct 64 ms 24208 KB Output is correct
99 Correct 63 ms 24216 KB Output is correct
100 Correct 64 ms 24320 KB Output is correct
101 Correct 64 ms 24264 KB Output is correct
102 Correct 78 ms 24152 KB Output is correct
103 Correct 65 ms 24112 KB Output is correct
104 Correct 63 ms 24264 KB Output is correct
105 Correct 64 ms 24324 KB Output is correct
106 Correct 65 ms 25116 KB Output is correct
107 Correct 66 ms 25036 KB Output is correct
108 Correct 65 ms 25044 KB Output is correct
109 Correct 67 ms 25048 KB Output is correct
110 Correct 66 ms 25136 KB Output is correct
111 Correct 65 ms 25032 KB Output is correct
112 Correct 66 ms 25136 KB Output is correct
113 Correct 66 ms 25040 KB Output is correct
114 Correct 66 ms 25032 KB Output is correct
115 Correct 65 ms 25036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14424 KB Output is correct
2 Correct 6 ms 14464 KB Output is correct
3 Correct 6 ms 14556 KB Output is correct
4 Correct 7 ms 14424 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14428 KB Output is correct
7 Correct 6 ms 14532 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 6 ms 14364 KB Output is correct
11 Correct 6 ms 14424 KB Output is correct
12 Correct 6 ms 14484 KB Output is correct
13 Correct 8 ms 14668 KB Output is correct
14 Correct 16 ms 16216 KB Output is correct
15 Correct 31 ms 18012 KB Output is correct
16 Correct 37 ms 19804 KB Output is correct
17 Correct 35 ms 19820 KB Output is correct
18 Correct 36 ms 19720 KB Output is correct
19 Correct 35 ms 19688 KB Output is correct
20 Correct 36 ms 19632 KB Output is correct
21 Correct 35 ms 19804 KB Output is correct
22 Correct 37 ms 19804 KB Output is correct
23 Correct 40 ms 19940 KB Output is correct
24 Correct 47 ms 19804 KB Output is correct
25 Correct 38 ms 19800 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 6 ms 14428 KB Output is correct
28 Correct 8 ms 14684 KB Output is correct
29 Correct 16 ms 16216 KB Output is correct
30 Correct 27 ms 18012 KB Output is correct
31 Correct 37 ms 19940 KB Output is correct
32 Correct 42 ms 19792 KB Output is correct
33 Correct 41 ms 19936 KB Output is correct
34 Correct 45 ms 19792 KB Output is correct
35 Correct 37 ms 19820 KB Output is correct
36 Correct 37 ms 19800 KB Output is correct
37 Correct 37 ms 19804 KB Output is correct
38 Correct 42 ms 20016 KB Output is correct
39 Correct 41 ms 19804 KB Output is correct
40 Correct 38 ms 19804 KB Output is correct
41 Correct 6 ms 14424 KB Output is correct
42 Correct 6 ms 14428 KB Output is correct
43 Correct 6 ms 14580 KB Output is correct
44 Correct 7 ms 14684 KB Output is correct
45 Correct 8 ms 14684 KB Output is correct
46 Correct 7 ms 14684 KB Output is correct
47 Correct 11 ms 15452 KB Output is correct
48 Correct 11 ms 15484 KB Output is correct
49 Correct 21 ms 16648 KB Output is correct
50 Correct 30 ms 18268 KB Output is correct
51 Correct 46 ms 20392 KB Output is correct
52 Correct 41 ms 20312 KB Output is correct
53 Correct 43 ms 20368 KB Output is correct
54 Correct 44 ms 20304 KB Output is correct
55 Correct 42 ms 20304 KB Output is correct
56 Correct 40 ms 20380 KB Output is correct
57 Correct 40 ms 20396 KB Output is correct
58 Correct 40 ms 20308 KB Output is correct
59 Correct 41 ms 20312 KB Output is correct
60 Correct 41 ms 20316 KB Output is correct
61 Correct 41 ms 20316 KB Output is correct
62 Correct 47 ms 20580 KB Output is correct
63 Correct 45 ms 20372 KB Output is correct
64 Correct 41 ms 20388 KB Output is correct
65 Correct 41 ms 20312 KB Output is correct
66 Correct 46 ms 20380 KB Output is correct
67 Correct 42 ms 20316 KB Output is correct
68 Correct 41 ms 20392 KB Output is correct
69 Correct 41 ms 20308 KB Output is correct
70 Correct 42 ms 20316 KB Output is correct
71 Correct 12 ms 15448 KB Output is correct
72 Correct 12 ms 15452 KB Output is correct
73 Correct 12 ms 15452 KB Output is correct
74 Correct 12 ms 15444 KB Output is correct
75 Correct 12 ms 15452 KB Output is correct
76 Correct 23 ms 16492 KB Output is correct
77 Correct 19 ms 16476 KB Output is correct
78 Correct 19 ms 16472 KB Output is correct
79 Correct 21 ms 16872 KB Output is correct
80 Correct 20 ms 16476 KB Output is correct
81 Correct 33 ms 18716 KB Output is correct
82 Correct 33 ms 18780 KB Output is correct
83 Correct 32 ms 18772 KB Output is correct
84 Correct 33 ms 18772 KB Output is correct
85 Correct 32 ms 18648 KB Output is correct
86 Correct 39 ms 18976 KB Output is correct
87 Correct 39 ms 19028 KB Output is correct
88 Correct 37 ms 19024 KB Output is correct
89 Correct 34 ms 19028 KB Output is correct
90 Correct 34 ms 19036 KB Output is correct
91 Correct 58 ms 22360 KB Output is correct
92 Correct 55 ms 22480 KB Output is correct
93 Correct 58 ms 22476 KB Output is correct
94 Correct 52 ms 22476 KB Output is correct
95 Correct 54 ms 22476 KB Output is correct
96 Correct 63 ms 24264 KB Output is correct
97 Correct 65 ms 24312 KB Output is correct
98 Correct 64 ms 24208 KB Output is correct
99 Correct 63 ms 24216 KB Output is correct
100 Correct 64 ms 24320 KB Output is correct
101 Correct 64 ms 24264 KB Output is correct
102 Correct 78 ms 24152 KB Output is correct
103 Correct 65 ms 24112 KB Output is correct
104 Correct 63 ms 24264 KB Output is correct
105 Correct 64 ms 24324 KB Output is correct
106 Correct 65 ms 25116 KB Output is correct
107 Correct 66 ms 25036 KB Output is correct
108 Correct 65 ms 25044 KB Output is correct
109 Correct 67 ms 25048 KB Output is correct
110 Correct 66 ms 25136 KB Output is correct
111 Correct 65 ms 25032 KB Output is correct
112 Correct 66 ms 25136 KB Output is correct
113 Correct 66 ms 25040 KB Output is correct
114 Correct 66 ms 25032 KB Output is correct
115 Correct 65 ms 25036 KB Output is correct
116 Execution timed out 7017 ms 18628 KB Time limit exceeded
117 Halted 0 ms 0 KB -