This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define name "Council"
#define cntbit(x) __builtin_popcountll(x)
#define getbit(x, i) ((x >> i)&1)
#define all(x) x.begin(), x.end()
#define fi(i, a, b) for(int i = a; i <= b; ++i)
#define fid(i, a, b) for(int i = a; i >= b; --i)
#define maxn (int) (1e6 + 7)
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, m, zero, one, two;
int c[maxn], a[maxn];
vector<pair<int, int>> f[1 << 20], cnt[1 << 20];
int rv(int x) { return ((1 << m) - 1) ^ x; }
void inline cb(vector<pair<int, int>> &x, const vector<pair<int, int>> y) {
vector<pair<int, int>> P;
P.insert(P.end(), all(x));
P.insert(P.end(), all(y));
x.clear();
sort(all(P), greater<>());
for(pair<int, int> val : P) if(x.size() < 2) {
if(x.empty() || x.back().second != val.second) x.push_back(val);
}
}
void solve() {
cin >> n >> m;
fi(id, 1, n) {
fi(i, 0, m - 1) {
int x; cin >> x;
a[id] |= (1 << i) * x;
c[i] += x;
}
cb(cnt[rv(a[id])], {{0, id}});
}
fi(i, 0, m - 1) {
if(c[i] == n / 2) zero |= (1 << i);
if(c[i] == n / 2 + 1) one |= (1 << i);
if(c[i] >= n / 2 + 2) two |= (1 << i);
}
fi(i, 0, m - 1) {
fi(x, 0, (1 << m) - 1) if(!getbit(x, i)) {
cb(cnt[x], cnt[x ^ (1 << i)]);
}
}
fi(i, 0, (1 << m) - 1) {
int k = cntbit(i);
if(cnt[i].size() == 1) f[i] = {{ k, cnt[i][0].second }};
else if(cnt[i].size() > 1) {
f[i].push_back({k, cnt[i][0].second});
f[i].push_back({k, cnt[i][1].second});
}
}
fi(i, 0, m - 1) {
fi(x, 0, (1 << m) - 1) if(getbit(x, i)) {
cb(f[x], f[x ^ (1 << i)]);
}
}
fi(i, 1, n) {
int ONE = two | (one & rv(a[i]));
int ZERO = (one & a[i]) | (zero & rv(a[i]));
int ans = cntbit(ONE);
if(f[ZERO][0].second != i) ans += f[ZERO][0].first;
else ans += f[ZERO][1].first;
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
if(fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
solve();
}
Compilation message (stderr)
council.cpp: In function 'int main()':
council.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
council.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |