Submission #1049504

# Submission time Handle Problem Language Result Execution time Memory
1049504 2024-08-08T19:51:05 Z PoonYaPat Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 243296 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

//sl : increase from l
//sr : increase from r

const int mx=300000,MID=150000,sq=1350;

struct NN {
    ll sum,sl,sr,cnt;
} s[1<<21];

int ccnt[mx+5],posi[mx+5],num[mx+5];

NN merge(NN a, NN b) {
    NN res;
    res.sum=a.sum+b.sum;
    res.cnt=a.cnt+b.cnt;
    res.sl=a.sl+a.cnt*b.sum+b.sl;
    res.sr=b.sr+b.cnt*a.sum+a.sr;
    return res;
}

ll fast2_query(int l, int r, int idx, int x) {
    if (x>r) return s[idx].sr+s[idx].sum*(ll)(x-r);
    if (x<l) return s[idx].sl+s[idx].sum*(ll)(l-x);
    if (l==r) {
        s[idx]={ccnt[l],ccnt[l],ccnt[l],1};
        return ccnt[l];
    }
    int mid=(l+r)/2;
    ll res1=fast2_query(l,mid,2*idx,x);
    ll res2=fast2_query(mid+1,r,2*idx+1,x);
    s[idx]=merge(s[2*idx],s[2*idx+1]);
    return res1+res2;
}

int level[mx+5],have[mx+5],idx_seq=-1; //range of box that has been used
vector<int> seq;
deque<int> dq[mx+5];

ll ans=0;

void sw(int a, int b) {
    swap(num[a],num[b]);
    posi[num[a]]=a;
    posi[num[b]]=b;
}

void add(int val) {

    if (!have[val]) {
        
        ++idx_seq;
        int pos=seq[idx_seq]; //the position of the seg-tree to be added
        ccnt[pos]=1;
        posi[val]=pos;
        num[pos]=val;
        dq[1].push_back(pos);
        ans+=fast2_query(1,mx,1,pos);

    } else {

        int pos=posi[val], cnt=ccnt[pos]; //increase from cnt to cnt+1
        int max_pos=dq[cnt].front();
        sw(pos,max_pos);

        //increase from cnt to cnt+1
        dq[cnt].pop_front();
        dq[cnt+1].push_back(max_pos);
        ++ccnt[max_pos];
        ans+=fast2_query(1,mx,1,max_pos);
    }

    ++have[val];
}

void del(int val) {

    int pos=posi[val], cnt=ccnt[pos];
    int min_pos=dq[cnt].back();
    sw(pos,min_pos);

    
    dq[cnt].pop_back();

    if (cnt==1) --idx_seq;
    else dq[cnt-1].push_front(min_pos);

    --ccnt[min_pos];
    ans-=(fast2_query(1,mx,1,min_pos)+1);

    --have[val];
}

int n,q,a[mx+5];

struct Query {
    int l,r,idx;
    bool operator<(Query other) const {
        return pii(l/sq,((l/sq)&1) ? -r : +r)<pii(other.l/sq,((other.l/sq)&1) ? -other.r : +other.r);
    }
};

vector<Query> ques;
ll Ans[50005];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int ll=MID,rr=MID;
    level[MID]=0;
    seq.push_back(MID);
    for (int i=1; i<mx; ++i) {
        if (i%2==0) level[--ll]=i, seq.push_back(ll);
        else level[++rr]=i, seq.push_back(rr);
    }

    cin>>n>>q;
    for (int i=0; i<n; ++i) cin>>a[i];

    for (int i=0; i<q; ++i) {
        int l,r; cin>>l>>r;
        ques.push_back({--l,--r,i});
    }

    sort(ques.begin(),ques.end());

    int cur_l=0;
    int cur_r=-1;

    for (auto q : ques) {
        while (cur_l>q.l) add(a[--cur_l]);
        while (cur_r<q.r) add(a[++cur_r]);
        while (cur_l<q.l) del(a[cur_l++]);
        while (cur_r>q.r) del(a[cur_r--]);
        Ans[q.idx]=ans;
    }

    for (int i=0; i<q; ++i) cout<<Ans[i]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 71 ms 210884 KB Output is correct
