Submission #408920

# Submission time Handle Problem Language Result Execution time Memory
408920 2021-05-19T19:34:33 Z doowey Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
4288 ms 61752 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair

const int N = (int)5e5 + 10;
const int M = N * 2;
const int inf = (int)1e9;

struct Node{
    int maxi;
    int cnt;
    int lzy;
};

vector<pii> cmp;

Node T[M * 4 + 512];

void push(int node, int cl, int cr){
    T[node].maxi -= T[node].lzy;
    T[node].cnt += T[node].lzy;
    if(cl != cr){
        T[node * 2].lzy += T[node].lzy;
        T[node * 2 + 1].lzy += T[node].lzy;
    }
    T[node].lzy = 0;
}

void set_val(int node, int cl, int cr, int id, int v){
    push(node, cl, cr);
    if(cl > id || cr < id) return;
    if(cl == cr){
        T[node].maxi = v;
        return;
    }
    int mid = (cl + cr) / 2;
    set_val(node * 2, cl, mid, id, v);
    set_val(node * 2 + 1, mid + 1, cr, id, v);
    T[node].maxi = max(T[node * 2].maxi, T[node * 2 + 1].maxi);
}

void incr(int node, int cl, int cr, int tl, int tr, int v){
    push(node, cl, cr);
    if(cr < tl || cl > tr) return;
    if(cl >= tl && cr <= tr){
        T[node].lzy = v;
        push(node, cl, cr);
        return;
    }
    int mid = (cl + cr) / 2;
    incr(node * 2, cl, mid, tl, tr, v);
    incr(node * 2 + 1, mid + 1, cr, tl, tr, v);
    T[node].maxi = max(T[node * 2].maxi, T[node * 2 + 1].maxi);
}

int get(int node, int cl, int cr, int tl, int tr){
    push(node, cl, cr);
    if(cr < tl) return 0;
    if(cl > tr) return 0;
    if(cl >= tl && cr <= tr){
        return T[node].cnt;
    }
    int mid = (cl + cr) / 2;
    return get(node * 2, cl, mid, tl, tr) + get(node * 2 + 1, mid + 1, cr, tl, tr);
}

