Submission #531366

# Submission time Handle Problem Language Result Execution time Memory
531366 2022-02-28T14:27:12 Z azberjibiou Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 3312 KB
#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<double, double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pmax pair<__int128, __int128>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=300003;
const int mxM=1010;
const int mxK=105;
const int MOD=1000000007;
const ll INF=1e18;
const ll P1=1000000007;
const ll P2=1000000009;
typedef struct query{
    int s, e, idx;
    ll ans;
}query;
int N, M, Q, L;
int A[mxN];
query T[mxN];
int ncnt[mxN];  ///number
int tcnt[mxN];  ///type
int typ_cnt;
int ns=1, ne;
map <int, int> s;
ll ans;
ll lval, rval;
int nsum;
bool cmp1(query a, query b)
{
    if(a.s/M!=b.s/M)    return a.s<b.s;
    return a.e<b.e;
}
void set_remove(int now)
{
    if(s[now]==1)   s.erase(now);
    else    s[now]--;
}
void set_insert(int now)
{
    if(!s.count(now))   s[now]=1;
    else    s[now]++;
}
void mo(int pos, int typ)
{
    int pre=ncnt[A[pos]], now=ncnt[A[pos]]+typ;
    ncnt[A[pos]]+=typ;
    if(pre==0)  typ_cnt++;
    if(now==0)  typ_cnt--;
    if(pre!=0)  set_remove(pre);
    if(now!=0)  set_insert(now);
}
ll sum(ll a, ll d, ll n)
{
    return (a*(a+1)*n+(2*a+1)*d*n*(n-1)/2+d*d*(n-1)*n*(2*n-1)/6)/2;
}
void typ_insert(int now, int num)
{
    if(num==0)  return;
    nsum+=num;
    if(nsum==typ_cnt) num--;
    if(num==0)  return;
    if(lval>rval)   swap(lval, rval);
    int lcnt=0, rcnt=0;
    lcnt+=min((ll)num, (rval-lval)/now);
    num-=lcnt;
    if(num%2==0)    lcnt+=num/2, rcnt+=num/2;
    else    lcnt+=num/2+1, rcnt+=num/2;
    ans-=sum(lval+now, now, lcnt);
    ans-=sum(L-lval-lcnt*now, now, lcnt);
    lval+=lcnt*now;
    ans-=sum(rval+now, now, rcnt);
    ans-=sum(L-rval-rcnt*now, now, rcnt);
    rval+=rcnt*now;
}
void print()
{
    for(int i=1;i<=M;i++)   printf("cnt[%d]=%d\n", i, tcnt[i]);
    for(auto iter=s.begin();iter!=s.end();iter++)   printf("%d ", *iter);
    printf("\n");
}
int main()
{
    gibon
    cin >> N >> Q;
    M=sqrt(N);
    for(int i=1;i<=N;i++)   cin >> A[i];
    for(int i=0;i<Q;i++)
    {
        cin >> T[i].s >> T[i].e;
        T[i].idx=i;
    }
    sort(T, T+Q, cmp1);
    for(int i=0;i<Q;i++)
    {
        while(ne<T[i].e)
        {
            ne++;
            mo(ne, 1);
        }
        while(ns>T[i].s)
        {
            ns--;
            mo(ns, 1);
        }
        while(ne>T[i].e)
        {
            mo(ne, -1);
            ne--;
        }
        while(ns<T[i].s)
        {
            mo(ns, -1);
            ns++;
        }
        L=T[i].e-T[i].s+1;
        nsum=0;
        ans=(ll)L*(L+1)/2*typ_cnt;
        lval=0, rval=0;
        for(auto ele : s)
        {
            typ_insert(ele.fir, ele.sec);
        }
        T[i].ans=ans;
    }
    sort(T, T+Q, [](query a, query b){return a.idx<b.idx;});
    for(int i=0;i<Q;i++)    cout << T[i].ans << '\n';
}

Compilation message

