Submission #1090158

# Submission time Handle Problem Language Result Execution time Memory
1090158 2024-09-17T22:28:54 Z urd05 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 18372 KB
#include <bits/stdc++.h>
using namespace std;

int n,q;
int arr[300005];
int cnt[300005];
const int sq=500;
const int sz=524288;
typedef pair<long long,long long> P;
typedef pair<P,int> Pi;
P seg[sz*2];
const int MAX=300000;

P pl(P a,P b) {
    return P(a.first+b.first,a.second+b.second);
}

P sum(int l,int r,int node=1,int nodel=0,int noder=sz-1) {
    if (r<nodel||l>noder) {
        return P(0,0);
    }
    if (l<=nodel&&noder<=r) {
        return seg[node];
    }
    int mid=(nodel+noder)/2;
    return pl(sum(l,r,node*2,nodel,mid),sum(l,r,node*2+1,mid+1,noder));
}

void update(int i,P val) {
    i+=sz;
    seg[i]=pl(seg[i],val);
    while (i>1) {
        i/=2;
        seg[i]=pl(seg[i*2],seg[i*2+1]);
    }
}

int pos[300005];
int tree[300005];

int sum(int i) {
    i++;
    int ret=0;
    while (i>0) {
        ret+=tree[i];
        i-=(i&-i);
    }
    return ret;
}

void upd(int i,int val) {
    i++;
    while (i<=300004) {
        tree[i]+=val;
        i+=(i&-i);
    }
}

bool comp(Pi a,Pi b) {
    if (a.first.first/sq==b.first.first/sq) {
        return a.first.second<b.first.second;
    }
    return a.first.first/sq<b.first.first/sq;
}

long long value=0;

void push(int ind) {
    int now=arr[ind];
    int p=sum(cnt[now])-1;
    p=pos[p];
    upd(cnt[now],-1);
    upd(cnt[now]+1,1);
    cnt[now]++;
    P got=sum(0,p-1);
    value+=p*got.second-got.first;
    got=sum(p+1,MAX-1);
    value+=got.first-p*got.second;
    update(p,P(p,1));
}

void out(int ind) {
    int now=arr[ind];
    int p=sum(cnt[now]-1);
    p=pos[p];
    upd(cnt[now],-1);
    upd(cnt[now]-1,1);
    cnt[now]--;
    P got=sum(0,p-1);
    value-=p*got.second-got.first;
    got=sum(p+1,MAX-1);
    value-=got.first-p*got.second;
    update(p,P(-p,-1));
}

long long ret[50005];

