QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#709023 | #8077. Alice and Bob | robinyqc | TL | 0ms | 3620kb | C++20 | 2.9kb | 2024-11-04 10:53:59 | 2024-11-04 10:54:00 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
using i32 = int;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
template<typename T> using vec = vector<T>;
template<int len = (1 << 20)> class myio
{
public:
myio() : p1(ibuf), p2(ibuf) {}
inline char get_char()
{
if (p1 == p2) {
p2 = (p1 = ibuf) + fread(ibuf, 1, len, stdin);
if (p1 == p2) return EOF;
}
return *p1++;
}
template<typename T>
inline void read_int(T &x)
{
x = 0;
while (!is_digit(tc = get_char())) ;
do x = (x << 3) + (x << 1) + (tc ^ 48);
while (is_digit(tc = get_char())) ;
}
myio<len>& operator >> (u64& y) {read_int(y); return (*this);}
myio<len>& operator >> (u32& y) {read_int(y); return (*this);}
private:
char ibuf[len], *p1, *p2;
char tc;
inline bool is_digit(char c)
{
return c >= 48 && c <= 57;
}
} ;
myio<> asdf;
#define cin asdf
void solve()
{
u32 n;
cin >> n;
vec<u64> a(n);
for (u64 &i: a) cin >> i;
sort(a.begin(), a.end());
vec<u64> b(a);
b.erase(unique(b.begin(), b.end()), b.end());
vec<u32> c1(b.size());
vec<u64> c2(b.size());
for (u32 i = 0, p = 0; i < n; i++) {
if (a[i] != b[p]) p++;
c1[p]++;
}
for (u32 i = 0; i < b.size(); i++) {
c2[i] = c1[i] * u64(c1[i] - 1) / 2;
}
a = b;
u64 ans = 0, s = 0, sp = 0;
for (u32 i: c1) {
ans += sp * i;
sp += s * i;
s += i;
}
for (u32 bit = 59; bit; bit--) {
u64 rem = (1ull << bit) - 1;
u32 m = b.size(), p = m;
for (u32 i = 0; i < m; i++) {
if ((b[i] >> bit) & 1) p = min(p, i);
b[i] &= rem;
}
{
vec<u64> d(m);
merge(b.begin(), b.begin() + p, b.begin() + p, b.end(), d.begin());
d.erase(unique(d.begin(), d.end()), d.end());
b.swap(d);
}
u32 q = b.size(), i = 0, j;
vec<u32> d1(q);
vec<u64> d2(q);
p = 0;
for (; i < m && ((~a[i] >> bit) & 1); i++) {
while ((a[i] & rem) != b[p]) p++;
d1[p] += c1[i];
d2[p] += c2[i];
}
if (bit & 1) for (p = 0, j = i; j < m; j++) {
while ((a[j] & rem) != b[p]) p++;
ans += d1[p] * c2[j] + d2[p] * c1[j];
}
for (p = 0, j = i; j < m; j++) {
while ((a[j] & rem) != b[p]) p++;
d1[p] += c1[j];
d2[p] += c2[j];
}
a = b;
d1.swap(c1);
d2.swap(c2);
}
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cout.tie(0);
u32 t;
cin >> t;
while (t--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
3 4 2 0 2 3 3 2 2 3 3 0 2 3
output:
3 0 1
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
input:
1000000 3 63 98 95 3 61 38 97 3 7 73 98 3 1 10 91 3 94 31 99 3 96 54 97 3 14 44 99 3 81 51 97 3 96 95 92 3 35 90 98 3 7 39 96 3 71 8 96 3 36 35 99 3 82 52 96 3 89 53 99 3 76 85 95 3 80 34 91 3 9 13 99 3 12 17 94 3 40 4 95 3 57 5 93 3 47 69 93 3 23 0 94 3 62 44 97 3 7 4 99 3 21 97 99 3 41 3 99 3 36 9...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...