diversity.cpp: In function 'void print()':
diversity.cpp:87:62: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::pair<const int, int>' [-Wformat=]
   87 |     for(auto iter=s.begin();iter!=s.end();iter++)   printf("%d ", *iter);
      |                                                             ~^    ~~~~~
      |                                                              |    |
      |                                                              int  std::pair<const int, int>
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 15 ms 924 KB Output is correct
5 Correct 32 ms 1484 KB Output is correct
6 Correct 58 ms 1952 KB Output is correct
7 Correct 43 ms 1996 KB Output is correct
8 Correct 55 ms 1928 KB Output is correct
9 Correct 52 ms 1976 KB Output is correct
10 Correct 49 ms 2000 KB Output is correct
11 Correct 56 ms 1984 KB Output is correct
12 Correct 69 ms 1892 KB Output is correct
13 Correct 52 ms 1972 KB Output is correct
14 Correct 48 ms 2004 KB Output is correct
15 Correct 47 ms 2000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 15 ms 924 KB Output is correct
5 Correct 32 ms 1484 KB Output is correct
6 Correct 58 ms 1952 KB Output is correct
7 Correct 43 ms 1996 KB Output is correct
8 Correct 55 ms 1928 KB Output is correct
9 Correct 52 ms 1976 KB Output is correct
10 Correct 49 ms 2000 KB Output is correct
11 Correct 56 ms 1984 KB Output is correct
12 Correct 69 ms 1892 KB Output is correct
13 Correct 52 ms 1972 KB Output is correct
14 Correct 48 ms 2004 KB Output is correct
15 Correct 47 ms 2000 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 3 ms 392 KB Output is correct
19 Correct 20 ms 956 KB Output is correct
20 Correct 40 ms 1612 KB Output is correct
21 Correct 59 ms 1928 KB Output is correct
22 Correct 62 ms 1996 KB Output is correct
23 Correct 61 ms 1996 KB Output is correct
24 Correct 61 ms 1992 KB Output is correct
25 Correct 61 ms 2076 KB Output is correct
26 Correct 63 ms 1996 KB Output is correct
27 Correct 57 ms 2000 KB Output is correct
28 Correct 62 ms 2008 KB Output is correct
29 Correct 77 ms 1992 KB Output is correct
30 Correct 60 ms 2012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 15 ms 924 KB Output is correct
5 Correct 32 ms 1484 KB Output is correct
6 Correct 58 ms 1952 KB Output is correct
7 Correct 43 ms 1996 KB Output is correct
8 Correct 55 ms 1928 KB Output is correct
9 Correct 52 ms 1976 KB Output is correct
10 Correct 49 ms 2000 KB Output is correct
11 Correct 56 ms 1984 KB Output is correct
12 Correct 69 ms 1892 KB Output is correct
13 Correct 52 ms 1972 KB Output is correct
14 Correct 48 ms 2004 KB Output is correct
15 Correct 47 ms 2000 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 3 ms 392 KB Output is correct
19 Correct 20 ms 956 KB Output is correct
20 Correct 40 ms 1612 KB Output is correct
21 Correct 59 ms 1928 KB Output is correct
22 Correct 62 ms 1996 KB Output is correct
23 Correct 61 ms 1996 KB Output is correct
24 Correct 61 ms 1992 KB Output is correct
25 Correct 61 ms 2076 KB Output is correct
26 Correct 63 ms 1996 KB Output is correct
27 Correct 57 ms 2000 KB Output is correct
28 Correct 62 ms 2008 KB Output is correct
29 Correct 77 ms 1992 KB Output is correct
30 Correct 60 ms 2012 KB Output is correct
31 Correct 1 ms 312 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 16 ms 732 KB Output is correct
38 Correct 13 ms 716 KB Output is correct
39 Correct 28 ms 1100 KB Output is correct
40 Correct 55 ms 1624 KB Output is correct
41 Correct 84 ms 1996 KB Output is correct
42 Correct 93 ms 2028 KB Output is correct
43 Correct 82 ms 1956 KB Output is correct
44 Correct 83 ms 1988 KB Output is correct
45 Correct 83 ms 2016 KB Output is correct
46 Correct 79 ms 2020 KB Output is correct
47 Correct 83 ms 1988 KB Output is correct
48 Correct 96 ms 2148 KB Output is correct
49 Correct 103 ms 1936 KB Output is correct
50 Correct 77 ms 1992 KB Output is correct
51 Correct 78 ms 1996 KB Output is correct
52 Correct 78 ms 2020 KB Output is correct
53 Correct 86 ms 1924 KB Output is correct
54 Correct 88 ms 2028 KB Output is correct
55 Correct 78 ms 2028 KB Output is correct
56 Correct 78 ms 1988 KB Output is correct
57 Correct 101 ms 2156 KB Output is correct
58 Correct 89 ms 2020 KB Output is correct
59 Correct 84 ms 1928 KB Output is correct
60 Correct 83 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 15 ms 924 KB Output is correct
15 Correct 32 ms 1484 KB Output is correct
16 Correct 58 ms 1952 KB Output is correct
17 Correct 43 ms 1996 KB Output is correct
18 Correct 55 ms 1928 KB Output is correct
19 Correct 52 ms 1976 KB Output is correct
20 Correct 49 ms 2000 KB Output is correct
21 Correct 56 ms 1984 KB Output is correct
22 Correct 69 ms 1892 KB Output is correct
23 Correct 52 ms 1972 KB Output is correct
24 Correct 48 ms 2004 KB Output is correct
25 Correct 47 ms 2000 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 300 KB Output is correct
28 Correct 3 ms 392 KB Output is correct
29 Correct 20 ms 956 KB Output is correct
30 Correct 40 ms 1612 KB Output is correct
31 Correct 59 ms 1928 KB Output is correct
32 Correct 62 ms 1996 KB Output is correct
33 Correct 61 ms 1996 KB Output is correct
34 Correct 61 ms 1992 KB Output is correct
35 Correct 61 ms 2076 KB Output is correct
36 Correct 63 ms 1996 KB Output is correct
37 Correct 57 ms 2000 KB Output is correct
38 Correct 62 ms 2008 KB Output is correct
39 Correct 77 ms 1992 KB Output is correct
40 Correct 60 ms 2012 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 3 ms 332 KB Output is correct
45 Correct 3 ms 332 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 16 ms 732 KB Output is correct
48 Correct 13 ms 716 KB Output is correct
49 Correct 28 ms 1100 KB Output is correct
50 Correct 55 ms 1624 KB Output is correct
51 Correct 84 ms 1996 KB Output is correct
52 Correct 93 ms 2028 KB Output is correct
53 Correct 82 ms 1956 KB Output is correct
54 Correct 83 ms 1988 KB Output is correct
55 Correct 83 ms 2016 KB Output is correct
56 Correct 79 ms 2020 KB Output is correct
57 Correct 83 ms 1988 KB Output is correct
58 Correct 96 ms 2148 KB Output is correct
59 Correct 103 ms 1936 KB Output is correct
60 Correct 77 ms 1992 KB Output is correct
61 Correct 78 ms 1996 KB Output is correct
62 Correct 78 ms 2020 KB Output is correct
63 Correct 86 ms 1924 KB Output is correct
64 Correct 88 ms 2028 KB Output is correct
65 Correct 78 ms 2028 KB Output is correct
66 Correct 78 ms 1988 KB Output is correct
67 Correct 101 ms 2156 KB Output is correct
68 Correct 89 ms 2020 KB Output is correct
69 Correct 84 ms 1928 KB Output is correct
70 Correct 83 ms 1920 KB Output is correct
71 Correct 9 ms 716 KB Output is correct
72 Correct 10 ms 716 KB Output is correct
73 Correct 11 ms 716 KB Output is correct
74 Correct 14 ms 768 KB Output is correct
75 Correct 12 ms 716 KB Output is correct
76 Correct 16 ms 1220 KB Output is correct
77 Correct 14 ms 1220 KB Output is correct
78 Correct 14 ms 1100 KB Output is correct
79 Correct 15 ms 1100 KB Output is correct
80 Correct 14 ms 1100 KB Output is correct
81 Correct 29 ms 1660 KB Output is correct
82 Correct 30 ms 1608 KB Output is correct
83 Correct 29 ms 1612 KB Output is correct
84 Correct 28 ms 1612 KB Output is correct
85 Correct 31 ms 1728 KB Output is correct
86 Correct 40 ms 1732 KB Output is correct
87 Correct 31 ms 1896 KB Output is correct
88 Correct 29 ms 1948 KB Output is correct
89 Correct 26 ms 1956 KB Output is correct
90 Correct 28 ms 1868 KB Output is correct
91 Correct 53 ms 2644 KB Output is correct
92 Correct 52 ms 2596 KB Output is correct
93 Correct 35 ms 2628 KB Output is correct
94 Correct 39 ms 2628 KB Output is correct
95 Correct 34 ms 2652 KB Output is correct
96 Correct 36 ms 3040 KB Output is correct
97 Correct 41 ms 3028 KB Output is correct
98 Correct 38 ms 2976 KB Output is correct
99 Correct 38 ms 2984 KB Output is correct
100 Correct 37 ms 2964 KB Output is correct
101 Correct 40 ms 2952 KB Output is correct
102 Correct 37 ms 3040 KB Output is correct
103 Correct 56 ms 3040 KB Output is correct
104 Correct 33 ms 3008 KB Output is correct
105 Correct 38 ms 3008 KB Output is correct
106 Correct 38 ms 3164 KB Output is correct
107 Correct 53 ms 3136 KB Output is correct
108 Correct 45 ms 3140 KB Output is correct
109 Correct 47 ms 3192 KB Output is correct
110 Correct 33 ms 3312 KB Output is correct
111 Correct 36 ms 3112 KB Output is correct
112 Correct 44 ms 3136 KB Output is correct
113 Correct 40 ms 3092 KB Output is correct
114 Correct 28 ms 2884 KB Output is correct
115 Correct 29 ms 2860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 15 ms 924 KB Output is correct
15 Correct 32 ms 1484 KB Output is correct
16 Correct 58 ms 1952 KB Output is correct
17 Correct 43 ms 1996 KB Output is correct
18 Correct 55 ms 1928 KB Output is correct
19 Correct 52 ms 1976 KB Output is correct
20 Correct 49 ms 2000 KB Output is correct
21 Correct 56 ms 1984 KB Output is correct
22 Correct 69 ms 1892 KB Output is correct
23 Correct 52 ms 1972 KB Output is correct
24 Correct 48 ms 2004 KB Output is correct
25 Correct 47 ms 2000 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 300 KB Output is correct
28 Correct 3 ms 392 KB Output is correct
29 Correct 20 ms 956 KB Output is correct
30 Correct 40 ms 1612 KB Output is correct
31 Correct 59 ms 1928 KB Output is correct
32 Correct 62 ms 1996 KB Output is correct
33 Correct 61 ms 1996 KB Output is correct
34 Correct 61 ms 1992 KB Output is correct
35 Correct 61 ms 2076 KB Output is correct
36 Correct 63 ms 1996 KB Output is correct
37 Correct 57 ms 2000 KB Output is correct
38 Correct 62 ms 2008 KB Output is correct
39 Correct 77 ms 1992 KB Output is correct
40 Correct 60 ms 2012 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 3 ms 332 KB Output is correct
45 Correct 3 ms 332 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 16 ms 732 KB Output is correct
48 Correct 13 ms 716 KB Output is correct
49 Correct 28 ms 1100 KB Output is correct
50 Correct 55 ms 1624 KB Output is correct
51 Correct 84 ms 1996 KB Output is correct
52 Correct 93 ms 2028 KB Output is correct
53 Correct 82 ms 1956 KB Output is correct
54 Correct 83 ms 1988 KB Output is correct
55 Correct 83 ms 2016 KB Output is correct
56 Correct 79 ms 2020 KB Output is correct
57 Correct 83 ms 1988 KB Output is correct
58 Correct 96 ms 2148 KB Output is correct
59 Correct 103 ms 1936 KB Output is correct
60 Correct 77 ms 1992 KB Output is correct
61 Correct 78 ms 1996 KB Output is correct
62 Correct 78 ms 2020 KB Output is correct
63 Correct 86 ms 1924 KB Output is correct
64 Correct 88 ms 2028 KB Output is correct
65 Correct 78 ms 2028 KB Output is correct
66 Correct 78 ms 1988 KB Output is correct
67 Correct 101 ms 2156 KB Output is correct
68 Correct 89 ms 2020 KB Output is correct
69 Correct 84 ms 1928 KB Output is correct
70 Correct 83 ms 1920 KB Output is correct
71 Correct 9 ms 716 KB Output is correct
72 Correct 10 ms 716 KB Output is correct
73 Correct 11 ms 716 KB Output is correct
74 Correct 14 ms 768 KB Output is correct
75 Correct 12 ms 716 KB Output is correct
76 Correct 16 ms 1220 KB Output is correct
77 Correct 14 ms 1220 KB Output is correct
78 Correct 14 ms 1100 KB Output is correct
79 Correct 15 ms 1100 KB Output is correct
80 Correct 14 ms 1100 KB Output is correct
81 Correct 29 ms 1660 KB Output is correct
82 Correct 30 ms 1608 KB Output is correct
83 Correct 29 ms 1612 KB Output is correct
84 Correct 28 ms 1612 KB Output is correct
85 Correct 31 ms 1728 KB Output is correct
86 Correct 40 ms 1732 KB Output is correct
87 Correct 31 ms 1896 KB Output is correct
88 Correct 29 ms 1948 KB Output is correct
89 Correct 26 ms 1956 KB Output is correct
90 Correct 28 ms 1868 KB Output is correct
91 Correct 53 ms 2644 KB Output is correct
92 Correct 52 ms 2596 KB Output is correct
93 Correct 35 ms 2628 KB Output is correct
94 Correct 39 ms 2628 KB Output is correct
95 Correct 34 ms 2652 KB Output is correct
96 Correct 36 ms 3040 KB Output is correct
97 Correct 41 ms 3028 KB Output is correct
98 Correct 38 ms 2976 KB Output is correct
99 Correct 38 ms 2984 KB Output is correct
100 Correct 37 ms 2964 KB Output is correct
101 Correct 40 ms 2952 KB Output is correct
102 Correct 37 ms 3040 KB Output is correct
103 Correct 56 ms 3040 KB Output is correct
104 Correct 33 ms 3008 KB Output is correct
105 Correct 38 ms 3008 KB Output is correct
106 Correct 38 ms 3164 KB Output is correct
107 Correct 53 ms 3136 KB Output is correct
108 Correct 45 ms 3140 KB Output is correct
109 Correct 47 ms 3192 KB Output is correct
110 Correct 33 ms 3312 KB Output is correct
111 Correct 36 ms 3112 KB Output is correct
112 Correct 44 ms 3136 KB Output is correct
113 Correct 40 ms 3092 KB Output is correct
114 Correct 28 ms 2884 KB Output is correct
115 Correct 29 ms 2860 KB Output is correct
116 Correct 554 ms 2424 KB Output is correct
117 Correct 512 ms 2508 KB Output is correct
118 Correct 3230 ms 2848 KB Output is correct
119 Correct 2360 ms 2856 KB Output is correct
120 Correct 1653 ms 2856 KB Output is correct
121 Correct 5501 ms 3056 KB Output is correct
122 Correct 4253 ms 3052 KB Output is correct
123 Execution timed out 7008 ms 3012 KB Time limit exceeded
124 Halted 0 ms 0 KB -