Submission #58036

# Submission time Handle Problem Language Result Execution time Memory
58036 2018-07-16T15:58:52 Z Benq Pictionary (COCI18_pictionary) C++14
140 / 140
297 ms 31304 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;

int n,m,q,l[MX],r[MX];
pi p[MX];
vi tri[MX];
vpi ed;
 
template<int SZ> struct DSU {
    int par[SZ], sz[SZ];
    DSU() {
        F0R(i,SZ) par[i] = i, sz[i] = 1;
    }
    
    int get(int x) { // path compression
    	if (par[x] != x) par[x] = get(par[x]);
    	return par[x];
    }
    
    bool unite(int x, int y) { // union-by-rank
    	x = get(x), y = get(y);
    	if (x == y) return 0;
    	if (sz[x] < sz[y]) swap(x,y);
    	sz[x] += sz[y], par[y] = x;
    	return 1;
    }
};

bool left() {
    F0R(i,sz(ed)) tri[i].clear();
    bool ok = 0;
    F0R(i,q) if (l[i] != r[i]) {
        tri[(l[i]+r[i])/2].pb(i);
        ok = 1;
    }
    return ok;
}

void test() {
    DSU<MX> D = DSU<MX>();
    F0R(i,sz(ed)+1) {
        if (i) D.unite(ed[i-1].f,ed[i-1].s);
        for (int x: tri[i]) {
            if (D.get(p[x].f) == D.get(p[x].s)) r[x] = i;
            else l[x] = i+1;
        }
    }
}
 
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m >> q;
    
    DSU<MX> D = DSU<MX>();
    FOR(i,1,m+1) {
        int t = m+1-i;
        for (int j = t; j+t <= n; j += t) if (D.unite(j,j+t)) ed.pb({j,j+t});
    }
    
    F0R(i,q) {
        cin >> p[i].f >> p[i].s;
        l[i] = 0, r[i] = sz(ed);
    }
    
    while (left()) test();
    F0R(i,q) {
        if (l[i] == 0) {
            cout << "0\n";
        } else {
            l[i] --;
            cout << m+1-__gcd(ed[l[i]].f,ed[l[i]].s) << "\n";
        }
    }
}
 
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 5928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 9740 KB Output is correct
2 Correct 66 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 13600 KB Output is correct
2 Correct 63 ms 14056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 14056 KB Output is correct
2 Correct 73 ms 14056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 14868 KB Output is correct
2 Correct 85 ms 16152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 18808 KB Output is correct
2 Correct 113 ms 18808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 22588 KB Output is correct
2 Correct 160 ms 23320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 25688 KB Output is correct
2 Correct 231 ms 27352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 30432 KB Output is correct
2 Correct 297 ms 31304 KB Output is correct