QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#872439 | #8613. Cardinality | ucup-team055# | WA | 1231ms | 482820kb | C++26 | 8.7kb | 2025-01-26 01:37:25 | 2025-01-26 01:37:30 |
Judging History
answer
#pragma GCC target("arch=icelake-server,prefer-vector-width=512")
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
namespace{
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=INFINITY;
const ld EPS=1e-9;
const ld PI=3.14159265358979323846;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload5(a,b,c,d,e,name,...) name
#define overload4(a,b,c,d,name,...) name
#define overload3(a,b,c,name,...) name
#define rep1(n) rep2(_,n)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each4(w,x,y,z,a) for(auto&&[w,x,y,z]:a)
#define each(...) overload5(__VA_ARGS__,each4,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) rbegin(i),rend(i)
#define rall2(i,a) rbegin(i),rbegin(i)+a
#define rall3(i,a,b) rbegin(i)+a,rbegin(i)+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),mint{})
#define elif else if
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector name(h,vector(w,vector<type>(__VA_ARGS__)))
template<class T> ll sz(const T& a){ return size(a); }
template<class T, class U> ll count(const T& a, const U& b){ return count(all(a), b); }
template<class T, class F> ll count_if(const T& a, F b){ return count_if(all(a), b); }
template<class T, class F> void filter(T& a, F b){ a.erase(remove_if(all(a), not_fn(b)), a.end()); }
template<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }
template<class T> void rev(T& a){ reverse(all(a)); }
template<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }
ll popcnt(ull a){ return __builtin_popcountll(a); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template<class T> bool chmin(T& a, const T& b){ return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T& a, const T& b){ return a < b ? a = b, 1 : 0; }
template<class T, class U> bool chmin(T& a, const U& b){ return a > b ? a = b, 1 : 0; }
template<class T, class U> bool chmax(T& a, const U& b){ return a < b ? a = b, 1 : 0; }
vector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ uniq(a); unordered_map<T, ll> ans; rep(i, a.size()) ans[a[i]] = i; return ans; }
template<class T> auto run_press(const T& a){ vector<pair<decay_t<decltype(a[0])>, ll>> ans; each(x, a){ if(ans.empty() || ans.back().first != x) ans.emplace_back(x, 1); else ans.back().second++; } return ans; }
template<class... Ts> void in(Ts&... t);
[[maybe_unused]] void print(){}
template<class T, class... Ts> void print(const T& t, const Ts&... ts);
template<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\n'; }
namespace IO{
#define VOID(a) decltype(void(a))
struct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }
template<class T> void o(const T& t){ o(t, P<4>{}); }
template<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }
template<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }
template<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }
template<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }
template<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }
template<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }
#undef VOID
}
#define unpack(a) (void)(0 + ... + (a, 0))
template<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }
template<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }
#undef unpack
#ifdef DEBUG
ll __lg(ull x){ return 63 - __builtin_clzll(x); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
#define YESNO(yes,no) void yes(bool i = 1){ out(i?#yes:#no); } void no(){ out(#no); }
YESNO(first, second)
YESNO(First, Second)
YESNO(Yes, No)
YESNO(YES, NO)
YESNO(possible, impossible)
YESNO(Possible, Impossible)
YESNO(POSSIBLE, IMPOSSIBLE)
using Bit = bitset<50000>;
struct T;
struct S {
Bit bit;
int t;
};
struct T {
int mn, mx, l, r;
S* s;
};
T sets[550000];
S mem[77500];
mt19937 rnd;
S* alloc(){
static int cnt = 0;
if (cnt < 77500) return mem + cnt++;
S* s = mem + rnd() % 77500;
const int t = s->t;
sets[t].s = nullptr;
return s;
}
T merge(int l, int r){
return {
max(sets[l].mn, sets[r].mn),
sets[l].mx + sets[r].mx,
l, r,
nullptr
};
}
int n;
void recalc1(int t, int depth=7) {
if (t < n) return;
auto& s = sets[t];
if (depth) recalc1(s.l, depth - 1);
if (depth) recalc1(s.r, depth - 1);
s.mn = max(sets[s.l].mn, sets[s.r].mn);
s.mx = min(sets[s.l].mx + sets[s.r].mx, n);
}
Bit recalc(int t){
if (t < n) {
Bit b;
b.set(t);
return b;
}
auto& s = sets[t];
if (s.s) return s.s->bit;
s.s = alloc();
s.s->t = t;
auto& b = s.s->bit;
b = recalc(s.l) | recalc(s.r);
s.mn = s.mx = b.count();
return b;
}
void solve(){
in(n);
LL(q);
rep(i,n){
sets[i]=T{1,1,-1,-1,nullptr};
}
rep(t,q){
LL(l,r);
l--; r--;
auto&ans=sets[n+t]=merge(l,r);
recalc1(n+t);
if(ans.mn*4<ans.mx)recalc(n+t);
out(ans.mn*2);
if(t%50==49)flush(cout);
}
}
}
int main(){
ll t = 1;
// in(t); // マルチテストケースかどうか確認!
rep(t){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 24ms
memory: 477668kb
input:
4 5 1 2 2 3 5 6 6 7 4 7
output:
2 2 2 2 2
result:
ok
Test #2:
score: 0
Accepted
time: 18ms
memory: 477764kb
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 2 2 2 2 2 2 2 2 2 2 10 12 10 10 10 2 2 10 12 2 4 2 2 2 12 4 12 10 12 14 14 6 2 12 14 2 8 14 8 14 6 12 16 12 8 12 12 2 2 2 18 2 4 12 20 14 14 12 18 14 10 14 18 2 14 10 14 14 6 16 12 2 14 12 14 12 12 14 16 12 16 14 6 2 14 12 2 12 18 6 12 10 14 10 20 18 16
result:
ok
Test #3:
score: 0
Accepted
time: 18ms
memory: 477696kb
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 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 10 2 2 2 2 2 2 2 2 2 2 2 2 2 2 10 2 10 2 2 2 2 2 2 2 2 2 10 2 2 2 2 2 12 2 2 2 2 2 2 2
result:
ok
Test #4:
score: 0
Accepted
time: 21ms
memory: 477864kb
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 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 ...
result:
ok
Test #5:
score: 0
Accepted
time: 31ms
memory: 477988kb
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 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 ...
result:
ok
Test #6:
score: 0
Accepted
time: 342ms
memory: 480412kb
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 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 10 2 2 2 2 2 2 2 2 2 2...
result:
ok
Test #7:
score: -100
Wrong Answer
time: 1231ms
memory: 482820kb
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 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 10 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...
result:
wrong answer Too big difference in the 167075-th query: correct 353, got 874, ratio: 247.59%