void build(int node, int cl, int cr){
    T[node].maxi = -inf;
    if(cl == cr){
        return;
    }
    int mid = (cl + cr) / 2;
    build(node * 2, cl, mid);
    build(node * 2 + 1, mid + 1, cr);
}

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V){
	vector<int> outp;
    int n = A.size();
    int q = X.size();
    int sol;
    for(int i = 0 ; i < n; i ++ ){
        cmp.push_back(mp(A[i], i));
    }
    for(int iq = 0; iq < q; iq ++ ){
        cmp.push_back(mp(V[iq], X[iq]));
    }
    sort(cmp.begin(), cmp.end());
    cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
    int m = cmp.size();
    build(1, 0, m - 1);
    int idx;
    for(int i = 0 ; i < n; i ++ ){
        idx = lower_bound(cmp.begin(), cmp.end(), mp(A[i], i)) - cmp.begin();
        incr(1, 0, m - 1, idx + 1, m - 1, +1);
    }
    int vv;
    for(int i = 0 ; i < n; i ++ ){
        idx = lower_bound(cmp.begin(), cmp.end(), mp(A[i], i)) - cmp.begin();
        vv = get(1, 0, m - 1, idx, idx);
        set_val(1, 0, m - 1, idx, i - vv);
    }
    for(int iq = 0; iq < q; iq ++ ){
        idx = lower_bound(cmp.begin(), cmp.end(), mp(A[X[iq]], X[iq])) - cmp.begin();
        vv = get(1, 0, m - 1, idx, idx);
        set_val(1, 0, m - 1, idx, -inf);
        incr(1, 0, m - 1, idx + 1, m - 1, -1);

        A[X[iq]] = V[iq];
        idx = lower_bound(cmp.begin(), cmp.end(), mp(A[X[iq]], X[iq])) - cmp.begin();
        vv = get(1, 0, m - 1, idx, idx);
        set_val(1, 0, m - 1, idx, X[iq] - vv);
        incr(1, 0, m - 1, idx + 1, m - 1, +1);
        outp.push_back(T[1].maxi);
    }
    return outp;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:89:9: warning: unused variable 'sol' [-Wunused-variable]
   89 |     int sol;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 8 ms 460 KB Output is correct
4 Correct 8 ms 460 KB Output is correct
5 Correct 8 ms 460 KB Output is correct
6 Correct 8 ms 512 KB Output is correct
7 Correct 8 ms 448 KB Output is correct
8 Correct 8 ms 504 KB Output is correct
9 Correct 8 ms 504 KB Output is correct
10 Correct 7 ms 460 KB Output is correct
11 Correct 8 ms 460 KB Output is correct
12 Correct 8 ms 460 KB Output is correct
13 Correct 7 ms 460 KB Output is correct
14 Correct 7 ms 460 KB Output is correct
15 Correct 7 ms 460 KB Output is correct
16 Correct 9 ms 504 KB Output is correct
17 Correct 7 ms 508 KB Output is correct
18 Correct 9 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 8 ms 460 KB Output is correct
4 Correct 8 ms 460 KB Output is correct
5 Correct 8 ms 460 KB Output is correct
6 Correct 8 ms 512 KB Output is correct
7 Correct 8 ms 448 KB Output is correct
8 Correct 8 ms 504 KB Output is correct
9 Correct 8 ms 504 KB Output is correct
10 Correct 7 ms 460 KB Output is correct
11 Correct 8 ms 460 KB Output is correct
12 Correct 8 ms 460 KB Output is correct
13 Correct 7 ms 460 KB Output is correct
14 Correct 7 ms 460 KB Output is correct
15 Correct 7 ms 460 KB Output is correct
16 Correct 9 ms 504 KB Output is correct
17 Correct 7 ms 508 KB Output is correct
18 Correct 9 ms 460 KB Output is correct
19 Correct 32 ms 1100 KB Output is correct
20 Correct 34 ms 1140 KB Output is correct
21 Correct 32 ms 1136 KB Output is correct
22 Correct 33 ms 1100 KB Output is correct
23 Correct 32 ms 1100 KB Output is correct
24 Correct 32 ms 1132 KB Output is correct
25 Correct 31 ms 1144 KB Output is correct
26 Correct 32 ms 1136 KB Output is correct
27 Correct 31 ms 1140 KB Output is correct
28 Correct 31 ms 1140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 1688 KB Output is correct
2 Correct 142 ms 3156 KB Output is correct
3 Correct 257 ms 6420 KB Output is correct
4 Correct 254 ms 6328 KB Output is correct
5 Correct 248 ms 6280 KB Output is correct
6 Correct 262 ms 6300 KB Output is correct
7 Correct 266 ms 6272 KB Output is correct
8 Correct 282 ms 6328 KB Output is correct
9 Correct 249 ms 6492 KB Output is correct
10 Correct 209 ms 4792 KB Output is correct
11 Correct 240 ms 4844 KB Output is correct
12 Correct 243 ms 4840 KB Output is correct
13 Correct 258 ms 4900 KB Output is correct
14 Correct 226 ms 4788 KB Output is correct
15 Correct 227 ms 4780 KB Output is correct
16 Correct 206 ms 4804 KB Output is correct
17 Correct 209 ms 4792 KB Output is correct
18 Correct 206 ms 4780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 8 ms 460 KB Output is correct
4 Correct 8 ms 460 KB Output is correct
5 Correct 8 ms 460 KB Output is correct
6 Correct 8 ms 512 KB Output is correct
7 Correct 8 ms 448 KB Output is correct
8 Correct 8 ms 504 KB Output is correct
9 Correct 8 ms 504 KB Output is correct
10 Correct 7 ms 460 KB Output is correct
11 Correct 8 ms 460 KB Output is correct
12 Correct 8 ms 460 KB Output is correct
13 Correct 7 ms 460 KB Output is correct
14 Correct 7 ms 460 KB Output is correct
15 Correct 7 ms 460 KB Output is correct
16 Correct 9 ms 504 KB Output is correct
17 Correct 7 ms 508 KB Output is correct
18 Correct 9 ms 460 KB Output is correct
19 Correct 32 ms 1100 KB Output is correct
20 Correct 34 ms 1140 KB Output is correct
21 Correct 32 ms 1136 KB Output is correct
22 Correct 33 ms 1100 KB Output is correct
23 Correct 32 ms 1100 KB Output is correct
24 Correct 32 ms 1132 KB Output is correct
25 Correct 31 ms 1144 KB Output is correct
26 Correct 32 ms 1136 KB Output is correct
27 Correct 31 ms 1140 KB Output is correct
28 Correct 31 ms 1140 KB Output is correct
29 Correct 48 ms 1688 KB Output is correct
30 Correct 142 ms 3156 KB Output is correct
31 Correct 257 ms 6420 KB Output is correct
32 Correct 254 ms 6328 KB Output is correct
33 Correct 248 ms 6280 KB Output is correct
34 Correct 262 ms 6300 KB Output is correct
35 Correct 266 ms 6272 KB Output is correct
36 Correct 282 ms 6328 KB Output is correct
37 Correct 249 ms 6492 KB Output is correct
38 Correct 209 ms 4792 KB Output is correct
39 Correct 240 ms 4844 KB Output is correct
40 Correct 243 ms 4840 KB Output is correct
41 Correct 258 ms 4900 KB Output is correct
42 Correct 226 ms 4788 KB Output is correct
43 Correct 227 ms 4780 KB Output is correct
44 Correct 206 ms 4804 KB Output is correct
45 Correct 209 ms 4792 KB Output is correct
46 Correct 206 ms 4780 KB Output is correct
47 Correct 1031 ms 23612 KB Output is correct
48 Correct 4104 ms 58772 KB Output is correct
49 Correct 4288 ms 61368 KB Output is correct
50 Correct 4229 ms 61588 KB Output is correct
51 Correct 4276 ms 61636 KB Output is correct
52 Correct 4208 ms 61344 KB Output is correct
53 Correct 4222 ms 61520 KB Output is correct
54 Correct 3755 ms 61632 KB Output is correct
55 Correct 4062 ms 61584 KB Output is correct
56 Correct 3649 ms 61612 KB Output is correct
57 Correct 3875 ms 61632 KB Output is correct
58 Correct 3685 ms 61752 KB Output is correct
59 Correct 3433 ms 60540 KB Output is correct
60 Correct 3464 ms 60332 KB Output is correct
61 Correct 3410 ms 60208 KB Output is correct
62 Correct 3301 ms 60040 KB Output is correct
63 Correct 3308 ms 60064 KB Output is correct
64 Correct 3328 ms 60056 KB Output is correct
65 Correct 3205 ms 59936 KB Output is correct
66 Correct 3438 ms 59908 KB Output is correct
67 Correct 3379 ms 59904 KB Output is correct