Submission #696713

# Submission time Handle Problem Language Result Execution time Memory
696713 2023-02-07T05:53:14 Z socpite Izbori (COCI22_izbori) C++14
110 / 110
689 ms 94176 KB
#include<bits/stdc++.h>
using namespace std;
 
#define f first
#define s second
 
typedef long long ll;
typedef long double ld;
 
const int maxn = 4e5+5;
const int zr = 2e5+3;
const ll inf = 1e18+5;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll s1[4*maxn], s2[4*maxn], sumid[4*maxn];
int lz[4*maxn], A[maxn];
pair<int, int> vec[maxn];
vector<int> pos[maxn];
int n;
 
vector<int> nd;
 
void build(int l = 1, int r = maxn-1, int id = 1){
    if(l == r)sumid[id] = maxn - l;
    else{
        int mid = (l+r)>>1;
        build(l, mid, id<<1);
        build(mid+1, r, id<<1|1);
        sumid[id] = sumid[id<<1] + sumid[id<<1|1];
    } 
}
 
void push(int l, int r, int id){
    nd.push_back(id);
    if(!lz[id])return;
    s1[id]+=1LL*lz[id]*(r-l+1);
    s2[id]+=1LL*lz[id]*sumid[id];
    if(l < r){
        lz[id<<1]+=lz[id];
        nd.push_back(id<<1);
        nd.push_back(id<<1|1);
        lz[id<<1|1]+=lz[id];
    }
    lz[id] = 0;
}
 
void add(int l, int r, int ql, int qr, int id){
    push(l, r, id);
    if(qr < l || r < ql)return;
    if(ql <= l && r <= qr){
        lz[id]++;
        push(l, r, id);
    }
    else{
        int mid = (l+r)>>1;
        add(l, mid, ql, qr, id<<1);
        add(mid+1, r, ql, qr, id<<1|1);
        s1[id]= s1[id<<1] + s1[id<<1|1];
        s2[id] = s2[id<<1] + s2[id<<1|1];
 
    }
}
 
ll q2(int l, int r, int ql, int qr, int id){
    push(l, r, id);
    if(qr < l || r < ql)return 0;
    if(ql <= l && r <= qr)return s2[id] - s1[id]*(maxn-qr-1);
    else{
        int mid = (l+r)>>1;
        return q2(l, mid, ql, qr, id<<1) + q2(mid+1, r, ql, qr, id<<1|1);
    }
}
 
