QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#872640 | #8613. Cardinality | ucup-team008# | RE | 4071ms | 397660kb | C++17 | 4.8kb | 2025-01-26 03:06:10 | 2025-01-26 03:06:16 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef int64_t ll;
uint64_t fhash(uint64_t x) {
x = ((x >> 33) ^ x) * 0xff51afd7ed558ccd;
x = ((x >> 33) ^ x) * 0xc4ceb9fe1a85ec53;
x = (x >> 33) ^ x;
return x;
}
class HyperLogLogPlusPlus {
private:
size_t precision; // Number of bits used for buckets (registers)
size_t numRegisters;
std::vector<uint8_t> registers;
double alphaMM;
double computeAlphaMM(size_t m) {
if (m == 16) return 0.673 * m * m;
if (m == 32) return 0.697 * m * m;
if (m == 64) return 0.709 * m * m;
return (0.7213 / (1 + 1.079 / m)) * m * m;
}
uint8_t countLeadingZeros(uint64_t x) {
return x ? __builtin_clzll(x) + 1 : 64;
}
double biasCorrection(double estimate) {
return estimate;
}
public:
explicit HyperLogLogPlusPlus(size_t precision = 9) : precision(precision), numRegisters(1 << precision) {
registers.resize(numRegisters, 0);
alphaMM = computeAlphaMM(numRegisters);
}
void add(uint64_t value) {
uint64_t hashValue = fhash(value);
size_t index = (hashValue >> (64 - precision)) % numRegisters;
uint64_t remainingBits = hashValue << precision;
uint8_t leadingZeros = countLeadingZeros(remainingBits) + 1;
registers[index] = std::max(registers[index], leadingZeros);
}
void merge(const HyperLogLogPlusPlus& other) {
for (size_t i = 0; i < numRegisters; ++i) {
registers[i] = std::max(registers[i], other.registers[i]);
}
}
double estimate() {
double sum = 0.0;
size_t zeroCount = 0;
for (uint8_t reg : registers) {
sum += pow(2, -double(reg));
if (reg == 0) zeroCount++;
}
double rawEstimate = alphaMM / sum;
if (rawEstimate <= 2.5 * numRegisters && zeroCount > 0) {
rawEstimate = numRegisters * log(static_cast<double>(numRegisters) / zeroCount);
}
return biasCorrection(rawEstimate);
}
};
HyperLogLogPlusPlus hllpp[700000];
void solve() {
int n, q;
cin >> n >> q;
for(int i = 0; i < n; i++) hllpp[i].add(i+1);
for(int qidx = 0; qidx < q; qidx++) {
int a, b;
cin >> a >> b;
a--; b--;
hllpp[n+qidx].merge(hllpp[a]);
hllpp[n+qidx].merge(hllpp[b]);
cout << int(hllpp[n+qidx].estimate()) << "\n";
if(qidx%50 == 49 || qidx == q-1) cout << flush;
}
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 77ms
memory: 397660kb
input:
4 5 1 2 2 3 5 6 6 7 4 7
output:
2 2 3 3 4
result:
ok
Test #2:
score: 0
Accepted
time: 74ms
memory: 397480kb
input:
10 100 9 2 9 10 5 1 6 6 13 14 3 4 3 8 8 4 16 5 14 2 8 13 14 9 6 17 15 11 24 7 24 20 1 26 14 27 6 18 14 14 15 11 14 25 8 11 7 30 3 11 12 3 6 19 29 36 30 9 38 6 2 28 12 40 33 25 20 42 17 30 23 1 34 41 41 36 7 18 39 45 32 4 30 21 46 26 12 39 42 42 46 48 31 54 16 37 42 4 27 34 10 35 11 12 1 35 51 23 17 ...
output:
2 2 2 1 3 2 2 2 3 2 3 2 3 5 6 5 5 5 3 1 5 6 3 2 3 3 4 6 2 6 5 6 7 7 3 4 6 7 3 4 7 4 7 3 6 8 6 4 6 6 4 3 4 9 3 2 6 10 7 7 6 9 7 5 7 9 3 7 5 7 7 3 8 6 2 7 6 7 6 6 7 8 6 8 7 3 2 7 6 4 6 9 3 6 5 7 5 10 9 8
result:
ok
Test #3:
score: 0
Accepted
time: 66ms
memory: 397624kb
input:
100 100 82 51 68 54 25 11 21 47 84 43 78 91 1 88 29 50 10 62 38 29 100 65 23 4 77 10 29 7 59 39 56 81 73 3 113 10 49 25 59 103 20 40 42 55 46 87 9 26 30 43 70 97 7 12 2 54 41 68 82 60 129 69 86 82 85 38 105 71 81 58 59 36 76 111 10 68 108 19 46 31 127 60 35 120 79 125 138 21 14 10 64 72 140 127 126 ...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 3 2 3 2 3 4 3 3 2 2 4 3 3 3 2 3 4 2 2 3 2 4 3 5 4 2 4 4 2 4 3 3 4 2 2 3 2 3 5 2 5 3 3 3 3 4 3 2 2 3 5 3 3 3 2 2 5 4 2 4 4 3 2 4
result:
ok
Test #4:
score: 0
Accepted
time: 74ms
memory: 397300kb
input:
1000 1000 89 983 726 406 473 684 779 306 5 585 185 774 484 220 988 291 857 606 783 143 238 193 187 68 342 227 833 183 645 453 714 271 717 845 811 608 601 1013 101 716 563 790 500 449 962 863 255 787 236 837 560 412 788 681 487 992 311 884 389 251 199 927 942 1013 760 829 794 763 323 37 380 773 520 9...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 3 2 2 2 2 2 2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 2 2 2 2 2 2 4 3 2 2 4 2 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 3 3 2 2 3 ...
result:
ok
Test #5:
score: 0
Accepted
time: 124ms
memory: 397448kb
input:
1000 10000 609 422 750 225 479 328 513 581 935 302 164 982 913 807 716 785 888 102 867 698 397 957 743 229 35 252 222 697 614 421 442 266 748 44 698 740 556 746 748 637 259 372 752 867 503 605 483 380 586 608 977 584 603 335 347 202 514 622 343 167 700 845 370 673 597 499 314 38 647 976 784 644 721 ...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 3 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 3 2 2 3 3 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 4 2 ...
result:
ok
Test #6:
score: 0
Accepted
time: 688ms
memory: 397560kb
input:
1000 100000 359 877 601 2 857 749 72 386 503 918 74 209 504 14 653 714 168 192 993 870 342 822 540 854 495 452 996 651 1005 932 898 279 105 83 778 924 517 326 326 16 747 863 73 501 190 386 211 416 330 72 857 269 543 485 344 637 111 611 449 942 426 739 585 459 100 269 1025 249 763 945 834 432 492 148...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 2 2 2 3 3 2 2 2 2 2 2 3 3 2 2 2 2 4 2 2 2 2 3 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 3 5 2 2 2 3 2 2 2 4 2 2 ...
result:
ok
Test #7:
score: 0
Accepted
time: 1466ms
memory: 397496kb
input:
1000 200000 769 45 350 115 462 826 361 748 422 502 757 529 131 165 525 252 5 610 58 557 894 966 867 661 699 337 508 118 248 715 307 307 814 382 957 825 302 694 22 761 146 621 149 929 553 337 428 844 429 371 355 740 889 726 1017 938 31 269 906 259 173 45 852 442 41 87 179 284 866 1033 115 130 757 192...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 5 3 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 4 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 2 3 2 3 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 3 2 3 4 2 2 2 2 2 2 2 3 ...
result:
ok
Test #8:
score: 0
Accepted
time: 4071ms
memory: 397452kb
input:
1000 500000 24 935 982 976 490 112 293 703 499 131 922 786 572 620 322 364 508 616 333 817 664 297 581 82 257 726 95 310 119 28 208 523 86 518 866 919 777 618 314 979 640 663 377 898 713 187 64 78 725 243 883 113 868 514 546 816 945 529 749 724 300 243 282 41 625 398 376 572 63 420 91 995 715 757 12...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 4 3 2 2 2 2 3 4 2 3 2 2 2 2 2 3 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 3 2 2 2 2 2 3 2 3 2 5 2 2 3 2 3 2 2 2 3 2 2 3 2 2 2 2 2 2 2 ...
result:
ok
Test #9:
score: -100
Runtime Error
input:
10000 200000 7688 6283 9094 4308 3710 4803 6747 3889 4207 6061 2726 4542 8829 796 5675 7143 5638 863 245 5734 8825 5750 3987 8324 4975 5890 1970 7899 6849 4477 8086 4128 6329 4663 2284 2090 4982 2135 6163 5095 9030 9535 108 3964 1061 123 9528 6368 5864 6484 4099 7490 1920 9370 7465 708 3243 6206 630...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...