Submission #489130

# Submission time Handle Problem Language Result Execution time Memory
489130 2021-11-21T08:59:19 Z Wayne_Yan Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 16452 KB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
typedef int64_t ll;
typedef long double ld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define RF(n) RFi(i,n)
#define RFi(i,n) RFl(i,0,n)
#define RFl(i,l,n) for(int i=n-1;i>=l;i--)
#define all(v) begin(v),end(v)
#define siz(v) (ll(v.size()))
#define get_pos(v,x) (lower_bound(all(v),x)-begin(v))
#define sort_uni(v) sort(begin(v),end(v)),v.erase(unique(begin(v),end(v)),end(v))
#define mem(v,x) memset(v,x,sizeof v)
#define ff first
#define ss second
#define mid ((l+r)>>1)
#define RAN(a,b) uniform_int_distribution<int> (a, b)(rng)
#define debug(x) (cerr << (#x) << " = " << x << "\n")
template <typename T> using max_heap = __gnu_pbds::priority_queue<T,less<T> >;
template <typename T> using min_heap = __gnu_pbds::priority_queue<T,greater<T> >;
template <typename T> using rbt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

int n,q,k;
const int maxN = 3e5+10;

bool cmp(pair<pii,int> a, pair<pii,int> b){
  if((a.ff.ff / k) == (b.ff.ff / k)) return a.ff.ss < b.ff.ss;
  return (a.ff.ff / k) < (b.ff.ff / k);
}
multiset<int> s;
int times[maxN];
signed main(){
  cin >> n >> q;
  k = n / (int)sqrt(q);


  int arr[n];
  F(n) cin >> arr[i];
  pair<pii, int> queries[q];
  F(q){
    int l,r;
    cin >> l >> r;
    l--; r--;
    queries[i] = mp(mp(l, r), i);
  }
  sort(queries, queries + q, cmp);

  int fin[q];
  int cl = 0; int cr = -1;
  F(q){
    int nl = queries[i].ff.ff; int nr = queries[i].ff.ss;
    int id = queries[i].ss;

    // process the right bound change
    if(nr > cr){
      while(nr > cr){
        cr++;
        if(times[arr[cr]] != 0)
          s.erase(s.lower_bound(times[arr[cr]]));
        times[arr[cr]]++;
        s.insert(times[arr[cr]]);
      }
    }else if(nr < cr){
      while(nr < cr){
        s.erase(s.lower_bound(times[arr[cr]]));
        times[arr[cr]] --;
        if(times[arr[cr]] != 0){
          s.insert(times[arr[cr]]);
        }
        cr--;
      }
    }

    // process the left bound change

    if(nl > cl){
      while(nl > cl){
        s.erase(s.lower_bound(times[arr[cl]]));
        times[arr[cl]]--;
        if(times[arr[cl]] != 0){
          s.insert(times[arr[cl]]);
        }
        cl++;
      }
    }else if(nl < cl){
      while(nl < cl){
        cl--;
        if(times[arr[cl]] != 0){
          s.erase(s.lower_bound(times[arr[cl]]));
        }
        times[arr[cl]]++;
        s.insert(times[arr[cl]]);
      }
    }


    // answer the query

    int ans = (cr-cl+1) * (cr-cl+2) / 2;
    int ldist = 0, rdist = 0;
    auto it = s.begin();
    Fi(j, (int) s.size() - 1){
      if(ldist > rdist) swap(ldist, rdist);
      ldist += *it;
      ans += ((cr-cl+1) - ldist) * ldist;
      it++;
    }
    fin[id] = ans;
  }


  F(q){
    printf("%lld\n", fin[i]);
  }




  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 20 ms 1152 KB Output is correct
5 Correct 49 ms 1868 KB Output is correct
6 Correct 61 ms 2700 KB Output is correct
7 Correct 55 ms 2756 KB Output is correct
8 Correct 59 ms 2756 KB Output is correct
9 Correct 58 ms 2692 KB Output is correct
10 Correct 60 ms 2696 KB Output is correct
11 Correct 59 ms 2636 KB Output is correct
12 Correct 61 ms 2696 KB Output is correct
13 Correct 66 ms 2636 KB Output is correct
14 Correct 79 ms 2696 KB Output is correct
15 Correct 58 ms 2704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 20 ms 1152 KB Output is correct
5 Correct 49 ms 1868 KB Output is correct
6 Correct 61 ms 2700 KB Output is correct
7 Correct 55 ms 2756 KB Output is correct
8 Correct 59 ms 2756 KB Output is correct
9 Correct 58 ms 2692 KB Output is correct
10 Correct 60 ms 2696 KB Output is correct
11 Correct 59 ms 2636 KB Output is correct
12 Correct 61 ms 2696 KB Output is correct
13 Correct 66 ms 2636 KB Output is correct
14 Correct 79 ms 2696 KB Output is correct
15 Correct 58 ms 2704 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 3 ms 332 KB Output is correct
19 Correct 23 ms 1156 KB Output is correct
20 Correct 45 ms 1912 KB Output is correct
21 Correct 67 ms 2696 KB Output is correct
22 Correct 67 ms 2636 KB Output is correct
23 Correct 67 ms 2636 KB Output is correct
24 Correct 76 ms 2696 KB Output is correct
25 Correct 77 ms 2696 KB Output is correct
26 Correct 66 ms 2636 KB Output is correct
27 Correct 67 ms 2636 KB Output is correct
28 Correct 65 ms 2636 KB Output is correct
29 Correct 68 ms 2708 KB Output is correct
30 Correct 81 ms 2692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 20 ms 1152 KB Output is correct
5 Correct 49 ms 1868 KB Output is correct
6 Correct 61 ms 2700 KB Output is correct
7 Correct 55 ms 2756 KB Output is correct
8 Correct 59 ms 2756 KB Output is correct
9 Correct 58 ms 2692 KB Output is correct
10 Correct 60 ms 2696 KB Output is correct
11 Correct 59 ms 2636 KB Output is correct
12 Correct 61 ms 2696 KB Output is correct
13 Correct 66 ms 2636 KB Output is correct
14 Correct 79 ms 2696 KB Output is correct
15 Correct 58 ms 2704 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 3 ms 332 KB Output is correct
19 Correct 23 ms 1156 KB Output is correct
20 Correct 45 ms 1912 KB Output is correct
21 Correct 67 ms 2696 KB Output is correct
22 Correct 67 ms 2636 KB Output is correct
23 Correct 67 ms 2636 KB Output is correct
24 Correct 76 ms 2696 KB Output is correct
25 Correct 77 ms 2696 KB Output is correct
26 Correct 66 ms 2636 KB Output is correct
27 Correct 67 ms 2636 KB Output is correct
28 Correct 65 ms 2636 KB Output is correct
29 Correct 68 ms 2708 KB Output is correct
30 Correct 81 ms 2692 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 4 ms 332 KB Output is correct
36 Correct 4 ms 332 KB Output is correct
37 Correct 16 ms 784 KB Output is correct
38 Correct 17 ms 812 KB Output is correct
39 Correct 38 ms 1152 KB Output is correct
40 Correct 69 ms 1924 KB Output is correct
41 Correct 101 ms 2748 KB Output is correct
42 Correct 106 ms 2756 KB Output is correct
43 Correct 102 ms 2756 KB Output is correct
44 Correct 100 ms 2708 KB Output is correct
45 Correct 99 ms 2696 KB Output is correct
46 Correct 101 ms 2700 KB Output is correct
47 Correct 100 ms 2856 KB Output is correct
48 Correct 102 ms 2756 KB Output is correct
49 Correct 106 ms 2708 KB Output is correct
50 Correct 100 ms 2700 KB Output is correct
51 Correct 101 ms 2696 KB Output is correct
52 Correct 116 ms 2828 KB Output is correct
53 Correct 109 ms 2756 KB Output is correct
54 Correct 99 ms 2708 KB Output is correct
55 Correct 99 ms 2756 KB Output is correct
56 Correct 101 ms 2700 KB Output is correct
57 Correct 102 ms 2700 KB Output is correct
58 Correct 103 ms 2756 KB Output is correct
59 Correct 99 ms 2692 KB Output is correct
60 Correct 99 ms 2700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 20 ms 1152 KB Output is correct
15 Correct 49 ms 1868 KB Output is correct
16 Correct 61 ms 2700 KB Output is correct
17 Correct 55 ms 2756 KB Output is correct
18 Correct 59 ms 2756 KB Output is correct
19 Correct 58 ms 2692 KB Output is correct
20 Correct 60 ms 2696 KB Output is correct
21 Correct 59 ms 2636 KB Output is correct
22 Correct 61 ms 2696 KB Output is correct
23 Correct 66 ms 2636 KB Output is correct
24 Correct 79 ms 2696 KB Output is correct
25 Correct 58 ms 2704 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 3 ms 332 KB Output is correct
29 Correct 23 ms 1156 KB Output is correct
30 Correct 45 ms 1912 KB Output is correct
31 Correct 67 ms 2696 KB Output is correct
32 Correct 67 ms 2636 KB Output is correct
33 Correct 67 ms 2636 KB Output is correct
34 Correct 76 ms 2696 KB Output is correct
35 Correct 77 ms 2696 KB Output is correct
36 Correct 66 ms 2636 KB Output is correct
37 Correct 67 ms 2636 KB Output is correct
38 Correct 65 ms 2636 KB Output is correct
39 Correct 68 ms 2708 KB Output is correct
40 Correct 81 ms 2692 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 3 ms 332 KB Output is correct
45 Correct 4 ms 332 KB Output is correct
46 Correct 4 ms 332 KB Output is correct
47 Correct 16 ms 784 KB Output is correct
48 Correct 17 ms 812 KB Output is correct
49 Correct 38 ms 1152 KB Output is correct
50 Correct 69 ms 1924 KB Output is correct
51 Correct 101 ms 2748 KB Output is correct
52 Correct 106 ms 2756 KB Output is correct
53 Correct 102 ms 2756 KB Output is correct
54 Correct 100 ms 2708 KB Output is correct
55 Correct 99 ms 2696 KB Output is correct
56 Correct 101 ms 2700 KB Output is correct
57 Correct 100 ms 2856 KB Output is correct
58 Correct 102 ms 2756 KB Output is correct
59 Correct 106 ms 2708 KB Output is correct
60 Correct 100 ms 2700 KB Output is correct
61 Correct 101 ms 2696 KB Output is correct
62 Correct 116 ms 2828 KB Output is correct
63 Correct 109 ms 2756 KB Output is correct
64 Correct 99 ms 2708 KB Output is correct
65 Correct 99 ms 2756 KB Output is correct
66 Correct 101 ms 2700 KB Output is correct
67 Correct 102 ms 2700 KB Output is correct
68 Correct 103 ms 2756 KB Output is correct
69 Correct 99 ms 2692 KB Output is correct
70 Correct 99 ms 2700 KB Output is correct
71 Correct 19 ms 764 KB Output is correct
72 Correct 25 ms 844 KB Output is correct
73 Correct 21 ms 760 KB Output is correct
74 Correct 22 ms 752 KB Output is correct
75 Correct 20 ms 756 KB Output is correct
76 Correct 41 ms 1400 KB Output is correct
77 Correct 41 ms 1384 KB Output is correct
78 Correct 42 ms 1376 KB Output is correct
79 Correct 41 ms 1388 KB Output is correct
80 Correct 42 ms 1396 KB Output is correct
81 Correct 88 ms 2428 KB Output is correct
82 Correct 89 ms 2456 KB Output is correct
83 Correct 89 ms 2436 KB Output is correct
84 Correct 96 ms 2436 KB Output is correct
85 Correct 88 ms 2448 KB Output is correct
86 Correct 96 ms 2932 KB Output is correct
87 Correct 102 ms 2940 KB Output is correct
88 Correct 105 ms 2996 KB Output is correct
89 Correct 96 ms 2936 KB Output is correct
90 Correct 95 ms 3012 KB Output is correct
91 Correct 157 ms 8132 KB Output is correct
92 Correct 168 ms 8260 KB Output is correct
93 Correct 159 ms 8132 KB Output is correct
94 Correct 157 ms 8064 KB Output is correct
95 Correct 184 ms 8132 KB Output is correct
96 Correct 155 ms 13676 KB Output is correct
97 Correct 161 ms 13580 KB Output is correct
98 Correct 164 ms 13588 KB Output is correct
99 Correct 155 ms 13572 KB Output is correct
100 Correct 156 ms 13636 KB Output is correct
101 Correct 157 ms 13656 KB Output is correct
102 Correct 153 ms 13572 KB Output is correct
103 Correct 153 ms 13572 KB Output is correct
104 Correct 167 ms 13704 KB Output is correct
105 Correct 155 ms 13576 KB Output is correct
106 Correct 149 ms 16392 KB Output is correct
107 Correct 158 ms 16276 KB Output is correct
108 Correct 161 ms 16392 KB Output is correct
109 Correct 148 ms 16324 KB Output is correct
110 Correct 160 ms 16276 KB Output is correct
111 Correct 186 ms 16388 KB Output is correct
112 Correct 149 ms 16452 KB Output is correct
113 Correct 160 ms 16336 KB Output is correct
114 Correct 160 ms 16388 KB Output is correct
115 Correct 150 ms 16388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 20 ms 1152 KB Output is correct
15 Correct 49 ms 1868 KB Output is correct
16 Correct 61 ms 2700 KB Output is correct
17 Correct 55 ms 2756 KB Output is correct
18 Correct 59 ms 2756 KB Output is correct
19 Correct 58 ms 2692 KB Output is correct
20 Correct 60 ms 2696 KB Output is correct
21 Correct 59 ms 2636 KB Output is correct
22 Correct 61 ms 2696 KB Output is correct
23 Correct 66 ms 2636 KB Output is correct
24 Correct 79 ms 2696 KB Output is correct
25 Correct 58 ms 2704 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 3 ms 332 KB Output is correct
29 Correct 23 ms 1156 KB Output is correct
30 Correct 45 ms 1912 KB Output is correct
31 Correct 67 ms 2696 KB Output is correct
32 Correct 67 ms 2636 KB Output is correct
33 Correct 67 ms 2636 KB Output is correct
34 Correct 76 ms 2696 KB Output is correct
35 Correct 77 ms 2696 KB Output is correct
36 Correct 66 ms 2636 KB Output is correct
37 Correct 67 ms 2636 KB Output is correct
38 Correct 65 ms 2636 KB Output is correct
39 Correct 68 ms 2708 KB Output is correct
40 Correct 81 ms 2692 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 3 ms 332 KB Output is correct
45 Correct 4 ms 332 KB Output is correct
46 Correct 4 ms 332 KB Output is correct
47 Correct 16 ms 784 KB Output is correct
48 Correct 17 ms 812 KB Output is correct
49 Correct 38 ms 1152 KB Output is correct
50 Correct 69 ms 1924 KB Output is correct
51 Correct 101 ms 2748 KB Output is correct
52 Correct 106 ms 2756 KB Output is correct
53 Correct 102 ms 2756 KB Output is correct
54 Correct 100 ms 2708 KB Output is correct
55 Correct 99 ms 2696 KB Output is correct
56 Correct 101 ms 2700 KB Output is correct
57 Correct 100 ms 2856 KB Output is correct
58 Correct 102 ms 2756 KB Output is correct
59 Correct 106 ms 2708 KB Output is correct
60 Correct 100 ms 2700 KB Output is correct
61 Correct 101 ms 2696 KB Output is correct
62 Correct 116 ms 2828 KB Output is correct
63 Correct 109 ms 2756 KB Output is correct
64 Correct 99 ms 2708 KB Output is correct
65 Correct 99 ms 2756 KB Output is correct
66 Correct 101 ms 2700 KB Output is correct
67 Correct 102 ms 2700 KB Output is correct
68 Correct 103 ms 2756 KB Output is correct
69 Correct 99 ms 2692 KB Output is correct
70 Correct 99 ms 2700 KB Output is correct
71 Correct 19 ms 764 KB Output is correct
72 Correct 25 ms 844 KB Output is correct
73 Correct 21 ms 760 KB Output is correct
74 Correct 22 ms 752 KB Output is correct
75 Correct 20 ms 756 KB Output is correct
76 Correct 41 ms 1400 KB Output is correct
77 Correct 41 ms 1384 KB Output is correct
78 Correct 42 ms 1376 KB Output is correct
79 Correct 41 ms 1388 KB Output is correct
80 Correct 42 ms 1396 KB Output is correct
81 Correct 88 ms 2428 KB Output is correct
82 Correct 89 ms 2456 KB Output is correct
83 Correct 89 ms 2436 KB Output is correct
84 Correct 96 ms 2436 KB Output is correct
85 Correct 88 ms 2448 KB Output is correct
86 Correct 96 ms 2932 KB Output is correct
87 Correct 102 ms 2940 KB Output is correct
88 Correct 105 ms 2996 KB Output is correct
89 Correct 96 ms 2936 KB Output is correct
90 Correct 95 ms 3012 KB Output is correct
91 Correct 157 ms 8132 KB Output is correct
92 Correct 168 ms 8260 KB Output is correct
93 Correct 159 ms 8132 KB Output is correct
94 Correct 157 ms 8064 KB Output is correct
95 Correct 184 ms 8132 KB Output is correct
96 Correct 155 ms 13676 KB Output is correct
97 Correct 161 ms 13580 KB Output is correct
98 Correct 164 ms 13588 KB Output is correct
99 Correct 155 ms 13572 KB Output is correct
100 Correct 156 ms 13636 KB Output is correct
101 Correct 157 ms 13656 KB Output is correct
102 Correct 153 ms 13572 KB Output is correct
103 Correct 153 ms 13572 KB Output is correct
104 Correct 167 ms 13704 KB Output is correct
105 Correct 155 ms 13576 KB Output is correct
106 Correct 149 ms 16392 KB Output is correct
107 Correct 158 ms 16276 KB Output is correct
108 Correct 161 ms 16392 KB Output is correct
109 Correct 148 ms 16324 KB Output is correct
110 Correct 160 ms 16276 KB Output is correct
111 Correct 186 ms 16388 KB Output is correct
112 Correct 149 ms 16452 KB Output is correct
113 Correct 160 ms 16336 KB Output is correct
114 Correct 160 ms 16388 KB Output is correct
115 Correct 150 ms 16388 KB Output is correct
116 Correct 357 ms 2380 KB Output is correct
117 Correct 427 ms 2888 KB Output is correct
118 Correct 1698 ms 3468 KB Output is correct
119 Correct 2316 ms 3624 KB Output is correct
120 Correct 2698 ms 3556 KB Output is correct
121 Correct 5135 ms 4156 KB Output is correct
122 Correct 5894 ms 4264 KB Output is correct
123 Execution timed out 7045 ms 4808 KB Time limit exceeded
124 Halted 0 ms 0 KB -