2 Correct 78 ms 212896 KB Output is correct
3 Correct 65 ms 212932 KB Output is correct
4 Correct 77 ms 212928 KB Output is correct
5 Correct 78 ms 212928 KB Output is correct
6 Correct 73 ms 212872 KB Output is correct
7 Correct 73 ms 212928 KB Output is correct
8 Correct 71 ms 212896 KB Output is correct
9 Correct 74 ms 212932 KB Output is correct
10 Correct 74 ms 212932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 213080 KB Output is correct
2 Correct 77 ms 212888 KB Output is correct
3 Correct 76 ms 212932 KB Output is correct
4 Correct 92 ms 213184 KB Output is correct
5 Correct 108 ms 213444 KB Output is correct
6 Correct 127 ms 213700 KB Output is correct
7 Correct 130 ms 213764 KB Output is correct
8 Correct 118 ms 213696 KB Output is correct
9 Correct 132 ms 213500 KB Output is correct
10 Correct 122 ms 213696 KB Output is correct
11 Correct 130 ms 213728 KB Output is correct
12 Correct 123 ms 213700 KB Output is correct
13 Correct 123 ms 213516 KB Output is correct
14 Correct 124 ms 213696 KB Output is correct
15 Correct 122 ms 213700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 213080 KB Output is correct
2 Correct 77 ms 212888 KB Output is correct
3 Correct 76 ms 212932 KB Output is correct
4 Correct 92 ms 213184 KB Output is correct
5 Correct 108 ms 213444 KB Output is correct
6 Correct 127 ms 213700 KB Output is correct
7 Correct 130 ms 213764 KB Output is correct
8 Correct 118 ms 213696 KB Output is correct
9 Correct 132 ms 213500 KB Output is correct
10 Correct 122 ms 213696 KB Output is correct
11 Correct 130 ms 213728 KB Output is correct
12 Correct 123 ms 213700 KB Output is correct
13 Correct 123 ms 213516 KB Output is correct
14 Correct 124 ms 213696 KB Output is correct
15 Correct 122 ms 213700 KB Output is correct
16 Correct 74 ms 212932 KB Output is correct
17 Correct 70 ms 212940 KB Output is correct
18 Correct 76 ms 213088 KB Output is correct
19 Correct 94 ms 213256 KB Output is correct
20 Correct 108 ms 213444 KB Output is correct
21 Correct 122 ms 213652 KB Output is correct
22 Correct 127 ms 213440 KB Output is correct
23 Correct 123 ms 213440 KB Output is correct
24 Correct 129 ms 213592 KB Output is correct
25 Correct 123 ms 213560 KB Output is correct
26 Correct 121 ms 213688 KB Output is correct
27 Correct 123 ms 213592 KB Output is correct
28 Correct 127 ms 213688 KB Output is correct
29 Correct 125 ms 213680 KB Output is correct
30 Correct 134 ms 213696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 213080 KB Output is correct
2 Correct 77 ms 212888 KB Output is correct
3 Correct 76 ms 212932 KB Output is correct
4 Correct 92 ms 213184 KB Output is correct
5 Correct 108 ms 213444 KB Output is correct
6 Correct 127 ms 213700 KB Output is correct
7 Correct 130 ms 213764 KB Output is correct
8 Correct 118 ms 213696 KB Output is correct
9 Correct 132 ms 213500 KB Output is correct
10 Correct 122 ms 213696 KB Output is correct
11 Correct 130 ms 213728 KB Output is correct
12 Correct 123 ms 213700 KB Output is correct
13 Correct 123 ms 213516 KB Output is correct
14 Correct 124 ms 213696 KB Output is correct
15 Correct 122 ms 213700 KB Output is correct
16 Correct 74 ms 212932 KB Output is correct
17 Correct 70 ms 212940 KB Output is correct
18 Correct 76 ms 213088 KB Output is correct
19 Correct 94 ms 213256 KB Output is correct
20 Correct 108 ms 213444 KB Output is correct
21 Correct 122 ms 213652 KB Output is correct
22 Correct 127 ms 213440 KB Output is correct
23 Correct 123 ms 213440 KB Output is correct
24 Correct 129 ms 213592 KB Output is correct
25 Correct 123 ms 213560 KB Output is correct
26 Correct 121 ms 213688 KB Output is correct
27 Correct 123 ms 213592 KB Output is correct
28 Correct 127 ms 213688 KB Output is correct
29 Correct 125 ms 213680 KB Output is correct
30 Correct 134 ms 213696 KB Output is correct
31 Correct 73 ms 212924 KB Output is correct
32 Correct 75 ms 213104 KB Output is correct
33 Correct 73 ms 212904 KB Output is correct
34 Correct 74 ms 212932 KB Output is correct
35 Correct 75 ms 212932 KB Output is correct
36 Correct 84 ms 213008 KB Output is correct
37 Correct 94 ms 213256 KB Output is correct
38 Correct 92 ms 213188 KB Output is correct
39 Correct 101 ms 213440 KB Output is correct
40 Correct 115 ms 213700 KB Output is correct
41 Correct 135 ms 213952 KB Output is correct
42 Correct 132 ms 214048 KB Output is correct
43 Correct 139 ms 213956 KB Output is correct
44 Correct 140 ms 213840 KB Output is correct
45 Correct 145 ms 213952 KB Output is correct
46 Correct 137 ms 213892 KB Output is correct
47 Correct 135 ms 213804 KB Output is correct
48 Correct 135 ms 214020 KB Output is correct
49 Correct 136 ms 213832 KB Output is correct
50 Correct 135 ms 214060 KB Output is correct
51 Correct 135 ms 213956 KB Output is correct
52 Correct 139 ms 214208 KB Output is correct
53 Correct 137 ms 213896 KB Output is correct
54 Correct 135 ms 213840 KB Output is correct
55 Correct 129 ms 213952 KB Output is correct
56 Correct 135 ms 213956 KB Output is correct
57 Correct 137 ms 214040 KB Output is correct
58 Correct 133 ms 213956 KB Output is correct
59 Correct 141 ms 213956 KB Output is correct
60 Correct 133 ms 213956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 210884 KB Output is correct
2 Correct 78 ms 212896 KB Output is correct
3 Correct 65 ms 212932 KB Output is correct
4 Correct 77 ms 212928 KB Output is correct
5 Correct 78 ms 212928 KB Output is correct
6 Correct 73 ms 212872 KB Output is correct
7 Correct 73 ms 212928 KB Output is correct
8 Correct 71 ms 212896 KB Output is correct
9 Correct 74 ms 212932 KB Output is correct
10 Correct 74 ms 212932 KB Output is correct
11 Correct 77 ms 213080 KB Output is correct
12 Correct 77 ms 212888 KB Output is correct
13 Correct 76 ms 212932 KB Output is correct
14 Correct 92 ms 213184 KB Output is correct
15 Correct 108 ms 213444 KB Output is correct
16 Correct 127 ms 213700 KB Output is correct
17 Correct 130 ms 213764 KB Output is correct
18 Correct 118 ms 213696 KB Output is correct
19 Correct 132 ms 213500 KB Output is correct
20 Correct 122 ms 213696 KB Output is correct
21 Correct 130 ms 213728 KB Output is correct
22 Correct 123 ms 213700 KB Output is correct
23 Correct 123 ms 213516 KB Output is correct
24 Correct 124 ms 213696 KB Output is correct
25 Correct 122 ms 213700 KB Output is correct
26 Correct 74 ms 212932 KB Output is correct
27 Correct 70 ms 212940 KB Output is correct
28 Correct 76 ms 213088 KB Output is correct
29 Correct 94 ms 213256 KB Output is correct
30 Correct 108 ms 213444 KB Output is correct
31 Correct 122 ms 213652 KB Output is correct
32 Correct 127 ms 213440 KB Output is correct
33 Correct 123 ms 213440 KB Output is correct
34 Correct 129 ms 213592 KB Output is correct
35 Correct 123 ms 213560 KB Output is correct
36 Correct 121 ms 213688 KB Output is correct
37 Correct 123 ms 213592 KB Output is correct
38 Correct 127 ms 213688 KB Output is correct
39 Correct 125 ms 213680 KB Output is correct
40 Correct 134 ms 213696 KB Output is correct
41 Correct 73 ms 212924 KB Output is correct
42 Correct 75 ms 213104 KB Output is correct
43 Correct 73 ms 212904 KB Output is correct
44 Correct 74 ms 212932 KB Output is correct
45 Correct 75 ms 212932 KB Output is correct
46 Correct 84 ms 213008 KB Output is correct
47 Correct 94 ms 213256 KB Output is correct
48 Correct 92 ms 213188 KB Output is correct
49 Correct 101 ms 213440 KB Output is correct
50 Correct 115 ms 213700 KB Output is correct
51 Correct 135 ms 213952 KB Output is correct
52 Correct 132 ms 214048 KB Output is correct
53 Correct 139 ms 213956 KB Output is correct
54 Correct 140 ms 213840 KB Output is correct
55 Correct 145 ms 213952 KB Output is correct
56 Correct 137 ms 213892 KB Output is correct
57 Correct 135 ms 213804 KB Output is correct
58 Correct 135 ms 214020 KB Output is correct
59 Correct 136 ms 213832 KB Output is correct
60 Correct 135 ms 214060 KB Output is correct
61 Correct 135 ms 213956 KB Output is correct
62 Correct 139 ms 214208 KB Output is correct
63 Correct 137 ms 213896 KB Output is correct
64 Correct 135 ms 213840 KB Output is correct
65 Correct 129 ms 213952 KB Output is correct
66 Correct 135 ms 213956 KB Output is correct
67 Correct 137 ms 214040 KB Output is correct
68 Correct 133 ms 213956 KB Output is correct
69 Correct 141 ms 213956 KB Output is correct
70 Correct 133 ms 213956 KB Output is correct
71 Correct 90 ms 215488 KB Output is correct
72 Correct 87 ms 215496 KB Output is correct
73 Correct 89 ms 215492 KB Output is correct
74 Correct 93 ms 215488 KB Output is correct
75 Correct 94 ms 215488 KB Output is correct
76 Correct 95 ms 217792 KB Output is correct
77 Correct 101 ms 217792 KB Output is correct
78 Correct 99 ms 217792 KB Output is correct
79 Correct 104 ms 217792 KB Output is correct
80 Correct 101 ms 217792 KB Output is correct
81 Correct 121 ms 220280 KB Output is correct
82 Correct 121 ms 220348 KB Output is correct
83 Correct 121 ms 220356 KB Output is correct
84 Correct 126 ms 220348 KB Output is correct
85 Correct 130 ms 220348 KB Output is correct
86 Correct 117 ms 220804 KB Output is correct
87 Correct 112 ms 220696 KB Output is correct
88 Correct 116 ms 220608 KB Output is correct
89 Correct 116 ms 220604 KB Output is correct
90 Correct 119 ms 220612 KB Output is correct
91 Correct 143 ms 227264 KB Output is correct
92 Correct 144 ms 227008 KB Output is correct
93 Correct 141 ms 227028 KB Output is correct
94 Correct 151 ms 227008 KB Output is correct
95 Correct 157 ms 227008 KB Output is correct
96 Correct 144 ms 237780 KB Output is correct
97 Correct 139 ms 237928 KB Output is correct
98 Correct 146 ms 237756 KB Output is correct
99 Correct 148 ms 237960 KB Output is correct
100 Correct 143 ms 237756 KB Output is correct
101 Correct 146 ms 237936 KB Output is correct
102 Correct 148 ms 237756 KB Output is correct
103 Correct 144 ms 237748 KB Output is correct
104 Correct 145 ms 237948 KB Output is correct
105 Correct 150 ms 237852 KB Output is correct
106 Correct 144 ms 243268 KB Output is correct
107 Correct 142 ms 243132 KB Output is correct
108 Correct 143 ms 243136 KB Output is correct
109 Correct 141 ms 243296 KB Output is correct
110 Correct 158 ms 243132 KB Output is correct
111 Correct 142 ms 243136 KB Output is correct
112 Correct 142 ms 243136 KB Output is correct
113 Correct 148 ms 243088 KB Output is correct
114 Correct 140 ms 243132 KB Output is correct
115 Correct 144 ms 243120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 210884 KB Output is correct
2 Correct 78 ms 212896 KB Output is correct
3 Correct 65 ms 212932 KB Output is correct
4 Correct 77 ms 212928 KB Output is correct
5 Correct 78 ms 212928 KB Output is correct
6 Correct 73 ms 212872 KB Output is correct
7 Correct 73 ms 212928 KB Output is correct
8 Correct 71 ms 212896 KB Output is correct
9 Correct 74 ms 212932 KB Output is correct
10 Correct 74 ms 212932 KB Output is correct
11 Correct 77 ms 213080 KB Output is correct
12 Correct 77 ms 212888 KB Output is correct
13 Correct 76 ms 212932 KB Output is correct
14 Correct 92 ms 213184 KB Output is correct
15 Correct 108 ms 213444 KB Output is correct
16 Correct 127 ms 213700 KB Output is correct
17 Correct 130 ms 213764 KB Output is correct
18 Correct 118 ms 213696 KB Output is correct
19 Correct 132 ms 213500 KB Output is correct
20 Correct 122 ms 213696 KB Output is correct
21 Correct 130 ms 213728 KB Output is correct
22 Correct 123 ms 213700 KB Output is correct
23 Correct 123 ms 213516 KB Output is correct
24 Correct 124 ms 213696 KB Output is correct
25 Correct 122 ms 213700 KB Output is correct
26 Correct 74 ms 212932 KB Output is correct
27 Correct 70 ms 212940 KB Output is correct
28 Correct 76 ms 213088 KB Output is correct
29 Correct 94 ms 213256 KB Output is correct
30 Correct 108 ms 213444 KB Output is correct
31 Correct 122 ms 213652 KB Output is correct
32 Correct 127 ms 213440 KB Output is correct
33 Correct 123 ms 213440 KB Output is correct
34 Correct 129 ms 213592 KB Output is correct
35 Correct 123 ms 213560 KB Output is correct
36 Correct 121 ms 213688 KB Output is correct
37 Correct 123 ms 213592 KB Output is correct
38 Correct 127 ms 213688 KB Output is correct
39 Correct 125 ms 213680 KB Output is correct
40 Correct 134 ms 213696 KB Output is correct
41 Correct 73 ms 212924 KB Output is correct
42 Correct 75 ms 213104 KB Output is correct
43 Correct 73 ms 212904 KB Output is correct
44 Correct 74 ms 212932 KB Output is correct
45 Correct 75 ms 212932 KB Output is correct
46 Correct 84 ms 213008 KB Output is correct
47 Correct 94 ms 213256 KB Output is correct
48 Correct 92 ms 213188 KB Output is correct
49 Correct 101 ms 213440 KB Output is correct
50 Correct 115 ms 213700 KB Output is correct
51 Correct 135 ms 213952 KB Output is correct
52 Correct 132 ms 214048 KB Output is correct
53 Correct 139 ms 213956 KB Output is correct
54 Correct 140 ms 213840 KB Output is correct
55 Correct 145 ms 213952 KB Output is correct
56 Correct 137 ms 213892 KB Output is correct
57 Correct 135 ms 213804 KB Output is correct
58 Correct 135 ms 214020 KB Output is correct
59 Correct 136 ms 213832 KB Output is correct
60 Correct 135 ms 214060 KB Output is correct
61 Correct 135 ms 213956 KB Output is correct
62 Correct 139 ms 214208 KB Output is correct
63 Correct 137 ms 213896 KB Output is correct
64 Correct 135 ms 213840 KB Output is correct
65 Correct 129 ms 213952 KB Output is correct
66 Correct 135 ms 213956 KB Output is correct
67 Correct 137 ms 214040 KB Output is correct
68 Correct 133 ms 213956 KB Output is correct
69 Correct 141 ms 213956 KB Output is correct
70 Correct 133 ms 213956 KB Output is correct
71 Correct 90 ms 215488 KB Output is correct
72 Correct 87 ms 215496 KB Output is correct
73 Correct 89 ms 215492 KB Output is correct
74 Correct 93 ms 215488 KB Output is correct
75 Correct 94 ms 215488 KB Output is correct
76 Correct 95 ms 217792 KB Output is correct
77 Correct 101 ms 217792 KB Output is correct
78 Correct 99 ms 217792 KB Output is correct
79 Correct 104 ms 217792 KB Output is correct
80 Correct 101 ms 217792 KB Output is correct
81 Correct 121 ms 220280 KB Output is correct
82 Correct 121 ms 220348 KB Output is correct
83 Correct 121 ms 220356 KB Output is correct
84 Correct 126 ms 220348 KB Output is correct
85 Correct 130 ms 220348 KB Output is correct
86 Correct 117 ms 220804 KB Output is correct
87 Correct 112 ms 220696 KB Output is correct
88 Correct 116 ms 220608 KB Output is correct
89 Correct 116 ms 220604 KB Output is correct
90 Correct 119 ms 220612 KB Output is correct
91 Correct 143 ms 227264 KB Output is correct
92 Correct 144 ms 227008 KB Output is correct
93 Correct 141 ms 227028 KB Output is correct
94 Correct 151 ms 227008 KB Output is correct
95 Correct 157 ms 227008 KB Output is correct
96 Correct 144 ms 237780 KB Output is correct
97 Correct 139 ms 237928 KB Output is correct
98 Correct 146 ms 237756 KB Output is correct
99 Correct 148 ms 237960 KB Output is correct
100 Correct 143 ms 237756 KB Output is correct
101 Correct 146 ms 237936 KB Output is correct
102 Correct 148 ms 237756 KB Output is correct
103 Correct 144 ms 237748 KB Output is correct
104 Correct 145 ms 237948 KB Output is correct
105 Correct 150 ms 237852 KB Output is correct
106 Correct 144 ms 243268 KB Output is correct
107 Correct 142 ms 243132 KB Output is correct
108 Correct 143 ms 243136 KB Output is correct
109 Correct 141 ms 243296 KB Output is correct
110 Correct 158 ms 243132 KB Output is correct
111 Correct 142 ms 243136 KB Output is correct
112 Correct 142 ms 243136 KB Output is correct
113 Correct 148 ms 243088 KB Output is correct
114 Correct 140 ms 243132 KB Output is correct
115 Correct 144 ms 243120 KB Output is correct
116 Correct 2795 ms 215232 KB Output is correct
117 Correct 2946 ms 215500 KB Output is correct
118 Correct 3073 ms 215488 KB Output is correct
119 Correct 3275 ms 215484 KB Output is correct
120 Correct 3410 ms 215608 KB Output is correct
121 Correct 3762 ms 215744 KB Output is correct
122 Correct 3865 ms 218048 KB Output is correct
123 Correct 5490 ms 216000 KB Output is correct
124 Correct 5701 ms 220860 KB Output is correct
125 Correct 5675 ms 223164 KB Output is correct
126 Execution timed out 7067 ms 216000 KB Time limit exceeded
127 Halted 0 ms 0 KB -