int main(void) {
    scanf("%d %d",&n,&q);
    for(int i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }
    vector<int> v;
    for(int i=1;i<=300000;i++) {
        v.push_back(i-1);
    }
    sort(v.begin(),v.end());
    vector<long long> v1;
    for(int i=0;i<v.size();i+=2) {
        v1.push_back(v[i]);
    }
    vector<long long> v2;
    for(int i=1;i<v.size();i+=2) {
        v2.push_back(v[i]);
    }
    reverse(v2.begin(),v2.end());
    for(auto x:v2) {
        v1.push_back(x);
    }
    for(int i=0;i<300000;i++) {
        pos[v1[i]]=i;
    }
    upd(0,300000);
    vector<Pi> query;
    for(int i=0;i<q;i++) {
        int l,r;
        scanf("%d %d",&l,&r);
        l--;
        r--;
        query.push_back(Pi(P(l,r),i));
    }
    sort(query.begin(),query.end(),comp);
    int l=query[0].first.first;
    int r=query[0].first.first-1;
    for(int i=0;i<q;i++) {
        int nl=query[i].first.first;
        int nr=query[i].first.second;
        while (l>nl) {
            l--;
            push(l);
        }
        while (r<nr) {
            r++;
            push(r);
        }
        while (l<nl) {
            out(l);
            l++;
        }
        while (r>nr) {
            out(r);
            r--;
        }
        int len=nr-nl+1;
        ret[query[i].second]=value+1LL*len*(len+1)/2;
    }
    for(int i=0;i<q;i++) {
        printf("%lld\n",ret[i]);
    }
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int i=0;i<v.size();i+=2) {
      |                 ~^~~~~~~~~
diversity.cpp:113:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |     for(int i=1;i<v.size();i+=2) {
      |                 ~^~~~~~~~~
diversity.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |     scanf("%d %d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
diversity.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
diversity.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         scanf("%d %d",&l,&r);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6964 KB Output is correct
2 Correct 10 ms 6864 KB Output is correct
3 Correct 10 ms 6868 KB Output is correct
4 Correct 10 ms 6868 KB Output is correct
5 Correct 10 ms 6868 KB Output is correct
6 Correct 14 ms 6868 KB Output is correct
7 Correct 12 ms 7292 KB Output is correct
8 Correct 11 ms 6868 KB Output is correct
9 Correct 13 ms 6908 KB Output is correct
10 Correct 16 ms 6868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6864 KB Output is correct
2 Correct 10 ms 6872 KB Output is correct
3 Correct 14 ms 7132 KB Output is correct
4 Correct 48 ms 7636 KB Output is correct
5 Correct 89 ms 8144 KB Output is correct
6 Correct 143 ms 8652 KB Output is correct
7 Correct 119 ms 8888 KB Output is correct
8 Correct 122 ms 8660 KB Output is correct
9 Correct 121 ms 8728 KB Output is correct
10 Correct 126 ms 8720 KB Output is correct
11 Correct 151 ms 8740 KB Output is correct
12 Correct 130 ms 8648 KB Output is correct
13 Correct 121 ms 8648 KB Output is correct
14 Correct 128 ms 8648 KB Output is correct
15 Correct 128 ms 8904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6864 KB Output is correct
2 Correct 10 ms 6872 KB Output is correct
3 Correct 14 ms 7132 KB Output is correct
4 Correct 48 ms 7636 KB Output is correct
5 Correct 89 ms 8144 KB Output is correct
6 Correct 143 ms 8652 KB Output is correct
7 Correct 119 ms 8888 KB Output is correct
8 Correct 122 ms 8660 KB Output is correct
9 Correct 121 ms 8728 KB Output is correct
10 Correct 126 ms 8720 KB Output is correct
11 Correct 151 ms 8740 KB Output is correct
12 Correct 130 ms 8648 KB Output is correct
13 Correct 121 ms 8648 KB Output is correct
14 Correct 128 ms 8648 KB Output is correct
15 Correct 128 ms 8904 KB Output is correct
16 Correct 11 ms 6868 KB Output is correct
17 Correct 11 ms 6904 KB Output is correct
18 Correct 14 ms 7124 KB Output is correct
19 Correct 49 ms 7624 KB Output is correct
20 Correct 95 ms 8148 KB Output is correct
21 Correct 124 ms 8912 KB Output is correct
22 Correct 125 ms 8916 KB Output is correct
23 Correct 122 ms 8840 KB Output is correct
24 Correct 138 ms 9000 KB Output is correct
25 Correct 127 ms 8908 KB Output is correct
26 Correct 131 ms 8916 KB Output is correct
27 Correct 127 ms 8916 KB Output is correct
28 Correct 133 ms 8900 KB Output is correct
29 Correct 141 ms 8932 KB Output is correct
30 Correct 126 ms 8908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6864 KB Output is correct
2 Correct 10 ms 6872 KB Output is correct
3 Correct 14 ms 7132 KB Output is correct
4 Correct 48 ms 7636 KB Output is correct
5 Correct 89 ms 8144 KB Output is correct
6 Correct 143 ms 8652 KB Output is correct
7 Correct 119 ms 8888 KB Output is correct
8 Correct 122 ms 8660 KB Output is correct
9 Correct 121 ms 8728 KB Output is correct
10 Correct 126 ms 8720 KB Output is correct
11 Correct 151 ms 8740 KB Output is correct
12 Correct 130 ms 8648 KB Output is correct
13 Correct 121 ms 8648 KB Output is correct
14 Correct 128 ms 8648 KB Output is correct
15 Correct 128 ms 8904 KB Output is correct
16 Correct 11 ms 6868 KB Output is correct
17 Correct 11 ms 6904 KB Output is correct
18 Correct 14 ms 7124 KB Output is correct
19 Correct 49 ms 7624 KB Output is correct
20 Correct 95 ms 8148 KB Output is correct
21 Correct 124 ms 8912 KB Output is correct
22 Correct 125 ms 8916 KB Output is correct
23 Correct 122 ms 8840 KB Output is correct
24 Correct 138 ms 9000 KB Output is correct
25 Correct 127 ms 8908 KB Output is correct
26 Correct 131 ms 8916 KB Output is correct
27 Correct 127 ms 8916 KB Output is correct
28 Correct 133 ms 8900 KB Output is correct
29 Correct 141 ms 8932 KB Output is correct
30 Correct 126 ms 8908 KB Output is correct
31 Correct 11 ms 6872 KB Output is correct
32 Correct 11 ms 6868 KB Output is correct
33 Correct 11 ms 6868 KB Output is correct
34 Correct 15 ms 7120 KB Output is correct
35 Correct 21 ms 7120 KB Output is correct
36 Correct 15 ms 7124 KB Output is correct
37 Correct 34 ms 7380 KB Output is correct
38 Correct 31 ms 7380 KB Output is correct
39 Correct 59 ms 7632 KB Output is correct
40 Correct 99 ms 8392 KB Output is correct
41 Correct 138 ms 9160 KB Output is correct
42 Correct 137 ms 9168 KB Output is correct
43 Correct 138 ms 9204 KB Output is correct
44 Correct 150 ms 9152 KB Output is correct
45 Correct 154 ms 9152 KB Output is correct
46 Correct 156 ms 9164 KB Output is correct
47 Correct 139 ms 9160 KB Output is correct
48 Correct 137 ms 9164 KB Output is correct
49 Correct 139 ms 9348 KB Output is correct
50 Correct 140 ms 9160 KB Output is correct
51 Correct 151 ms 9164 KB Output is correct
52 Correct 137 ms 9168 KB Output is correct
53 Correct 140 ms 9272 KB Output is correct
54 Correct 139 ms 9172 KB Output is correct
55 Correct 147 ms 9204 KB Output is correct
56 Correct 137 ms 9168 KB Output is correct
57 Correct 169 ms 9164 KB Output is correct
58 Correct 143 ms 9160 KB Output is correct
59 Correct 138 ms 9160 KB Output is correct
60 Correct 142 ms 9352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6964 KB Output is correct
2 Correct 10 ms 6864 KB Output is correct
3 Correct 10 ms 6868 KB Output is correct
4 Correct 10 ms 6868 KB Output is correct
5 Correct 10 ms 6868 KB Output is correct
6 Correct 14 ms 6868 KB Output is correct
7 Correct 12 ms 7292 KB Output is correct
8 Correct 11 ms 6868 KB Output is correct
9 Correct 13 ms 6908 KB Output is correct
10 Correct 16 ms 6868 KB Output is correct
11 Correct 11 ms 6864 KB Output is correct
12 Correct 10 ms 6872 KB Output is correct
13 Correct 14 ms 7132 KB Output is correct
14 Correct 48 ms 7636 KB Output is correct
15 Correct 89 ms 8144 KB Output is correct
16 Correct 143 ms 8652 KB Output is correct
17 Correct 119 ms 8888 KB Output is correct
18 Correct 122 ms 8660 KB Output is correct
19 Correct 121 ms 8728 KB Output is correct
20 Correct 126 ms 8720 KB Output is correct
21 Correct 151 ms 8740 KB Output is correct
22 Correct 130 ms 8648 KB Output is correct
23 Correct 121 ms 8648 KB Output is correct
24 Correct 128 ms 8648 KB Output is correct
25 Correct 128 ms 8904 KB Output is correct
26 Correct 11 ms 6868 KB Output is correct
27 Correct 11 ms 6904 KB Output is correct
28 Correct 14 ms 7124 KB Output is correct
29 Correct 49 ms 7624 KB Output is correct
30 Correct 95 ms 8148 KB Output is correct
31 Correct 124 ms 8912 KB Output is correct
32 Correct 125 ms 8916 KB Output is correct
33 Correct 122 ms 8840 KB Output is correct
34 Correct 138 ms 9000 KB Output is correct
35 Correct 127 ms 8908 KB Output is correct
36 Correct 131 ms 8916 KB Output is correct
37 Correct 127 ms 8916 KB Output is correct
38 Correct 133 ms 8900 KB Output is correct
39 Correct 141 ms 8932 KB Output is correct
40 Correct 126 ms 8908 KB Output is correct
41 Correct 11 ms 6872 KB Output is correct
42 Correct 11 ms 6868 KB Output is correct
43 Correct 11 ms 6868 KB Output is correct
44 Correct 15 ms 7120 KB Output is correct
45 Correct 21 ms 7120 KB Output is correct
46 Correct 15 ms 7124 KB Output is correct
47 Correct 34 ms 7380 KB Output is correct
48 Correct 31 ms 7380 KB Output is correct
49 Correct 59 ms 7632 KB Output is correct
50 Correct 99 ms 8392 KB Output is correct
51 Correct 138 ms 9160 KB Output is correct
52 Correct 137 ms 9168 KB Output is correct
53 Correct 138 ms 9204 KB Output is correct
54 Correct 150 ms 9152 KB Output is correct
55 Correct 154 ms 9152 KB Output is correct
56 Correct 156 ms 9164 KB Output is correct
57 Correct 139 ms 9160 KB Output is correct
58 Correct 137 ms 9164 KB Output is correct
59 Correct 139 ms 9348 KB Output is correct
60 Correct 140 ms 9160 KB Output is correct
61 Correct 151 ms 9164 KB Output is correct
62 Correct 137 ms 9168 KB Output is correct
63 Correct 140 ms 9272 KB Output is correct
64 Correct 139 ms 9172 KB Output is correct
65 Correct 147 ms 9204 KB Output is correct
66 Correct 137 ms 9168 KB Output is correct
67 Correct 169 ms 9164 KB Output is correct
68 Correct 143 ms 9160 KB Output is correct
69 Correct 138 ms 9160 KB Output is correct
70 Correct 142 ms 9352 KB Output is correct
71 Correct 33 ms 7380 KB Output is correct
72 Correct 35 ms 7380 KB Output is correct
73 Correct 42 ms 7376 KB Output is correct
74 Correct 32 ms 7380 KB Output is correct
75 Correct 33 ms 7452 KB Output is correct
76 Correct 54 ms 7860 KB Output is correct
77 Correct 58 ms 7892 KB Output is correct
78 Correct 62 ms 7884 KB Output is correct
79 Correct 54 ms 7888 KB Output is correct
80 Correct 54 ms 7884 KB Output is correct
81 Correct 99 ms 8660 KB Output is correct
82 Correct 108 ms 8892 KB Output is correct
83 Correct 97 ms 8900 KB Output is correct
84 Correct 110 ms 8648 KB Output is correct
85 Correct 101 ms 8660 KB Output is correct
86 Correct 105 ms 8932 KB Output is correct
87 Correct 108 ms 9056 KB Output is correct
88 Correct 101 ms 9116 KB Output is correct
89 Correct 108 ms 8968 KB Output is correct
90 Correct 101 ms 8900 KB Output is correct
91 Correct 151 ms 12956 KB Output is correct
92 Correct 160 ms 12792 KB Output is correct
93 Correct 161 ms 12732 KB Output is correct
94 Correct 168 ms 12736 KB Output is correct
95 Correct 150 ms 12840 KB Output is correct
96 Correct 150 ms 16576 KB Output is correct
97 Correct 157 ms 16628 KB Output is correct
98 Correct 162 ms 16568 KB Output is correct
99 Correct 149 ms 16512 KB Output is correct
100 Correct 152 ms 16476 KB Output is correct
101 Correct 152 ms 16568 KB Output is correct
102 Correct 150 ms 16568 KB Output is correct
103 Correct 146 ms 16584 KB Output is correct
104 Correct 168 ms 16428 KB Output is correct
105 Correct 152 ms 16592 KB Output is correct
106 Correct 149 ms 18364 KB Output is correct
107 Correct 149 ms 18232 KB Output is correct
108 Correct 146 ms 18372 KB Output is correct
109 Correct 154 ms 18360 KB Output is correct
110 Correct 149 ms 18368 KB Output is correct
111 Correct 167 ms 18304 KB Output is correct
112 Correct 155 ms 18368 KB Output is correct
113 Correct 146 ms 18364 KB Output is correct
114 Correct 154 ms 18272 KB Output is correct
115 Correct 146 ms 18308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6964 KB Output is correct
2 Correct 10 ms 6864 KB Output is correct
3 Correct 10 ms 6868 KB Output is correct
4 Correct 10 ms 6868 KB Output is correct
5 Correct 10 ms 6868 KB Output is correct
6 Correct 14 ms 6868 KB Output is correct
7 Correct 12 ms 7292 KB Output is correct
8 Correct 11 ms 6868 KB Output is correct
9 Correct 13 ms 6908 KB Output is correct
10 Correct 16 ms 6868 KB Output is correct
11 Correct 11 ms 6864 KB Output is correct
12 Correct 10 ms 6872 KB Output is correct
13 Correct 14 ms 7132 KB Output is correct
14 Correct 48 ms 7636 KB Output is correct
15 Correct 89 ms 8144 KB Output is correct
16 Correct 143 ms 8652 KB Output is correct
17 Correct 119 ms 8888 KB Output is correct
18 Correct 122 ms 8660 KB Output is correct
19 Correct 121 ms 8728 KB Output is correct
20 Correct 126 ms 8720 KB Output is correct
21 Correct 151 ms 8740 KB Output is correct
22 Correct 130 ms 8648 KB Output is correct
23 Correct 121 ms 8648 KB Output is correct
24 Correct 128 ms 8648 KB Output is correct
25 Correct 128 ms 8904 KB Output is correct
26 Correct 11 ms 6868 KB Output is correct
27 Correct 11 ms 6904 KB Output is correct
28 Correct 14 ms 7124 KB Output is correct
29 Correct 49 ms 7624 KB Output is correct
30 Correct 95 ms 8148 KB Output is correct
31 Correct 124 ms 8912 KB Output is correct
32 Correct 125 ms 8916 KB Output is correct
33 Correct 122 ms 8840 KB Output is correct
34 Correct 138 ms 9000 KB Output is correct
35 Correct 127 ms 8908 KB Output is correct
36 Correct 131 ms 8916 KB Output is correct
37 Correct 127 ms 8916 KB Output is correct
38 Correct 133 ms 8900 KB Output is correct
39 Correct 141 ms 8932 KB Output is correct
40 Correct 126 ms 8908 KB Output is correct
41 Correct 11 ms 6872 KB Output is correct
42 Correct 11 ms 6868 KB Output is correct
43 Correct 11 ms 6868 KB Output is correct
44 Correct 15 ms 7120 KB Output is correct
45 Correct 21 ms 7120 KB Output is correct
46 Correct 15 ms 7124 KB Output is correct
47 Correct 34 ms 7380 KB Output is correct
48 Correct 31 ms 7380 KB Output is correct
49 Correct 59 ms 7632 KB Output is correct
50 Correct 99 ms 8392 KB Output is correct
51 Correct 138 ms 9160 KB Output is correct
52 Correct 137 ms 9168 KB Output is correct
53 Correct 138 ms 9204 KB Output is correct
54 Correct 150 ms 9152 KB Output is correct
55 Correct 154 ms 9152 KB Output is correct
56 Correct 156 ms 9164 KB Output is correct
57 Correct 139 ms 9160 KB Output is correct
58 Correct 137 ms 9164 KB Output is correct
59 Correct 139 ms 9348 KB Output is correct
60 Correct 140 ms 9160 KB Output is correct
61 Correct 151 ms 9164 KB Output is correct
62 Correct 137 ms 9168 KB Output is correct
63 Correct 140 ms 9272 KB Output is correct
64 Correct 139 ms 9172 KB Output is correct
65 Correct 147 ms 9204 KB Output is correct
66 Correct 137 ms 9168 KB Output is correct
67 Correct 169 ms 9164 KB Output is correct
68 Correct 143 ms 9160 KB Output is correct
69 Correct 138 ms 9160 KB Output is correct
70 Correct 142 ms 9352 KB Output is correct
71 Correct 33 ms 7380 KB Output is correct
72 Correct 35 ms 7380 KB Output is correct
73 Correct 42 ms 7376 KB Output is correct
74 Correct 32 ms 7380 KB Output is correct
75 Correct 33 ms 7452 KB Output is correct
76 Correct 54 ms 7860 KB Output is correct
77 Correct 58 ms 7892 KB Output is correct
78 Correct 62 ms 7884 KB Output is correct
79 Correct 54 ms 7888 KB Output is correct
80 Correct 54 ms 7884 KB Output is correct
81 Correct 99 ms 8660 KB Output is correct
82 Correct 108 ms 8892 KB Output is correct
83 Correct 97 ms 8900 KB Output is correct
84 Correct 110 ms 8648 KB Output is correct
85 Correct 101 ms 8660 KB Output is correct
86 Correct 105 ms 8932 KB Output is correct
87 Correct 108 ms 9056 KB Output is correct
88 Correct 101 ms 9116 KB Output is correct
89 Correct 108 ms 8968 KB Output is correct
90 Correct 101 ms 8900 KB Output is correct
91 Correct 151 ms 12956 KB Output is correct
92 Correct 160 ms 12792 KB Output is correct
93 Correct 161 ms 12732 KB Output is correct
94 Correct 168 ms 12736 KB Output is correct
95 Correct 150 ms 12840 KB Output is correct
96 Correct 150 ms 16576 KB Output is correct
97 Correct 157 ms 16628 KB Output is correct
98 Correct 162 ms 16568 KB Output is correct
99 Correct 149 ms 16512 KB Output is correct
100 Correct 152 ms 16476 KB Output is correct
101 Correct 152 ms 16568 KB Output is correct
102 Correct 150 ms 16568 KB Output is correct
103 Correct 146 ms 16584 KB Output is correct
104 Correct 168 ms 16428 KB Output is correct
105 Correct 152 ms 16592 KB Output is correct
106 Correct 149 ms 18364 KB Output is correct
107 Correct 149 ms 18232 KB Output is correct
108 Correct 146 ms 18372 KB Output is correct
109 Correct 154 ms 18360 KB Output is correct
110 Correct 149 ms 18368 KB Output is correct
111 Correct 167 ms 18304 KB Output is correct
112 Correct 155 ms 18368 KB Output is correct
113 Correct 146 ms 18364 KB Output is correct
114 Correct 154 ms 18272 KB Output is correct
115 Correct 146 ms 18308 KB Output is correct
116 Correct 3018 ms 10360 KB Output is correct
117 Correct 3080 ms 10360 KB Output is correct
118 Correct 4860 ms 10980 KB Output is correct
119 Correct 4989 ms 10940 KB Output is correct
120 Correct 4989 ms 10936 KB Output is correct
121 Execution timed out 7052 ms 10856 KB Time limit exceeded
122 Halted 0 ms 0 KB -