Submission #538555

# Submission time Handle Problem Language Result Execution time Memory
538555 2022-03-17T05:45:44 Z Killer2501 Pictionary (COCI18_pictionary) C++14
140 / 140
222 ms 22788 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 5e3+5;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9+7;
int t, a[N], b[N], ans, lab[N], q;
int n, m, k, l[N], r[N];
vector<int> adj[N], g[N];
string s;
ll pw(ll k, ll n)
{
    ll total =1 ;
    for(; n; n >>= 1)
    {
        if(n&1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
struct IT
{
    int n;
    vector<int> st;
    IT(){}
    void init(int _n)
    {
        n = _n;
        st.assign(n*4+4, 0);
    }
    void build(int id, int l, int r, int pos, int x)
    {
        if(l == r)
        {
            st[id] = x;
            return;
        }
        int mid = (l+r)>>1;
        if(pos <= mid)build(id<<1, l, mid, pos, x);
        else build(id<<1|1, mid+1, r, pos, x);
        st[id] = (st[id<<1|1] + st[id<<1]) % mod;
    }
    void build(int pos, int x)
    {
        build(1, 1, n, pos, x);
    }
    int get(int id, int l, int r, int u, int v)
    {
        if(u <= l && r <= v)return st[id];
        if(u > r || l > v)return 0;
        int mid = (l+r)>>1;
        return (get(id<<1, l, mid, u, v) + get(id<<1|1, mid+1, r, u ,v)) % mod;
    }
    int get(int l, int r)
    {
        return get(1, 1, n, l, r);
    }
}it;
int findp(int u)
{
    return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
bool hop(int u, int v)
{
    u = findp(u);
    v = findp(v);
    if(u == v)return false;
    if(lab[u] > lab[v])swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
    return true;
}
void sol()
{
    cin >> n >> m >> q;
    for(int i = 1; i <= q; i ++)
    {
        cin >> a[i] >> b[i];
        l[i] = 1;
        r[i] = m;
    }
    bool ok = true;
    while(ok)
    {
        ok = false;
        for(int i = 1; i <= q; i ++)
        {
            if(l[i] <= r[i])
            {
                ok = true;
                adj[(l[i]+r[i])>>1].pb(i);
            }
        }
        fill_n(lab, n+1, -1);
        for(int i = m; i > 0; i --)
        {
            for(int j = i*2; j <= n; j += i)hop(i, j);
            for(int j: adj[i])
            {
                if(findp(a[j]) == findp(b[j]))l[j] = i+1;
                else r[j] = i-1;
            }
            adj[i].clear();
        }
    }
    for(int i = 1; i <= q; i ++)cout << m-r[i]+1 << '\n';
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}


Compilation message

pictionary.cpp: In function 'int main()':
pictionary.cpp:125:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:126:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 10068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 10900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 14360 KB Output is correct
2 Correct 30 ms 13564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 16356 KB Output is correct
2 Correct 41 ms 15284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 14544 KB Output is correct
2 Correct 57 ms 14212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 15180 KB Output is correct
2 Correct 53 ms 14132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 17500 KB Output is correct
2 Correct 93 ms 14976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 15800 KB Output is correct
2 Correct 147 ms 18944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 20700 KB Output is correct
2 Correct 170 ms 19532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 22788 KB Output is correct
2 Correct 190 ms 21096 KB Output is correct