Submission #894658

#TimeUsernameProblemLanguageResultExecution timeMemory
894658lunaTuBubble Sort 2 (JOI18_bubblesort2)C++17
17 / 100
9011 ms3092 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#include "bubblesort2.h"
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int N = 5e4 + 1;
int A[N];
set<int> s;
map<int, ordered_set> d;
map<int, int> ans;

int add(int i, int x) {
    int p = A[i];

    d[p].erase(i);
    ans[p] = 0;
    d[x].insert(i);
    ans[x] = 0;
    s.insert(x);

    if (d[p].empty()) {
        s.erase(p);
    }

    int mx = 0;
    int j1 = *d[x].rbegin(), j2 = *d[p].rbegin();

    // 3 2 1 4
    for (auto e : s) {

        int l = *d[e].begin(), r = *d[e].rbegin();

        if (e < p && r > i) ans[e]--;
        if (e < x && r > i) ans[e]++;

        if (!d[p].empty() && e > p) {
            ans[p] += d[e].order_of_key(j2);
        }
        if (e > x) {
            ans[x] += d[e].order_of_key(j1);
        }
        mx = max(mx, ans[e]);
    }

    A[i] = x;
    mx = max(mx, max(ans[p], ans[x]));
    return mx;
}

vector<int> countScans(vector<int> a, vector<int> x, vector<int> v){
    int n = a.size();
    for (int i = 0; i < n; i++) {
        int w = add(i, a[i]);
    }

	int q = x.size();

	vector<int> answer(q);

	for (int j = 0; j < q; j++) {
		answer[j] = add(x[j], v[j]);
	}

	return answer;
}

Compilation message (stderr)

bubblesort2.cpp: In function 'int add(int, int)':
bubblesort2.cpp:45:13: warning: unused variable 'l' [-Wunused-variable]
   45 |         int l = *d[e].begin(), r = *d[e].rbegin();
      |             ^
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:67:13: warning: unused variable 'w' [-Wunused-variable]
   67 |         int w = add(i, a[i]);
      |             ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...