ll q1(int l, int r, int ql, int qr, int id){
    push(l, r, id);
    if(qr < l || r < ql)return 0;
    if(ql <= l && r <= qr)return s1[id];
    else{
        int mid = (l+r)>>1;
        return q1(l, mid, ql, qr, id<<1) + q1(mid+1, r, ql, qr, id<<1|1);
    }
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        vec[i].f = rng()%2+1;
        cin >> vec[i].f;
        A[i] = vec[i].f;
        vec[i].s = i;
    }
    cout << "\n";
    int crr = 1;
    sort(vec+1, vec+n+1);
    pos[1].push_back(vec[1].s);
    for(int i = 2; i <= n; i++){
        if(vec[i].f != vec[i-1].f)crr++;
        pos[crr].push_back(vec[i].s);
    }
    ll ans = 0;
    build();
    for(int i = 1; i <= n; i++){
        if(pos[i].empty())break;
        int crr = zr;
        add(1, maxn-1, crr, crr, 1);
        if(pos[i][0] > 1){
            add(1, maxn-1, crr-pos[i][0]+1, crr-1, 1);
            //cout << crr-pos[i][0]+1 << " " << crr-1 << "\n";
            crr-=pos[i][0]-1;
        }
        crr++;
        add(1, maxn-1, crr, crr, 1);
        ans += q1(1, maxn-1, 1, crr-1, 1);
        for(int j = 1; j < pos[i].size(); j++){
            int d= pos[i][j] - pos[i][j-1]-1;
            if(d){
                ans += q2(1, maxn-1, crr-d-1, crr-2, 1);
                ans += q1(1, maxn-1, 1, crr-d-2, 1)*d; 
                add(1, maxn-1, crr-d, crr-1, 1);
                crr-=d;
            }
            //cout << crr << "\n";
            ans += q1(1, maxn-1, 1, crr, 1);
            crr++;
            add(1, maxn-1, crr, crr, 1);
        }
        if(pos[i].back() < n){
            int d = n - pos[i].back();
            ans += q2(1, maxn-1, crr-d-1, crr-2, 1);
            ans += q1(1, maxn-1, 1, crr-d-2, 1)*d;
        }
        for(auto v: nd){
            s1[v] = 0;
            s2[v] = 0;
            lz[v] = 0;
        }
        nd.clear();
        //ans = 0;
        //cout << ans << "\n";
    }
    cout << ans;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:117:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         for(int j = 1; j < pos[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18132 KB Output is correct
2 Correct 13 ms 18132 KB Output is correct
3 Correct 11 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 12 ms 18260 KB Output is correct
6 Correct 11 ms 18272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18132 KB Output is correct
2 Correct 13 ms 18132 KB Output is correct
3 Correct 11 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 12 ms 18260 KB Output is correct
6 Correct 11 ms 18272 KB Output is correct
7 Correct 12 ms 18132 KB Output is correct
8 Correct 11 ms 18132 KB Output is correct
9 Correct 15 ms 18516 KB Output is correct
10 Correct 15 ms 18592 KB Output is correct
11 Correct 15 ms 18516 KB Output is correct
12 Correct 15 ms 18576 KB Output is correct
13 Correct 18 ms 18552 KB Output is correct
14 Correct 19 ms 18548 KB Output is correct
15 Correct 15 ms 18516 KB Output is correct
16 Correct 16 ms 18532 KB Output is correct
17 Correct 13 ms 19288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 86020 KB Output is correct
2 Correct 301 ms 86568 KB Output is correct
3 Correct 180 ms 52664 KB Output is correct
4 Correct 341 ms 87160 KB Output is correct
5 Correct 335 ms 87236 KB Output is correct
6 Correct 357 ms 87448 KB Output is correct
7 Correct 345 ms 87448 KB Output is correct
8 Correct 362 ms 87588 KB Output is correct
9 Correct 348 ms 87416 KB Output is correct
10 Correct 359 ms 87428 KB Output is correct
11 Correct 229 ms 92720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18132 KB Output is correct
2 Correct 13 ms 18132 KB Output is correct
3 Correct 11 ms 18260 KB Output is correct
4 Correct 11 ms 18260 KB Output is correct
5 Correct 12 ms 18260 KB Output is correct
6 Correct 11 ms 18272 KB Output is correct
7 Correct 12 ms 18132 KB Output is correct
8 Correct 11 ms 18132 KB Output is correct
9 Correct 15 ms 18516 KB Output is correct
10 Correct 15 ms 18592 KB Output is correct
11 Correct 15 ms 18516 KB Output is correct
12 Correct 15 ms 18576 KB Output is correct
13 Correct 18 ms 18552 KB Output is correct
14 Correct 19 ms 18548 KB Output is correct
15 Correct 15 ms 18516 KB Output is correct
16 Correct 16 ms 18532 KB Output is correct
17 Correct 13 ms 19288 KB Output is correct
18 Correct 243 ms 86020 KB Output is correct
19 Correct 301 ms 86568 KB Output is correct
20 Correct 180 ms 52664 KB Output is correct
21 Correct 341 ms 87160 KB Output is correct
22 Correct 335 ms 87236 KB Output is correct
23 Correct 357 ms 87448 KB Output is correct
24 Correct 345 ms 87448 KB Output is correct
25 Correct 362 ms 87588 KB Output is correct
26 Correct 348 ms 87416 KB Output is correct
27 Correct 359 ms 87428 KB Output is correct
28 Correct 229 ms 92720 KB Output is correct
29 Correct 245 ms 94176 KB Output is correct
30 Correct 137 ms 22008 KB Output is correct
31 Correct 262 ms 25440 KB Output is correct
32 Correct 680 ms 34908 KB Output is correct
33 Correct 268 ms 25892 KB Output is correct
34 Correct 308 ms 26256 KB Output is correct
35 Correct 186 ms 23500 KB Output is correct
36 Correct 138 ms 21412 KB Output is correct
37 Correct 135 ms 22152 KB Output is correct
38 Correct 422 ms 58280 KB Output is correct
39 Correct 425 ms 58216 KB Output is correct
40 Correct 437 ms 58244 KB Output is correct
41 Correct 434 ms 58260 KB Output is correct
42 Correct 417 ms 58172 KB Output is correct
43 Correct 495 ms 45716 KB Output is correct
44 Correct 482 ms 45716 KB Output is correct
45 Correct 488 ms 45916 KB Output is correct
46 Correct 472 ms 45592 KB Output is correct
47 Correct 481 ms 45828 KB Output is correct
48 Correct 616 ms 34436 KB Output is correct
49 Correct 621 ms 34324 KB Output is correct
50 Correct 689 ms 36456 KB Output is correct
51 Correct 662 ms 36440 KB Output is correct
52 Correct 649 ms 32960 KB Output is correct
53 Correct 647 ms 32800 KB Output is correct
54 Correct 573 ms 33140 KB Output is correct