#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double wow = (double) ((ull) rng()) / ((ull)(0-1));
    return wow * (r - l) + l;
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
const int N = 100;
vector<pair<ll, ll>> groups[N];
ll get_l(pair<ll, ll> x, int bit, ll target){
    ll ans = 0;
    bool lockin = false;
    for(int i = bit; i >= 0; --i){
        if (GETBIT(target, i)){
            if (lockin) ans = ans * 2;
            else{
                ans = ans * 2 + GETBIT(x.first, i);
                if (GETBIT(x.first, i) != GETBIT(x.second, i))
                    x.second = MASK(bit+1) - 1;
            }
        }
        else{
            if (GETBIT(x.first, i) != GETBIT(x.second, i)){
                lockin = true;
            }
        }
    }
    return ans;
}
ll get_r(pair<ll, ll> x, int bit, ll target){
    ll ans = 0;
    bool lockin = false;
    for(int i = bit; i >= 0; --i){
        if (GETBIT(target, i)){
            if (lockin) ans = ans * 2 + 1;
            else{
                ans = ans * 2 + GETBIT(x.second, i);
                if (GETBIT(x.first, i) != GETBIT(x.second, i))
                    x.first = 0;
            }
        }
        else{
            if (GETBIT(x.first, i) != GETBIT(x.second, i)){
                lockin = true;
            }
        }
    }
    return ans;
}
pair<ll, ll> transform(pair<ll, ll> x, int bit, ll target){ // converting x.first and x.second to what would be the max when it comes to target
    ll fi = get_l(x, bit, target);
    ll se = get_r(x, bit, target);
    return make_pair(fi, se);
}
int check(vector<pair<ll, ll>> a, int bit, ll target){
    for(auto &i: a){
        i.first >>= bit;
        i.second >>= bit;
        i = transform(i, 31 - bit, target);
    }
    bit = pop_cnt(target);
    // cout << "Check: " << target << "\n";
    for(int i = bit-1; i >= 0; --i){
        int cnt = 0, oo = 0, idx = -1;
        for(int it = 0; it < (int) a.size(); ++it) {
            pair<ll, ll> j = a[it];
            if (GETBIT(j.first, i) != GETBIT(j.second, i)){
                cnt++;
                idx = it;
            }
            else oo |= GETBIT(j.second, i);
        }
        // cout << i << " " << cnt << " " << oo << " " << idx << "\n";
        if (cnt >= 2) return true;
        if (cnt == 0 && oo == 0) return false; 
        if (cnt == 1){
            if (oo == 1){
                return true;
            }
            else{
                a[idx].first = 0;
            }
        }
    }
    return true;
}
void solve(){
    int n, m; cin >> n >> m;
    vector<int> sz(m);
    for(int i = 0; i < m; ++i){
        cin >> sz[i];
    }
    for(int i = 0; i < m; ++i){
        while(sz[i]--){
            ll x, y; cin >> x >> y;
            groups[i].push_back(make_pair(x, y));
        }
    }
    ll ans = 0;
    for(int bit = 31; bit >= 0; --bit){
        ans = ans * 2 + 1;
        for(int i = 0; i < m; ++i){
            if (!check(groups[i], bit, ans)) {
                ans--;
                break;
            }
        }
    }
    cout << ans << "\n";
}
int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    clock_t cock = clock();
    solve();
    cerr << "Time elapsed: " << clock() - cock << "ms!\n";
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |