Submission #790533

# Submission time Handle Problem Language Result Execution time Memory
790533 2023-07-22T19:56:11 Z CSQ31 Diversity (CEOI21_diversity) C++17
64 / 100
122 ms 6148 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<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
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]);
}
ll cal(int n){
	vector<int>v;
	int T = 0;
	for(int x:s){
		v.pb(x);
		T+=freq[x];
	}
	int t = sz(v);
	vector<int>in(t);
	//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 = T*1LL*n*1LL*n + n,cur = 0;
	//cout<<"res "<<res<<'\n';
	
	vector<int>lf,rg;
	int turn = 0;
	for(int i=0;i<t;i++){
		for(int j=0;j<freq[v[i]];j++){
			if(!turn)lf.pb(v[i]);
			else rg.pb(v[i]);
			turn^=1;
		}
	}
	reverse(all(rg));
	for(int x:rg)lf.pb(x);
	
	ll sum = 0;
	for(int x:lf){
		res-=sum*sum;
		sum+=x;
	}
	reverse(all(lf));
	sum = 0;
	for(int x:lf){
		res-=sum*sum;
		sum+=x;
	}
	reverse(all(lf));
	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--;
		}
		//cout<<"TES"<<'\n';
		ans[qq[i].id] = cal(r-l+1);		
	}
	for(int i=0;i<q;i++)cout<<ans[i]<<'\n';
	
	
}

Compilation message

diversity.cpp: In function 'll cal(int)':
diversity.cpp:54:29: warning: unused variable 'cur' [-Wunused-variable]
   54 |  ll res = T*1LL*n*1LL*n + n,cur = 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 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 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 1 ms 340 KB Output is correct
