답안 #79456

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
79456 2018-10-14T01:42:03 Z Benq Pictionary (COCI18_pictionary) C++14
140 / 140
174 ms 20484 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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 4600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 5500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 8804 KB Output is correct
2 Correct 44 ms 9236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 11100 KB Output is correct
2 Correct 70 ms 11432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 11432 KB Output is correct
2 Correct 62 ms 11432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 11432 KB Output is correct
2 Correct 74 ms 12016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 13892 KB Output is correct
2 Correct 80 ms 13892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 16160 KB Output is correct
2 Correct 129 ms 16160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 17068 KB Output is correct
2 Correct 163 ms 17504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 19464 KB Output is correct
2 Correct 174 ms 20484 KB Output is correct