답안 #925330

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925330 2024-02-11T12:50:25 Z boris_mihov Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
1567 ms 117416 KB
#include "bubblesort2.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;

// template <typename T>
// using OSet = tree <T, null_type, std::less <T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long llong;
const int MAXN = 500000 + 10;
const int MAXNUM = 1e6 + 10;
const int INF  = 1e9;

int n, q;
int cntDiff;
struct SegmentTree
{
    struct Node
    {
        int max;
        int val;
        int lazy;
        bool isActive;
    
        Node()
        {
            max = lazy = isActive = 0;
        }

        friend Node operator + (const Node &left, const Node &right)
        {
            Node res;
            res.max = std::max(left.max, right.max);
            res.isActive = left.isActive | right.isActive;
            return res;
        }
    };

    Node tree[4*MAXNUM];
    void push(int node, int l, int r)
    {
        if (tree[node].lazy == 0)
        {
            return;
        }
        
        if (tree[node].isActive)
        {
            tree[node].max += tree[node].lazy;
        }
    
        if (l == r)
        {
            tree[node].val += tree[node].lazy;
        } else
        {
            tree[2*node].lazy += tree[node].lazy;
            tree[2*node + 1].lazy += tree[node].lazy;
        }

        tree[node].lazy = 0;
    }

    void updateActive(int l, int r, int node, int queryPos, int queryIdx)
    {
        push(node, l, r);
        if (queryPos < l || r < queryPos)
        {
            return;
        }

        if (l == r)
        {
            if (tree[node].isActive)
            {
                tree[node].isActive = 0;
                tree[node].max = 0;
            } else
            {
                tree[node].isActive = true;
                tree[node].max = queryIdx - 1 + tree[node].val;
            }

            return;
        }

        int mid = (l + r) / 2;
        updateActive(l, mid, 2*node, queryPos, queryIdx);
        updateActive(mid + 1, r, 2*node + 1, queryPos, queryIdx);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    void updateRange(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy += queryVal;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        updateRange(l, mid, 2*node, queryL, queryR, queryVal);
        updateRange(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    int query()
    {
        return tree[1].max;
    }

    void add(int idx, int val)
    {
        updateActive(1, cntDiff, 1, val, idx);
        if (val < cntDiff) updateRange(1, cntDiff, 1, val + 1, cntDiff, -1);
    }

    void remove(int idx, int val)
    {
        updateActive(1, cntDiff, 1, val, idx);
        if (val < cntDiff) updateRange(1, cntDiff, 1, val + 1, cntDiff, 1);
    }
};

int in[MAXN];
int pos[MAXN];
int order[MAXN];
int updatePos[MAXN];
llong updateVal[MAXN];
SegmentTree tree;
llong a[MAXN];

std::vector <int> countScans(std::vector <int> A, std::vector <int> X, std::vector <int> V)
{
    n = A.size();
    q = X.size();

    std::vector <std::pair <llong, std::pair <int,bool>>> v;
    for (int i = 1 ; i <= n ; ++i)
    {
        a[i] = 1LL * n * A[i - 1] + i;
        v.push_back({a[i], {i, true}});
    }

    for (int i = 0 ; i < q ; ++i)
    {
        updatePos[i + 1] = X[i] + 1; 
        updateVal[i + 1] = 1LL * n * V[i] + updatePos[i + 1];    
        v.push_back({updateVal[i + 1], {i + 1, false}});
    }

    std::sort(v.begin(), v.end());
    for (int i = 0 ; i < v.size() ; ++i)
    {
        cntDiff += (i == 0 || v[i].first > v[i - 1].first);
        if (v[i].second.second) a[v[i].second.first] = cntDiff;
        else updateVal[v[i].second.first] = cntDiff;
    }
    
    for (int i = 1 ; i <= n ; ++i)
    {
        tree.add(i, a[i]);
    }

    std::vector <int> answer(q, 0);
    for (int i = 1 ; i <= q ; ++i)
    {
        tree.remove(updatePos[i], a[updatePos[i]]);
        a[updatePos[i]] = updateVal[i];
        tree.add(updatePos[i], a[updatePos[i]]);
        answer[i - 1] = tree.query();
    }

    return answer;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:168:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, bool> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |     for (int i = 0 ; i < v.size() ; ++i)
      |                      ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 68184 KB Output is correct
2 Correct 12 ms 68184 KB Output is correct
3 Correct 15 ms 68188 KB Output is correct
4 Correct 14 ms 68188 KB Output is correct
5 Correct 14 ms 68104 KB Output is correct
6 Correct 14 ms 68188 KB Output is correct
7 Correct 14 ms 68184 KB Output is correct
8 Correct 17 ms 68188 KB Output is correct
9 Correct 14 ms 68304 KB Output is correct
10 Correct 14 ms 68188 KB Output is correct
11 Correct 14 ms 68184 KB Output is correct
12 Correct 14 ms 68184 KB Output is correct
13 Correct 14 ms 68188 KB Output is correct
14 Correct 14 ms 68164 KB Output is correct
15 Correct 14 ms 68440 KB Output is correct
16 Correct 14 ms 68356 KB Output is correct
17 Correct 14 ms 68188 KB Output is correct
18 Correct 14 ms 68108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 68184 KB Output is correct
2 Correct 12 ms 68184 KB Output is correct
3 Correct 15 ms 68188 KB Output is correct
4 Correct 14 ms 68188 KB Output is correct
5 Correct 14 ms 68104 KB Output is correct
6 Correct 14 ms 68188 KB Output is correct
7 Correct 14 ms 68184 KB Output is correct
8 Correct 17 ms 68188 KB Output is correct
9 Correct 14 ms 68304 KB Output is correct
10 Correct 14 ms 68188 KB Output is correct
11 Correct 14 ms 68184 KB Output is correct
12 Correct 14 ms 68184 KB Output is correct
13 Correct 14 ms 68188 KB Output is correct
14 Correct 14 ms 68164 KB Output is correct
15 Correct 14 ms 68440 KB Output is correct
16 Correct 14 ms 68356 KB Output is correct
17 Correct 14 ms 68188 KB Output is correct
18 Correct 14 ms 68108 KB Output is correct
19 Correct 24 ms 68700 KB Output is correct
20 Correct 25 ms 68956 KB Output is correct
21 Correct 25 ms 68956 KB Output is correct
22 Correct 25 ms 68952 KB Output is correct
23 Correct 25 ms 68956 KB Output is correct
24 Correct 25 ms 68952 KB Output is correct
25 Correct 26 ms 68952 KB Output is correct
26 Correct 24 ms 68944 KB Output is correct
27 Correct 23 ms 68956 KB Output is correct
28 Correct 23 ms 68908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 71380 KB Output is correct
2 Correct 66 ms 72508 KB Output is correct
3 Correct 114 ms 74192 KB Output is correct
4 Correct 108 ms 74096 KB Output is correct
5 Correct 109 ms 74156 KB Output is correct
6 Correct 114 ms 74188 KB Output is correct
7 Correct 107 ms 74192 KB Output is correct
8 Correct 111 ms 74188 KB Output is correct
9 Correct 108 ms 74188 KB Output is correct
10 Correct 87 ms 74136 KB Output is correct
11 Correct 89 ms 74180 KB Output is correct
12 Correct 93 ms 74184 KB Output is correct
13 Correct 86 ms 74180 KB Output is correct
14 Correct 93 ms 74184 KB Output is correct
15 Correct 88 ms 74192 KB Output is correct
16 Correct 84 ms 74184 KB Output is correct
17 Correct 95 ms 74184 KB Output is correct
18 Correct 93 ms 74184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 68184 KB Output is correct
2 Correct 12 ms 68184 KB Output is correct
3 Correct 15 ms 68188 KB Output is correct
4 Correct 14 ms 68188 KB Output is correct
5 Correct 14 ms 68104 KB Output is correct
6 Correct 14 ms 68188 KB Output is correct
7 Correct 14 ms 68184 KB Output is correct
8 Correct 17 ms 68188 KB Output is correct
9 Correct 14 ms 68304 KB Output is correct
10 Correct 14 ms 68188 KB Output is correct
11 Correct 14 ms 68184 KB Output is correct
12 Correct 14 ms 68184 KB Output is correct
13 Correct 14 ms 68188 KB Output is correct
14 Correct 14 ms 68164 KB Output is correct
15 Correct 14 ms 68440 KB Output is correct
16 Correct 14 ms 68356 KB Output is correct
17 Correct 14 ms 68188 KB Output is correct
18 Correct 14 ms 68108 KB Output is correct
19 Correct 24 ms 68700 KB Output is correct
20 Correct 25 ms 68956 KB Output is correct
21 Correct 25 ms 68956 KB Output is correct
22 Correct 25 ms 68952 KB Output is correct
23 Correct 25 ms 68956 KB Output is correct
24 Correct 25 ms 68952 KB Output is correct
25 Correct 26 ms 68952 KB Output is correct
26 Correct 24 ms 68944 KB Output is correct
27 Correct 23 ms 68956 KB Output is correct
28 Correct 23 ms 68908 KB Output is correct
29 Correct 33 ms 71380 KB Output is correct
30 Correct 66 ms 72508 KB Output is correct
31 Correct 114 ms 74192 KB Output is correct
32 Correct 108 ms 74096 KB Output is correct
33 Correct 109 ms 74156 KB Output is correct
34 Correct 114 ms 74188 KB Output is correct
35 Correct 107 ms 74192 KB Output is correct
36 Correct 111 ms 74188 KB Output is correct
37 Correct 108 ms 74188 KB Output is correct
38 Correct 87 ms 74136 KB Output is correct
39 Correct 89 ms 74180 KB Output is correct
40 Correct 93 ms 74184 KB Output is correct
41 Correct 86 ms 74180 KB Output is correct
42 Correct 93 ms 74184 KB Output is correct
43 Correct 88 ms 74192 KB Output is correct
44 Correct 84 ms 74184 KB Output is correct
45 Correct 95 ms 74184 KB Output is correct
46 Correct 93 ms 74184 KB Output is correct
47 Correct 376 ms 87488 KB Output is correct
48 Correct 1455 ms 114776 KB Output is correct
49 Correct 1499 ms 116900 KB Output is correct
50 Correct 1474 ms 116592 KB Output is correct
51 Correct 1512 ms 116640 KB Output is correct
52 Correct 1555 ms 116520 KB Output is correct
53 Correct 1567 ms 116668 KB Output is correct
54 Correct 1378 ms 117156 KB Output is correct
55 Correct 1503 ms 117416 KB Output is correct
56 Correct 1383 ms 116844 KB Output is correct
57 Correct 1401 ms 117148 KB Output is correct
58 Correct 1412 ms 116908 KB Output is correct
59 Correct 1295 ms 115616 KB Output is correct
60 Correct 1351 ms 116092 KB Output is correct
61 Correct 1357 ms 115708 KB Output is correct
62 Correct 1243 ms 115552 KB Output is correct
63 Correct 1300 ms 115612 KB Output is correct
64 Correct 1250 ms 115488 KB Output is correct
65 Correct 1152 ms 115008 KB Output is correct
66 Correct 1113 ms 115692 KB Output is correct
67 Correct 1075 ms 115208 KB Output is correct