Skip to content

Commit

Permalink
Add compress coords util
Browse files Browse the repository at this point in the history
  • Loading branch information
adamant-pwn committed Nov 12, 2024
1 parent e184e8c commit 94525b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
23 changes: 23 additions & 0 deletions cp-algo/util/compress_coords.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CP_ALGO_UTIL_COMPRESS_COORDS_HPP
#define CP_ALGO_UTIL_COMPRESS_COORDS_HPP
#include <algorithm>
#include <vector>
namespace cp_algo {
std::vector<int> compress_coords(auto &coords) {
static_assert(std::is_pointer_v<std::ranges::range_value_t<decltype(coords)>>);
std::vector<int> original;
original.reserve(size(coords));
std::ranges::sort(coords, {}, [](int* x) {return *x;});
int idx = -1, prev = -1;
for(auto x: coords) {
if(*x != prev) {
idx++;
prev = *x;
original.push_back(*x);
}
*x = idx;
}
return original;
}
}
#endif // CP_ALGO_UTIL_COMPRESS_COORDS_HPP
24 changes: 5 additions & 19 deletions verify/structures/fenwick/ordered_set.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@
#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"
#pragma GCC optimize("Ofast,unroll-loops")
#include "cp-algo/structures/fenwick_set.hpp"
#include "cp-algo/util/compress_coords.hpp"
#include <bits/stdc++.h>

using namespace std;
using cp_algo::structures::fenwick_set;

vector<int> compress(vector<int*> a) {
vector<int> nums;
ranges::sort(a, {}, [](int* x) {return *x;});
int idx = -1, prev = -1;
for(auto x: a) {
if(*x != prev) {
idx++;
prev = *x;
nums.push_back(*x);
}
*x = idx;
}
return nums;
}

void solve() {
int n, q;
cin >> n >> q;
Expand All @@ -38,7 +24,7 @@ void solve() {
coords.push_back(&x);
}
}
auto nums = compress(coords);
auto values = cp_algo::compress_coords(coords);
const int maxc = 1e6;
fenwick_set<maxc> me(a);
for(auto [t, x]: queries) {
Expand All @@ -48,15 +34,15 @@ void solve() {
me.erase(x);
} else if(t == 2) {
int res = me.find_by_order(x-1);
cout << (res == -1 ? -1 : nums[res]) << '\n';
cout << (res == -1 ? -1 : values[res]) << '\n';
} else if(t == 3) {
cout << me.order_of_key(x+1) << '\n';
} else if(t == 4) {
int res = me.pre_upper_bound(x);
cout << (res == -1 ? -1 : nums[res]) << '\n';
cout << (res == -1 ? -1 : values[res]) << '\n';
} else if(t == 5) {
int res = me.lower_bound(x);
cout << (res == -1 ? -1 : nums[res]) << '\n';
cout << (res == -1 ? -1 : values[res]) << '\n';
}
}
}
Expand Down

0 comments on commit 94525b6

Please sign in to comment.