Submission #489127

# Submission time Handle Problem Language Result Execution time Memory
489127 2021-11-21T08:50:59 Z Wayne_Yan Diversity (CEOI21_diversity) C++17
64 / 100
341 ms 5608 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 / 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];


  if(q == 1){
    int times[maxN] = {};
    F(n) times[arr[i]]++;
    sort(times, times+maxN);
    int ans = (n * (n+1)) / 2;
    int ldist = 0;
    int rdist = 0;
    F(maxN - 1){
      if(times[i] == 0) continue;
      if(ldist > rdist) swap(ldist, rdist);
      ldist += times[i];
      ans += (n - ldist) * ldist;
      
    }
    printf("%lld\n", ans);
  }else{
    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();
      F((int) s.size() - 1){
        if(ldist > rdist) swap(ldist, rdist);
        ldist += *it;
        ans += ((cr-cl+1) - ldist) * ldist;
      }
      fin[id] = ans;
    }


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


  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 6 ms 2636 KB Output is correct
4 Correct 5 ms 2636 KB Output is correct
5 Correct 5 ms 2636 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
7 Correct 5 ms 2648 KB Output is correct
8 Correct 5 ms 2612 KB Output is correct
9 Correct 5 ms 2660 KB Output is correct
10 Correct 5 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 10 ms 2668 KB Output is correct
4 Correct 19 ms 3620 KB Output is correct
5 Correct 31 ms 4572 KB Output is correct
6 Correct 46 ms 5488 KB Output is correct
7 Correct 49 ms 5512 KB Output is correct
8 Correct 44 ms 5444 KB Output is correct
9 Correct 44 ms 5452 KB Output is correct
10 Correct 45 ms 5476 KB Output is correct
11 Correct 46 ms 5484 KB Output is correct
12 Correct 45 ms 5472 KB Output is correct
13 Correct 44 ms 5500 KB Output is correct
14 Correct 45 ms 5452 KB Output is correct
15 Correct 43 ms 5488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 10 ms 2668 KB Output is correct
4 Correct 19 ms 3620 KB Output is correct
5 Correct 31 ms 4572 KB Output is correct
6 Correct 46 ms 5488 KB Output is correct
7 Correct 49 ms 5512 KB Output is correct
8 Correct 44 ms 5444 KB Output is correct
9 Correct 44 ms 5452 KB Output is correct
10 Correct 45 ms 5476 KB Output is correct
11 Correct 46 ms 5484 KB Output is correct
12 Correct 45 ms 5472 KB Output is correct
13 Correct 44 ms 5500 KB Output is correct
14 Correct 45 ms 5452 KB Output is correct
15 Correct 43 ms 5488 KB Output is correct
16 Correct 4 ms 2636 KB Output is correct
17 Correct 6 ms 2636 KB Output is correct
18 Correct 6 ms 2744 KB Output is correct
19 Correct 20 ms 3692 KB Output is correct
20 Correct 34 ms 4684 KB Output is correct
21 Correct 50 ms 5484 KB Output is correct
22 Correct 48 ms 5476 KB Output is correct
23 Correct 49 ms 5480 KB Output is correct
24 Correct 49 ms 5496 KB Output is correct
25 Correct 47 ms 5452 KB Output is correct
26 Correct 47 ms 5500 KB Output is correct
27 Correct 55 ms 5608 KB Output is correct
28 Correct 47 ms 5512 KB Output is correct
29 Correct 47 ms 5500 KB Output is correct
30 Correct 49 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 10 ms 2668 KB Output is correct
4 Correct 19 ms 3620 KB Output is correct
5 Correct 31 ms 4572 KB Output is correct
6 Correct 46 ms 5488 KB Output is correct
7 Correct 49 ms 5512 KB Output is correct
8 Correct 44 ms 5444 KB Output is correct
9 Correct 44 ms 5452 KB Output is correct
10 Correct 45 ms 5476 KB Output is correct
11 Correct 46 ms 5484 KB Output is correct
12 Correct 45 ms 5472 KB Output is correct
13 Correct 44 ms 5500 KB Output is correct
14 Correct 45 ms 5452 KB Output is correct
15 Correct 43 ms 5488 KB Output is correct
16 Correct 4 ms 2636 KB Output is correct
17 Correct 6 ms 2636 KB Output is correct
18 Correct 6 ms 2744 KB Output is correct
19 Correct 20 ms 3692 KB Output is correct
20 Correct 34 ms 4684 KB Output is correct
21 Correct 50 ms 5484 KB Output is correct
22 Correct 48 ms 5476 KB Output is correct
23 Correct 49 ms 5480 KB Output is correct
24 Correct 49 ms 5496 KB Output is correct
25 Correct 47 ms 5452 KB Output is correct
26 Correct 47 ms 5500 KB Output is correct
27 Correct 55 ms 5608 KB Output is correct
28 Correct 47 ms 5512 KB Output is correct
29 Correct 47 ms 5500 KB Output is correct
30 Correct 49 ms 5452 KB Output is correct
31 Correct 5 ms 2636 KB Output is correct
32 Correct 5 ms 2636 KB Output is correct
33 Correct 5 ms 2636 KB Output is correct
34 Correct 6 ms 2656 KB Output is correct
35 Correct 8 ms 2660 KB Output is correct
36 Correct 7 ms 2764 KB Output is correct
37 Correct 14 ms 3228 KB Output is correct
38 Correct 14 ms 3148 KB Output is correct
39 Correct 24 ms 3800 KB Output is correct
40 Correct 46 ms 4692 KB Output is correct
41 Correct 60 ms 5476 KB Output is correct
42 Correct 61 ms 5452 KB Output is correct
43 Correct 61 ms 5452 KB Output is correct
44 Correct 60 ms 5504 KB Output is correct
45 Correct 61 ms 5484 KB Output is correct
46 Correct 60 ms 5496 KB Output is correct
47 Correct 63 ms 5608 KB Output is correct
48 Correct 61 ms 5496 KB Output is correct
49 Correct 59 ms 5452 KB Output is correct
50 Correct 60 ms 5500 KB Output is correct
51 Correct 61 ms 5500 KB Output is correct
52 Correct 60 ms 5500 KB Output is correct
53 Correct 60 ms 5488 KB Output is correct
54 Correct 64 ms 5504 KB Output is correct
55 Correct 75 ms 5452 KB Output is correct
56 Correct 61 ms 5452 KB Output is correct
57 Correct 60 ms 5504 KB Output is correct
58 Correct 60 ms 5488 KB Output is correct
59 Correct 61 ms 5500 KB Output is correct
60 Correct 63 ms 5572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 6 ms 2636 KB Output is correct
4 Correct 5 ms 2636 KB Output is correct
5 Correct 5 ms 2636 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
7 Correct 5 ms 2648 KB Output is correct
8 Correct 5 ms 2612 KB Output is correct
9 Correct 5 ms 2660 KB Output is correct
10 Correct 5 ms 2636 KB Output is correct
11 Correct 5 ms 2636 KB Output is correct
12 Correct 5 ms 2636 KB Output is correct
13 Correct 10 ms 2668 KB Output is correct
14 Correct 19 ms 3620 KB Output is correct
15 Correct 31 ms 4572 KB Output is correct
16 Correct 46 ms 5488 KB Output is correct
17 Correct 49 ms 5512 KB Output is correct
18 Correct 44 ms 5444 KB Output is correct
19 Correct 44 ms 5452 KB Output is correct
20 Correct 45 ms 5476 KB Output is correct
21 Correct 46 ms 5484 KB Output is correct
22 Correct 45 ms 5472 KB Output is correct
23 Correct 44 ms 5500 KB Output is correct
24 Correct 45 ms 5452 KB Output is correct
25 Correct 43 ms 5488 KB Output is correct
26 Correct 4 ms 2636 KB Output is correct
27 Correct 6 ms 2636 KB Output is correct
28 Correct 6 ms 2744 KB Output is correct
29 Correct 20 ms 3692 KB Output is correct
30 Correct 34 ms 4684 KB Output is correct
31 Correct 50 ms 5484 KB Output is correct
32 Correct 48 ms 5476 KB Output is correct
33 Correct 49 ms 5480 KB Output is correct
34 Correct 49 ms 5496 KB Output is correct
35 Correct 47 ms 5452 KB Output is correct
36 Correct 47 ms 5500 KB Output is correct
37 Correct 55 ms 5608 KB Output is correct
38 Correct 47 ms 5512 KB Output is correct
39 Correct 47 ms 5500 KB Output is correct
40 Correct 49 ms 5452 KB Output is correct
41 Correct 5 ms 2636 KB Output is correct
42 Correct 5 ms 2636 KB Output is correct
43 Correct 5 ms 2636 KB Output is correct
44 Correct 6 ms 2656 KB Output is correct
45 Correct 8 ms 2660 KB Output is correct
46 Correct 7 ms 2764 KB Output is correct
47 Correct 14 ms 3228 KB Output is correct
48 Correct 14 ms 3148 KB Output is correct
49 Correct 24 ms 3800 KB Output is correct
50 Correct 46 ms 4692 KB Output is correct
51 Correct 60 ms 5476 KB Output is correct
52 Correct 61 ms 5452 KB Output is correct
53 Correct 61 ms 5452 KB Output is correct
54 Correct 60 ms 5504 KB Output is correct
55 Correct 61 ms 5484 KB Output is correct
56 Correct 60 ms 5496 KB Output is correct
57 Correct 63 ms 5608 KB Output is correct
58 Correct 61 ms 5496 KB Output is correct
59 Correct 59 ms 5452 KB Output is correct
60 Correct 60 ms 5500 KB Output is correct
61 Correct 61 ms 5500 KB Output is correct
62 Correct 60 ms 5500 KB Output is correct
63 Correct 60 ms 5488 KB Output is correct
64 Correct 64 ms 5504 KB Output is correct
65 Correct 75 ms 5452 KB Output is correct
66 Correct 61 ms 5452 KB Output is correct
67 Correct 60 ms 5504 KB Output is correct
68 Correct 60 ms 5488 KB Output is correct
69 Correct 61 ms 5500 KB Output is correct
70 Correct 63 ms 5572 KB Output is correct
71 Correct 15 ms 3148 KB Output is correct
72 Correct 15 ms 3148 KB Output is correct
73 Correct 15 ms 3148 KB Output is correct
74 Correct 15 ms 3256 KB Output is correct
75 Correct 15 ms 3148 KB Output is correct
76 Correct 26 ms 3796 KB Output is correct
77 Correct 26 ms 3876 KB Output is correct
78 Correct 26 ms 3788 KB Output is correct
79 Correct 26 ms 3788 KB Output is correct
80 Correct 27 ms 3792 KB Output is correct
81 Correct 48 ms 4700 KB Output is correct
82 Correct 47 ms 4712 KB Output is correct
83 Correct 51 ms 4696 KB Output is correct
84 Correct 47 ms 4720 KB Output is correct
85 Correct 47 ms 4684 KB Output is correct
86 Correct 51 ms 4684 KB Output is correct
87 Correct 52 ms 4728 KB Output is correct
88 Correct 52 ms 4708 KB Output is correct
89 Correct 52 ms 4684 KB Output is correct
90 Correct 51 ms 4716 KB Output is correct
91 Correct 83 ms 5572 KB Output is correct
92 Correct 79 ms 5488 KB Output is correct
93 Correct 79 ms 5500 KB Output is correct
94 Correct 78 ms 5508 KB Output is correct
95 Correct 81 ms 5484 KB Output is correct
96 Correct 83 ms 5452 KB Output is correct
97 Correct 89 ms 5572 KB Output is correct
98 Correct 81 ms 5452 KB Output is correct
99 Correct 84 ms 5504 KB Output is correct
100 Correct 83 ms 5488 KB Output is correct
101 Correct 85 ms 5496 KB Output is correct
102 Correct 88 ms 5496 KB Output is correct
103 Correct 85 ms 5504 KB Output is correct
104 Correct 82 ms 5452 KB Output is correct
105 Correct 83 ms 5488 KB Output is correct
106 Correct 87 ms 5500 KB Output is correct
107 Correct 85 ms 5492 KB Output is correct
108 Correct 84 ms 5496 KB Output is correct
109 Correct 89 ms 5504 KB Output is correct
110 Correct 82 ms 5452 KB Output is correct
111 Correct 87 ms 5496 KB Output is correct
112 Correct 84 ms 5496 KB Output is correct
113 Correct 83 ms 5488 KB Output is correct
114 Correct 92 ms 5572 KB Output is correct
115 Correct 83 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2636 KB Output is correct
2 Correct 5 ms 2636 KB Output is correct
3 Correct 6 ms 2636 KB Output is correct
4 Correct 5 ms 2636 KB Output is correct
5 Correct 5 ms 2636 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
7 Correct 5 ms 2648 KB Output is correct
8 Correct 5 ms 2612 KB Output is correct
9 Correct 5 ms 2660 KB Output is correct
10 Correct 5 ms 2636 KB Output is correct
11 Correct 5 ms 2636 KB Output is correct
12 Correct 5 ms 2636 KB Output is correct
13 Correct 10 ms 2668 KB Output is correct
14 Correct 19 ms 3620 KB Output is correct
15 Correct 31 ms 4572 KB Output is correct
16 Correct 46 ms 5488 KB Output is correct
17 Correct 49 ms 5512 KB Output is correct
18 Correct 44 ms 5444 KB Output is correct
19 Correct 44 ms 5452 KB Output is correct
20 Correct 45 ms 5476 KB Output is correct
21 Correct 46 ms 5484 KB Output is correct
22 Correct 45 ms 5472 KB Output is correct
23 Correct 44 ms 5500 KB Output is correct
24 Correct 45 ms 5452 KB Output is correct
25 Correct 43 ms 5488 KB Output is correct
26 Correct 4 ms 2636 KB Output is correct
27 Correct 6 ms 2636 KB Output is correct
28 Correct 6 ms 2744 KB Output is correct
29 Correct 20 ms 3692 KB Output is correct
30 Correct 34 ms 4684 KB Output is correct
31 Correct 50 ms 5484 KB Output is correct
32 Correct 48 ms 5476 KB Output is correct
33 Correct 49 ms 5480 KB Output is correct
34 Correct 49 ms 5496 KB Output is correct
35 Correct 47 ms 5452 KB Output is correct
36 Correct 47 ms 5500 KB Output is correct
37 Correct 55 ms 5608 KB Output is correct
38 Correct 47 ms 5512 KB Output is correct
39 Correct 47 ms 5500 KB Output is correct
40 Correct 49 ms 5452 KB Output is correct
41 Correct 5 ms 2636 KB Output is correct
42 Correct 5 ms 2636 KB Output is correct
43 Correct 5 ms 2636 KB Output is correct
44 Correct 6 ms 2656 KB Output is correct
45 Correct 8 ms 2660 KB Output is correct
46 Correct 7 ms 2764 KB Output is correct
47 Correct 14 ms 3228 KB Output is correct
48 Correct 14 ms 3148 KB Output is correct
49 Correct 24 ms 3800 KB Output is correct
50 Correct 46 ms 4692 KB Output is correct
51 Correct 60 ms 5476 KB Output is correct
52 Correct 61 ms 5452 KB Output is correct
53 Correct 61 ms 5452 KB Output is correct
54 Correct 60 ms 5504 KB Output is correct
55 Correct 61 ms 5484 KB Output is correct
56 Correct 60 ms 5496 KB Output is correct
57 Correct 63 ms 5608 KB Output is correct
58 Correct 61 ms 5496 KB Output is correct
59 Correct 59 ms 5452 KB Output is correct
60 Correct 60 ms 5500 KB Output is correct
61 Correct 61 ms 5500 KB Output is correct
62 Correct 60 ms 5500 KB Output is correct
63 Correct 60 ms 5488 KB Output is correct
64 Correct 64 ms 5504 KB Output is correct
65 Correct 75 ms 5452 KB Output is correct
66 Correct 61 ms 5452 KB Output is correct
67 Correct 60 ms 5504 KB Output is correct
68 Correct 60 ms 5488 KB Output is correct
69 Correct 61 ms 5500 KB Output is correct
70 Correct 63 ms 5572 KB Output is correct
71 Correct 15 ms 3148 KB Output is correct
72 Correct 15 ms 3148 KB Output is correct
73 Correct 15 ms 3148 KB Output is correct
74 Correct 15 ms 3256 KB Output is correct
75 Correct 15 ms 3148 KB Output is correct
76 Correct 26 ms 3796 KB Output is correct
77 Correct 26 ms 3876 KB Output is correct
78 Correct 26 ms 3788 KB Output is correct
79 Correct 26 ms 3788 KB Output is correct
80 Correct 27 ms 3792 KB Output is correct
81 Correct 48 ms 4700 KB Output is correct
82 Correct 47 ms 4712 KB Output is correct
83 Correct 51 ms 4696 KB Output is correct
84 Correct 47 ms 4720 KB Output is correct
85 Correct 47 ms 4684 KB Output is correct
86 Correct 51 ms 4684 KB Output is correct
87 Correct 52 ms 4728 KB Output is correct
88 Correct 52 ms 4708 KB Output is correct
89 Correct 52 ms 4684 KB Output is correct
90 Correct 51 ms 4716 KB Output is correct
91 Correct 83 ms 5572 KB Output is correct
92 Correct 79 ms 5488 KB Output is correct
93 Correct 79 ms 5500 KB Output is correct
94 Correct 78 ms 5508 KB Output is correct
95 Correct 81 ms 5484 KB Output is correct
96 Correct 83 ms 5452 KB Output is correct
97 Correct 89 ms 5572 KB Output is correct
98 Correct 81 ms 5452 KB Output is correct
99 Correct 84 ms 5504 KB Output is correct
100 Correct 83 ms 5488 KB Output is correct
101 Correct 85 ms 5496 KB Output is correct
102 Correct 88 ms 5496 KB Output is correct
103 Correct 85 ms 5504 KB Output is correct
104 Correct 82 ms 5452 KB Output is correct
105 Correct 83 ms 5488 KB Output is correct
106 Correct 87 ms 5500 KB Output is correct
107 Correct 85 ms 5492 KB Output is correct
108 Correct 84 ms 5496 KB Output is correct
109 Correct 89 ms 5504 KB Output is correct
110 Correct 82 ms 5452 KB Output is correct
111 Correct 87 ms 5496 KB Output is correct
112 Correct 84 ms 5496 KB Output is correct
113 Correct 83 ms 5488 KB Output is correct
114 Correct 92 ms 5572 KB Output is correct
115 Correct 83 ms 5452 KB Output is correct
116 Incorrect 341 ms 5176 KB Output isn't correct
117 Halted 0 ms 0 KB -