Submission #790550

# Submission time Handle Problem Language Result Execution time Memory
790550 2023-07-22T20:34:58 Z CSQ31 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 6744 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
//typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
const int MAXN = 3e5+5,blk = 450;
int a[MAXN];
struct que{
	int l,r,id;
	que(){}
	que(int _l,int _r,int _id):l(_l),r(_r),id(_id){}
	bool operator<(const que &q){
		if((l+blk-1)/blk &1)return make_pair((l+blk-1)/blk,r) < make_pair((q.l+blk-1)/blk,q.r);
		return make_pair((l+blk-1)/blk,-r) < make_pair((q.l+blk-1)/blk,-q.r);
	}
};
int cnt[MAXN],freq[MAXN];
set<int>s;
void add(int x,int c){
	if(freq[cnt[x]] == 1)s.erase(cnt[x]);
	freq[cnt[x]]--;
	cnt[x]+=c;
	freq[cnt[x]]++;
	if(freq[cnt[x]] == 1)s.insert(cnt[x]);
}
typedef __int128 ll;
ll cal(ll n){
	vector<ll>v;
	ll T = 0;
	for(int x:s){
		v.pb(x);
		T+=freq[x];
	}
	int t = sz(v);
	vector<int>in(t,0);
	//ans = 1/2(T*n*n + n - sum of squares)
	//for(int x:v)cout<<x<<" ";cout<<'\n';
	//for(int x:v)cout<<freq[x]<<" ";cout<<'\n';
	ll res = 0,cur = 0;
	for(int i=1;i<t;i++){
		in[i] = in[i-1]+(freq[v[i]]&1);
		in[i]%=2;
	}
	//S = (cur+v)^2 + (cur+2v)^2 + ... + (cur+xv)^2
	//  = x*cur*cur - 2cur*v(1+2+...+x) + v^2(1^2+2^2+3^2...x2);
	
	//(n-cur-v)^2 + (n-cur-2v)^2 + ... + (n-cur-xv)^2;
	//= x*n*n - 2*n( x*cur + v+2v+3v..+xv) + S
	vector<ll>tmp;
	for(int i=0;i<t;i++){
		ll x = (freq[v[i]]+in[i])/2;
		/*
		ll y = x*(x+1)/2;
		ll z = (x*(x+1)*(2*x+1))/6;
		
		ll S = 0;
		S+=x*cur*cur;
		S+=2*cur*v[i]*y;
		S+=v[i]*v[i]*z;
		res-=x*n*n;
		res+=2*n*( x*cur + y*v[i]);
		res-=2*S;
		cur+=x*v[i];*/
		for(int j=0;j<x;j++)tmp.pb(v[i]);
	}
	for(int i=t-1;i>=0;i--){
		ll x = (freq[v[i]]+in[i])/2;
		x = freq[v[i]]-x;
		/*
		ll y = x*(x+1)/2;
		ll z = (x*(x+1)*(2*x+1))/6;
		
		ll S = 0;
		S+=x*cur*cur;
		S+=2*cur*v[i]*y;
		S+=v[i]*v[i]*z;
		assert(S>=0);
		res-=x*n*n;
		res+=2*n*( x*cur + y*v[i]);
		res-=2*S;
		cur+=x*v[i];
		* */
		for(int j=0;j<x;j++)tmp.pb(v[i]);
	}
	for(ll x:tmp){
		cur+=x;
		res-=cur*cur;
		res-=(n-cur)*(n-cur);
	}
	res+=T*n*n+n;
	res+=n*n;
	return res/2;
}
int main()
{
	owo
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	vector<que>qq(q);
	for(int i=0;i<q;i++){
		int l,r;
		cin>>l>>r;
		qq[i] = que(l,r,i);
	}
	sort(all(qq));
	vector<ll>ans(q);
	int L = 1,R = 0;
	for(int i=0;i<q;i++){
		int l = qq[i].l;
		int r = qq[i].r;
		/*
		while(L>l){
			L--;
			add(a[L],1);
		}
		while(L<l){
			add(a[L],-1);
			L++;
		}
		while(R<r){
			R++;
			add(a[R],1);
		}
		while(R>r){
			add(a[R],-1);
			R--;
		}*/
		for(int j=l;j<=r;j++)add(a[j],1);
		ans[qq[i].id] = cal(r-l+1);	
		for(int j=l;j<=r;j++)add(a[j],-1);	
	}
	for(int i=0;i<q;i++){
		long long int x = ans[i];
		cout<<x<<'\n';
	}
	
	
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:120:6: warning: unused variable 'L' [-Wunused-variable]
  120 |  int L = 1,R = 0;
      |      ^
diversity.cpp:120:12: warning: unused variable 'R' [-Wunused-variable]
  120 |  int L = 1,R = 0;
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 724 KB Output is correct
5 Correct 34 ms 1464 KB Output is correct
6 Correct 51 ms 1704 KB Output is correct
7 Correct 45 ms 2060 KB Output is correct
8 Correct 51 ms 1648 KB Output is correct
9 Correct 51 ms 1692 KB Output is correct
10 Correct 49 ms 1892 KB Output is correct
11 Correct 54 ms 1760 KB Output is correct
12 Correct 51 ms 1696 KB Output is correct
13 Correct 51 ms 1620 KB Output is correct
14 Correct 56 ms 1792 KB Output is correct
15 Correct 61 ms 1676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 724 KB Output is correct
5 Correct 34 ms 1464 KB Output is correct
6 Correct 51 ms 1704 KB Output is correct
7 Correct 45 ms 2060 KB Output is correct
8 Correct 51 ms 1648 KB Output is correct
9 Correct 51 ms 1692 KB Output is correct
10 Correct 49 ms 1892 KB Output is correct
11 Correct 54 ms 1760 KB Output is correct
12 Correct 51 ms 1696 KB Output is correct
13 Correct 51 ms 1620 KB Output is correct
14 Correct 56 ms 1792 KB Output is correct
15 Correct 61 ms 1676 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 19 ms 800 KB Output is correct
20 Correct 39 ms 1144 KB Output is correct
21 Correct 58 ms 1720 KB Output is correct
22 Correct 61 ms 1568 KB Output is correct
23 Correct 59 ms 1688 KB Output is correct
24 Correct 63 ms 1756 KB Output is correct
25 Correct 62 ms 1612 KB Output is correct
26 Correct 60 ms 1588 KB Output is correct
27 Correct 59 ms 1668 KB Output is correct
28 Correct 66 ms 1688 KB Output is correct
29 Correct 59 ms 1536 KB Output is correct
30 Correct 60 ms 1668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 16 ms 724 KB Output is correct
5 Correct 34 ms 1464 KB Output is correct
6 Correct 51 ms 1704 KB Output is correct
7 Correct 45 ms 2060 KB Output is correct
8 Correct 51 ms 1648 KB Output is correct
9 Correct 51 ms 1692 KB Output is correct
10 Correct 49 ms 1892 KB Output is correct
11 Correct 54 ms 1760 KB Output is correct
12 Correct 51 ms 1696 KB Output is correct
13 Correct 51 ms 1620 KB Output is correct
14 Correct 56 ms 1792 KB Output is correct
15 Correct 61 ms 1676 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 19 ms 800 KB Output is correct
20 Correct 39 ms 1144 KB Output is correct
21 Correct 58 ms 1720 KB Output is correct
22 Correct 61 ms 1568 KB Output is correct
23 Correct 59 ms 1688 KB Output is correct
24 Correct 63 ms 1756 KB Output is correct
25 Correct 62 ms 1612 KB Output is correct
26 Correct 60 ms 1588 KB Output is correct
27 Correct 59 ms 1668 KB Output is correct
28 Correct 66 ms 1688 KB Output is correct
29 Correct 59 ms 1536 KB Output is correct
30 Correct 60 ms 1668 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 7 ms 468 KB Output is correct
38 Correct 4 ms 468 KB Output is correct
39 Correct 10 ms 692 KB Output is correct
40 Correct 26 ms 1164 KB Output is correct
41 Correct 45 ms 1584 KB Output is correct
42 Correct 44 ms 1528 KB Output is correct
43 Correct 45 ms 1592 KB Output is correct
44 Correct 47 ms 1492 KB Output is correct
45 Correct 48 ms 1584 KB Output is correct
46 Correct 44 ms 1576 KB Output is correct
47 Correct 44 ms 1576 KB Output is correct
48 Correct 45 ms 1492 KB Output is correct
49 Correct 44 ms 1492 KB Output is correct
50 Correct 45 ms 1580 KB Output is correct
51 Correct 45 ms 1532 KB Output is correct
52 Correct 51 ms 1520 KB Output is correct
53 Correct 45 ms 1492 KB Output is correct
54 Correct 47 ms 1492 KB Output is correct
55 Correct 44 ms 1520 KB Output is correct
56 Correct 45 ms 1584 KB Output is correct
57 Correct 43 ms 1544 KB Output is correct
58 Correct 44 ms 1580 KB Output is correct
59 Correct 44 ms 1492 KB Output is correct
60 Correct 44 ms 1576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 16 ms 724 KB Output is correct
15 Correct 34 ms 1464 KB Output is correct
16 Correct 51 ms 1704 KB Output is correct
17 Correct 45 ms 2060 KB Output is correct
18 Correct 51 ms 1648 KB Output is correct
19 Correct 51 ms 1692 KB Output is correct
20 Correct 49 ms 1892 KB Output is correct
21 Correct 54 ms 1760 KB Output is correct
22 Correct 51 ms 1696 KB Output is correct
23 Correct 51 ms 1620 KB Output is correct
24 Correct 56 ms 1792 KB Output is correct
25 Correct 61 ms 1676 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 19 ms 800 KB Output is correct
30 Correct 39 ms 1144 KB Output is correct
31 Correct 58 ms 1720 KB Output is correct
32 Correct 61 ms 1568 KB Output is correct
33 Correct 59 ms 1688 KB Output is correct
34 Correct 63 ms 1756 KB Output is correct
35 Correct 62 ms 1612 KB Output is correct
36 Correct 60 ms 1588 KB Output is correct
37 Correct 59 ms 1668 KB Output is correct
38 Correct 66 ms 1688 KB Output is correct
39 Correct 59 ms 1536 KB Output is correct
40 Correct 60 ms 1668 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 4 ms 468 KB Output is correct
49 Correct 10 ms 692 KB Output is correct
50 Correct 26 ms 1164 KB Output is correct
51 Correct 45 ms 1584 KB Output is correct
52 Correct 44 ms 1528 KB Output is correct
53 Correct 45 ms 1592 KB Output is correct
54 Correct 47 ms 1492 KB Output is correct
55 Correct 48 ms 1584 KB Output is correct
56 Correct 44 ms 1576 KB Output is correct
57 Correct 44 ms 1576 KB Output is correct
58 Correct 45 ms 1492 KB Output is correct
59 Correct 44 ms 1492 KB Output is correct
60 Correct 45 ms 1580 KB Output is correct
61 Correct 45 ms 1532 KB Output is correct
62 Correct 51 ms 1520 KB Output is correct
63 Correct 45 ms 1492 KB Output is correct
64 Correct 47 ms 1492 KB Output is correct
65 Correct 44 ms 1520 KB Output is correct
66 Correct 45 ms 1584 KB Output is correct
67 Correct 43 ms 1544 KB Output is correct
68 Correct 44 ms 1580 KB Output is correct
69 Correct 44 ms 1492 KB Output is correct
70 Correct 44 ms 1576 KB Output is correct
71 Correct 4 ms 616 KB Output is correct
72 Correct 4 ms 596 KB Output is correct
73 Correct 4 ms 596 KB Output is correct
74 Correct 4 ms 596 KB Output is correct
75 Correct 5 ms 612 KB Output is correct
76 Correct 7 ms 852 KB Output is correct
77 Correct 7 ms 852 KB Output is correct
78 Correct 7 ms 852 KB Output is correct
79 Correct 7 ms 852 KB Output is correct
80 Correct 7 ms 884 KB Output is correct
81 Correct 13 ms 1548 KB Output is correct
82 Correct 13 ms 1492 KB Output is correct
83 Correct 13 ms 1492 KB Output is correct
84 Correct 13 ms 1492 KB Output is correct
85 Correct 13 ms 1560 KB Output is correct
86 Correct 17 ms 1848 KB Output is correct
87 Correct 14 ms 1748 KB Output is correct
88 Correct 18 ms 1748 KB Output is correct
89 Correct 14 ms 1796 KB Output is correct
90 Correct 15 ms 1848 KB Output is correct
91 Correct 21 ms 4044 KB Output is correct
92 Correct 21 ms 4044 KB Output is correct
93 Correct 21 ms 4044 KB Output is correct
94 Correct 21 ms 4056 KB Output is correct
95 Correct 21 ms 4044 KB Output is correct
96 Correct 26 ms 6436 KB Output is correct
97 Correct 26 ms 6464 KB Output is correct
98 Correct 29 ms 6496 KB Output is correct
99 Correct 26 ms 6524 KB Output is correct
100 Correct 28 ms 6500 KB Output is correct
101 Correct 26 ms 6476 KB Output is correct
102 Correct 26 ms 6456 KB Output is correct
103 Correct 25 ms 6464 KB Output is correct
104 Correct 26 ms 6476 KB Output is correct
105 Correct 28 ms 6480 KB Output is correct
106 Correct 36 ms 6744 KB Output is correct
107 Correct 34 ms 6600 KB Output is correct
108 Correct 26 ms 6732 KB Output is correct
109 Correct 26 ms 6628 KB Output is correct
110 Correct 27 ms 6660 KB Output is correct
111 Correct 26 ms 6620 KB Output is correct
112 Correct 26 ms 6732 KB Output is correct
113 Correct 26 ms 6696 KB Output is correct
114 Correct 26 ms 6736 KB Output is correct
115 Correct 25 ms 6640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 16 ms 724 KB Output is correct
15 Correct 34 ms 1464 KB Output is correct
16 Correct 51 ms 1704 KB Output is correct
17 Correct 45 ms 2060 KB Output is correct
18 Correct 51 ms 1648 KB Output is correct
19 Correct 51 ms 1692 KB Output is correct
20 Correct 49 ms 1892 KB Output is correct
21 Correct 54 ms 1760 KB Output is correct
22 Correct 51 ms 1696 KB Output is correct
23 Correct 51 ms 1620 KB Output is correct
24 Correct 56 ms 1792 KB Output is correct
25 Correct 61 ms 1676 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 19 ms 800 KB Output is correct
30 Correct 39 ms 1144 KB Output is correct
31 Correct 58 ms 1720 KB Output is correct
32 Correct 61 ms 1568 KB Output is correct
33 Correct 59 ms 1688 KB Output is correct
34 Correct 63 ms 1756 KB Output is correct
35 Correct 62 ms 1612 KB Output is correct
36 Correct 60 ms 1588 KB Output is correct
37 Correct 59 ms 1668 KB Output is correct
38 Correct 66 ms 1688 KB Output is correct
39 Correct 59 ms 1536 KB Output is correct
40 Correct 60 ms 1668 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 7 ms 468 KB Output is correct
48 Correct 4 ms 468 KB Output is correct
49 Correct 10 ms 692 KB Output is correct
50 Correct 26 ms 1164 KB Output is correct
51 Correct 45 ms 1584 KB Output is correct
52 Correct 44 ms 1528 KB Output is correct
53 Correct 45 ms 1592 KB Output is correct
54 Correct 47 ms 1492 KB Output is correct
55 Correct 48 ms 1584 KB Output is correct
56 Correct 44 ms 1576 KB Output is correct
57 Correct 44 ms 1576 KB Output is correct
58 Correct 45 ms 1492 KB Output is correct
59 Correct 44 ms 1492 KB Output is correct
60 Correct 45 ms 1580 KB Output is correct
61 Correct 45 ms 1532 KB Output is correct
62 Correct 51 ms 1520 KB Output is correct
63 Correct 45 ms 1492 KB Output is correct
64 Correct 47 ms 1492 KB Output is correct
65 Correct 44 ms 1520 KB Output is correct
66 Correct 45 ms 1584 KB Output is correct
67 Correct 43 ms 1544 KB Output is correct
68 Correct 44 ms 1580 KB Output is correct
69 Correct 44 ms 1492 KB Output is correct
70 Correct 44 ms 1576 KB Output is correct
71 Correct 4 ms 616 KB Output is correct
72 Correct 4 ms 596 KB Output is correct
73 Correct 4 ms 596 KB Output is correct
74 Correct 4 ms 596 KB Output is correct
75 Correct 5 ms 612 KB Output is correct
76 Correct 7 ms 852 KB Output is correct
77 Correct 7 ms 852 KB Output is correct
78 Correct 7 ms 852 KB Output is correct
79 Correct 7 ms 852 KB Output is correct
80 Correct 7 ms 884 KB Output is correct
81 Correct 13 ms 1548 KB Output is correct
82 Correct 13 ms 1492 KB Output is correct
83 Correct 13 ms 1492 KB Output is correct
84 Correct 13 ms 1492 KB Output is correct
85 Correct 13 ms 1560 KB Output is correct
86 Correct 17 ms 1848 KB Output is correct
87 Correct 14 ms 1748 KB Output is correct
88 Correct 18 ms 1748 KB Output is correct
89 Correct 14 ms 1796 KB Output is correct
90 Correct 15 ms 1848 KB Output is correct
91 Correct 21 ms 4044 KB Output is correct
92 Correct 21 ms 4044 KB Output is correct
93 Correct 21 ms 4044 KB Output is correct
94 Correct 21 ms 4056 KB Output is correct
95 Correct 21 ms 4044 KB Output is correct
96 Correct 26 ms 6436 KB Output is correct
97 Correct 26 ms 6464 KB Output is correct
98 Correct 29 ms 6496 KB Output is correct
99 Correct 26 ms 6524 KB Output is correct
100 Correct 28 ms 6500 KB Output is correct
101 Correct 26 ms 6476 KB Output is correct
102 Correct 26 ms 6456 KB Output is correct
103 Correct 25 ms 6464 KB Output is correct
104 Correct 26 ms 6476 KB Output is correct
105 Correct 28 ms 6480 KB Output is correct
106 Correct 36 ms 6744 KB Output is correct
107 Correct 34 ms 6600 KB Output is correct
108 Correct 26 ms 6732 KB Output is correct
109 Correct 26 ms 6628 KB Output is correct
110 Correct 27 ms 6660 KB Output is correct
111 Correct 26 ms 6620 KB Output is correct
112 Correct 26 ms 6732 KB Output is correct
113 Correct 26 ms 6696 KB Output is correct
114 Correct 26 ms 6736 KB Output is correct
115 Correct 25 ms 6640 KB Output is correct
116 Execution timed out 7054 ms 1772 KB Time limit exceeded
117 Halted 0 ms 0 KB -