4 Correct 11 ms 764 KB Output is correct
5 Correct 24 ms 1436 KB Output is correct
6 Correct 34 ms 1688 KB Output is correct
7 Correct 30 ms 2636 KB Output is correct
8 Correct 32 ms 2224 KB Output is correct
9 Correct 32 ms 2248 KB Output is correct
10 Correct 32 ms 2516 KB Output is correct
11 Correct 32 ms 2384 KB Output is correct
12 Correct 35 ms 2332 KB Output is correct
13 Correct 33 ms 2324 KB Output is correct
14 Correct 32 ms 2380 KB Output is correct
15 Correct 32 ms 2272 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 1 ms 340 KB Output is correct
4 Correct 11 ms 764 KB Output is correct
5 Correct 24 ms 1436 KB Output is correct
6 Correct 34 ms 1688 KB Output is correct
7 Correct 30 ms 2636 KB Output is correct
8 Correct 32 ms 2224 KB Output is correct
9 Correct 32 ms 2248 KB Output is correct
10 Correct 32 ms 2516 KB Output is correct
11 Correct 32 ms 2384 KB Output is correct
12 Correct 35 ms 2332 KB Output is correct
13 Correct 33 ms 2324 KB Output is correct
14 Correct 32 ms 2380 KB Output is correct
15 Correct 32 ms 2272 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 15 ms 976 KB Output is correct
20 Correct 25 ms 1688 KB Output is correct
21 Correct 36 ms 2500 KB Output is correct
22 Correct 37 ms 2316 KB Output is correct
23 Correct 37 ms 2384 KB Output is correct
24 Correct 38 ms 2420 KB Output is correct
25 Correct 37 ms 2344 KB Output is correct
26 Correct 38 ms 2300 KB Output is correct
27 Correct 38 ms 2416 KB Output is correct
28 Correct 36 ms 2484 KB Output is correct
29 Correct 37 ms 2296 KB Output is correct
30 Correct 37 ms 2372 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 1 ms 340 KB Output is correct
4 Correct 11 ms 764 KB Output is correct
5 Correct 24 ms 1436 KB Output is correct
6 Correct 34 ms 1688 KB Output is correct
7 Correct 30 ms 2636 KB Output is correct
8 Correct 32 ms 2224 KB Output is correct
9 Correct 32 ms 2248 KB Output is correct
10 Correct 32 ms 2516 KB Output is correct
11 Correct 32 ms 2384 KB Output is correct
12 Correct 35 ms 2332 KB Output is correct
13 Correct 33 ms 2324 KB Output is correct
14 Correct 32 ms 2380 KB Output is correct
15 Correct 32 ms 2272 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 15 ms 976 KB Output is correct
20 Correct 25 ms 1688 KB Output is correct
21 Correct 36 ms 2500 KB Output is correct
22 Correct 37 ms 2316 KB Output is correct
23 Correct 37 ms 2384 KB Output is correct
24 Correct 38 ms 2420 KB Output is correct
25 Correct 37 ms 2344 KB Output is correct
26 Correct 38 ms 2300 KB Output is correct
27 Correct 38 ms 2416 KB Output is correct
28 Correct 36 ms 2484 KB Output is correct
29 Correct 37 ms 2296 KB Output is correct
30 Correct 37 ms 2372 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 5 ms 724 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 8 ms 1064 KB Output is correct
40 Correct 18 ms 1892 KB Output is correct
41 Correct 30 ms 2564 KB Output is correct
42 Correct 31 ms 2664 KB Output is correct
43 Correct 31 ms 2660 KB Output is correct
44 Correct 30 ms 2624 KB Output is correct
45 Correct 30 ms 2644 KB Output is correct
46 Correct 35 ms 2636 KB Output is correct
47 Correct 30 ms 2596 KB Output is correct
48 Correct 30 ms 2680 KB Output is correct
49 Correct 30 ms 2684 KB Output is correct
50 Correct 31 ms 2680 KB Output is correct
51 Correct 30 ms 2664 KB Output is correct
52 Correct 30 ms 2672 KB Output is correct
53 Correct 30 ms 2608 KB Output is correct
54 Correct 31 ms 2668 KB Output is correct
55 Correct 30 ms 2632 KB Output is correct
56 Correct 30 ms 2652 KB Output is correct
57 Correct 29 ms 2668 KB Output is correct
58 Correct 30 ms 2672 KB Output is correct
59 Correct 30 ms 2668 KB Output is correct
60 Correct 30 ms 2608 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 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 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 1 ms 340 KB Output is correct
14 Correct 11 ms 764 KB Output is correct
15 Correct 24 ms 1436 KB Output is correct
16 Correct 34 ms 1688 KB Output is correct
17 Correct 30 ms 2636 KB Output is correct
18 Correct 32 ms 2224 KB Output is correct
19 Correct 32 ms 2248 KB Output is correct
20 Correct 32 ms 2516 KB Output is correct
21 Correct 32 ms 2384 KB Output is correct
22 Correct 35 ms 2332 KB Output is correct
23 Correct 33 ms 2324 KB Output is correct
24 Correct 32 ms 2380 KB Output is correct
25 Correct 32 ms 2272 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 15 ms 976 KB Output is correct
30 Correct 25 ms 1688 KB Output is correct
31 Correct 36 ms 2500 KB Output is correct
32 Correct 37 ms 2316 KB Output is correct
33 Correct 37 ms 2384 KB Output is correct
34 Correct 38 ms 2420 KB Output is correct
35 Correct 37 ms 2344 KB Output is correct
36 Correct 38 ms 2300 KB Output is correct
37 Correct 38 ms 2416 KB Output is correct
38 Correct 36 ms 2484 KB Output is correct
39 Correct 37 ms 2296 KB Output is correct
40 Correct 37 ms 2372 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 324 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 8 ms 1064 KB Output is correct
50 Correct 18 ms 1892 KB Output is correct
51 Correct 30 ms 2564 KB Output is correct
52 Correct 31 ms 2664 KB Output is correct
53 Correct 31 ms 2660 KB Output is correct
54 Correct 30 ms 2624 KB Output is correct
55 Correct 30 ms 2644 KB Output is correct
56 Correct 35 ms 2636 KB Output is correct
57 Correct 30 ms 2596 KB Output is correct
58 Correct 30 ms 2680 KB Output is correct
59 Correct 30 ms 2684 KB Output is correct
60 Correct 31 ms 2680 KB Output is correct
61 Correct 30 ms 2664 KB Output is correct
62 Correct 30 ms 2672 KB Output is correct
63 Correct 30 ms 2608 KB Output is correct
64 Correct 31 ms 2668 KB Output is correct
65 Correct 30 ms 2632 KB Output is correct
66 Correct 30 ms 2652 KB Output is correct
67 Correct 29 ms 2668 KB Output is correct
68 Correct 30 ms 2672 KB Output is correct
69 Correct 30 ms 2668 KB Output is correct
70 Correct 30 ms 2608 KB Output is correct
71 Correct 4 ms 720 KB Output is correct
72 Correct 4 ms 724 KB Output is correct
73 Correct 4 ms 724 KB Output is correct
74 Correct 6 ms 720 KB Output is correct
75 Correct 4 ms 724 KB Output is correct
76 Correct 6 ms 1236 KB Output is correct
77 Correct 6 ms 1232 KB Output is correct
78 Correct 6 ms 1236 KB Output is correct
79 Correct 7 ms 1236 KB Output is correct
80 Correct 6 ms 1236 KB Output is correct
81 Correct 12 ms 2132 KB Output is correct
82 Correct 12 ms 2256 KB Output is correct
83 Correct 12 ms 2132 KB Output is correct
84 Correct 12 ms 2132 KB Output is correct
85 Correct 12 ms 2224 KB Output is correct
86 Correct 16 ms 2508 KB Output is correct
87 Correct 16 ms 2516 KB Output is correct
88 Correct 14 ms 2504 KB Output is correct
89 Correct 14 ms 2464 KB Output is correct
90 Correct 14 ms 2508 KB Output is correct
91 Correct 20 ms 4520 KB Output is correct
92 Correct 20 ms 4468 KB Output is correct
93 Correct 20 ms 4536 KB Output is correct
94 Correct 20 ms 4560 KB Output is correct
95 Correct 20 ms 4536 KB Output is correct
96 Correct 24 ms 5764 KB Output is correct
97 Correct 24 ms 5772 KB Output is correct
98 Correct 23 ms 5736 KB Output is correct
99 Correct 23 ms 5752 KB Output is correct
100 Correct 28 ms 5696 KB Output is correct
101 Correct 24 ms 5820 KB Output is correct
102 Correct 23 ms 5692 KB Output is correct
103 Correct 24 ms 5736 KB Output is correct
104 Correct 24 ms 5768 KB Output is correct
105 Correct 24 ms 5760 KB Output is correct
106 Correct 23 ms 6016 KB Output is correct
107 Correct 23 ms 6148 KB Output is correct
108 Correct 24 ms 6080 KB Output is correct
109 Correct 24 ms 6076 KB Output is correct
110 Correct 23 ms 6020 KB Output is correct
111 Correct 24 ms 6080 KB Output is correct
112 Correct 24 ms 6080 KB Output is correct
113 Correct 26 ms 6092 KB Output is correct
114 Correct 24 ms 6088 KB Output is correct
115 Correct 23 ms 6068 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 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 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 1 ms 340 KB Output is correct
14 Correct 11 ms 764 KB Output is correct
15 Correct 24 ms 1436 KB Output is correct
16 Correct 34 ms 1688 KB Output is correct
17 Correct 30 ms 2636 KB Output is correct
18 Correct 32 ms 2224 KB Output is correct
19 Correct 32 ms 2248 KB Output is correct
20 Correct 32 ms 2516 KB Output is correct
21 Correct 32 ms 2384 KB Output is correct
22 Correct 35 ms 2332 KB Output is correct
23 Correct 33 ms 2324 KB Output is correct
24 Correct 32 ms 2380 KB Output is correct
25 Correct 32 ms 2272 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 15 ms 976 KB Output is correct
30 Correct 25 ms 1688 KB Output is correct
31 Correct 36 ms 2500 KB Output is correct
32 Correct 37 ms 2316 KB Output is correct
33 Correct 37 ms 2384 KB Output is correct
34 Correct 38 ms 2420 KB Output is correct
35 Correct 37 ms 2344 KB Output is correct
36 Correct 38 ms 2300 KB Output is correct
37 Correct 38 ms 2416 KB Output is correct
38 Correct 36 ms 2484 KB Output is correct
39 Correct 37 ms 2296 KB Output is correct
40 Correct 37 ms 2372 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 324 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 8 ms 1064 KB Output is correct
50 Correct 18 ms 1892 KB Output is correct
51 Correct 30 ms 2564 KB Output is correct
52 Correct 31 ms 2664 KB Output is correct
53 Correct 31 ms 2660 KB Output is correct
54 Correct 30 ms 2624 KB Output is correct
55 Correct 30 ms 2644 KB Output is correct
56 Correct 35 ms 2636 KB Output is correct
57 Correct 30 ms 2596 KB Output is correct
58 Correct 30 ms 2680 KB Output is correct
59 Correct 30 ms 2684 KB Output is correct
60 Correct 31 ms 2680 KB Output is correct
61 Correct 30 ms 2664 KB Output is correct
62 Correct 30 ms 2672 KB Output is correct
63 Correct 30 ms 2608 KB Output is correct
64 Correct 31 ms 2668 KB Output is correct
65 Correct 30 ms 2632 KB Output is correct
66 Correct 30 ms 2652 KB Output is correct
67 Correct 29 ms 2668 KB Output is correct
68 Correct 30 ms 2672 KB Output is correct
69 Correct 30 ms 2668 KB Output is correct
70 Correct 30 ms 2608 KB Output is correct
71 Correct 4 ms 720 KB Output is correct
72 Correct 4 ms 724 KB Output is correct
73 Correct 4 ms 724 KB Output is correct
74 Correct 6 ms 720 KB Output is correct
75 Correct 4 ms 724 KB Output is correct
76 Correct 6 ms 1236 KB Output is correct
77 Correct 6 ms 1232 KB Output is correct
78 Correct 6 ms 1236 KB Output is correct
79 Correct 7 ms 1236 KB Output is correct
80 Correct 6 ms 1236 KB Output is correct
81 Correct 12 ms 2132 KB Output is correct
82 Correct 12 ms 2256 KB Output is correct
83 Correct 12 ms 2132 KB Output is correct
84 Correct 12 ms 2132 KB Output is correct
85 Correct 12 ms 2224 KB Output is correct
86 Correct 16 ms 2508 KB Output is correct
87 Correct 16 ms 2516 KB Output is correct
88 Correct 14 ms 2504 KB Output is correct
89 Correct 14 ms 2464 KB Output is correct
90 Correct 14 ms 2508 KB Output is correct
91 Correct 20 ms 4520 KB Output is correct
92 Correct 20 ms 4468 KB Output is correct
93 Correct 20 ms 4536 KB Output is correct
94 Correct 20 ms 4560 KB Output is correct
95 Correct 20 ms 4536 KB Output is correct
96 Correct 24 ms 5764 KB Output is correct
97 Correct 24 ms 5772 KB Output is correct
98 Correct 23 ms 5736 KB Output is correct
99 Correct 23 ms 5752 KB Output is correct
100 Correct 28 ms 5696 KB Output is correct
101 Correct 24 ms 5820 KB Output is correct
102 Correct 23 ms 5692 KB Output is correct
103 Correct 24 ms 5736 KB Output is correct
104 Correct 24 ms 5768 KB Output is correct
105 Correct 24 ms 5760 KB Output is correct
106 Correct 23 ms 6016 KB Output is correct
107 Correct 23 ms 6148 KB Output is correct
108 Correct 24 ms 6080 KB Output is correct
109 Correct 24 ms 6076 KB Output is correct
110 Correct 23 ms 6020 KB Output is correct
111 Correct 24 ms 6080 KB Output is correct
112 Correct 24 ms 6080 KB Output is correct
113 Correct 26 ms 6092 KB Output is correct
114 Correct 24 ms 6088 KB Output is correct
115 Correct 23 ms 6068 KB Output is correct
116 Runtime error 122 ms 3024 KB Execution killed with signal 11
117 Halted 0 ms 0 KB -