Submission #824166

# Submission time Handle Problem Language Result Execution time Memory
824166 2023-08-13T16:23:48 Z GrindMachine Land of the Rainbow Gold (APIO17_rainbow) C++17
35 / 100
3000 ms 18840 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "rainbow.h"

struct DSU {
    vector<int> par, rankk, siz;

    DSU() {

    }

    DSU(int n) {
        init(n);
    }

    void init(int n) {
        par = vector<int>(n + 1);
        rankk = vector<int>(n + 1);
        siz = vector<int>(n + 1);
        rep(i, n + 1) create(i);
    }

    void create(int u) {
        par[u] = u;
        rankk[u] = 0;
        siz[u] = 1;
    }

    int find(int u) {
        if (u == par[u]) return u;
        else return par[u] = find(par[u]);
    }

    bool same(int u, int v) {
        return find(u) == find(v);
    }

    void merge(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (rankk[u] == rankk[v]) rankk[u]++;
        if (rankk[u] < rankk[v]) swap(u, v);

        par[v] = u;
        siz[u] += siz[v];
    }
};

vector<int> here[N];
vector<pii> row_segs[N];
int n,m;

void init(int n_, int m_, int sr, int sc, int k, char *S) {
    n = n_;
    m = m_;
    here[sr].pb(sc);
    rep(i,k){
        char ch = S[i];
        if(ch == 'E'){
            sc++;
        }
        else if(ch == 'W'){
            sc--;
        }
        else if(ch == 'N'){
            sr--;
        }
        else{
            sr++;
        }
        here[sr].pb(sc);
    }    

    rep1(i,n){
        auto &v = here[i];
        sort(all(v));
        v.resize(unique(all(v))-v.begin());
    }
}

int colour(int ar, int ac, int br, int bc) {
    vector<array<int,3>> prev_segs;
    vector<pii> edges;
    int ptr = 1;

    for(int i = ar; i <= br; ++i){
        vector<array<int,3>> curr_segs;
        int optr = ptr;

        auto add = [&](int l, int r){
            if(l <= r){
                curr_segs.pb({l,r,ptr++});
            }
        };

        auto it = lower_bound(all(here[i]),ac);
        if(it == here[i].end() or *it > bc){
            add(ac,bc);
        }
        else{
            add(ac,*it-1);
            int pos = it-here[i].begin();
            int siz = sz(here[i]);

            while(true){
                if(here[i][pos] > bc) break;
                if(pos+1 == siz){
                    add(here[i][pos]+1,bc);
                    break;
                }
                else{
                    add(here[i][pos]+1,min(here[i][pos+1]-1,bc));
                    pos++;
                }
            }
        }

        // ptr = optr;
        // vector<array<int,3>> a_curr_segs;
        // int l = -1;
        // auto &vec = here[i];

        // for(int j = ac; j <= bc; ++j){
        //     if(binary_search(all(vec),j)){
        //         if(l != -1){
        //             a_curr_segs.pb({l,j-1,ptr++});
        //         }
        //         l = -1;
        //     }
        //     else{
        //         if(l == -1){
        //             l = j;
        //         }
        //     }
        // }

        // if(l != -1){
        //     a_curr_segs.pb({l,bc,ptr++});
        // }

        // assert(curr_segs == a_curr_segs);
        
        // debug(i);
        // trav(ar,curr_segs){
        //     debug(ar);
        // }
        // cout << endl;

        for(auto [l1,r1,u] : prev_segs){
            for(auto [l2,r2,v] : curr_segs){
                if(max(l1,l2) <= min(r1,r2)){
                    edges.pb({u,v});
                }
            }
        }

        prev_segs = curr_segs;
    }

    ptr--;

    // debug(ptr);
    // debug(edges);

    DSU dsu(ptr+5);
    for(auto [u,v] : edges){
        dsu.merge(u,v);
    }

    int ans = 0;

    rep1(i,ptr){
        if(dsu.find(i) == i){
            ans++;
        }
    }

    return ans;
}

Compilation message

rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:148:13: warning: unused variable 'optr' [-Wunused-variable]
  148 |         int optr = ptr;
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 8 ms 9812 KB Output is correct
3 Correct 7 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 9 ms 9804 KB Output is correct
6 Correct 4 ms 9660 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 9 ms 9684 KB Output is correct
12 Correct 8 ms 9684 KB Output is correct
13 Correct 13 ms 9748 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 6 ms 9576 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 1804 ms 10968 KB Output is correct
4 Execution timed out 3075 ms 11480 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9576 KB Output is correct
2 Correct 12 ms 12108 KB Output is correct
3 Correct 14 ms 14804 KB Output is correct
4 Correct 17 ms 13260 KB Output is correct
5 Correct 15 ms 12872 KB Output is correct
6 Correct 15 ms 13100 KB Output is correct
7 Correct 16 ms 13480 KB Output is correct
8 Correct 8 ms 10448 KB Output is correct
9 Correct 7 ms 10324 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 11 ms 10452 KB Output is correct
12 Correct 8 ms 10448 KB Output is correct
13 Correct 10 ms 13140 KB Output is correct
14 Correct 10 ms 11604 KB Output is correct
15 Correct 9 ms 11728 KB Output is correct
16 Correct 17 ms 12740 KB Output is correct
17 Correct 12 ms 12016 KB Output is correct
18 Correct 12 ms 12364 KB Output is correct
19 Correct 14 ms 13896 KB Output is correct
20 Correct 12 ms 13056 KB Output is correct
21 Correct 22 ms 10468 KB Output is correct
22 Correct 43 ms 10784 KB Output is correct
23 Correct 15 ms 12176 KB Output is correct
24 Correct 20 ms 12932 KB Output is correct
25 Correct 16 ms 14116 KB Output is correct
26 Correct 24 ms 18840 KB Output is correct
27 Correct 26 ms 16156 KB Output is correct
28 Correct 21 ms 16352 KB Output is correct
29 Correct 20 ms 14264 KB Output is correct
30 Correct 18 ms 14412 KB Output is correct
31 Correct 25 ms 15328 KB Output is correct
32 Correct 18 ms 15436 KB Output is correct
33 Correct 19 ms 15368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 8 ms 9812 KB Output is correct
3 Correct 7 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 9 ms 9804 KB Output is correct
6 Correct 4 ms 9660 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 9 ms 9684 KB Output is correct
12 Correct 8 ms 9684 KB Output is correct
13 Correct 13 ms 9748 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 6 ms 9576 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Execution timed out 3085 ms 11648 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 8 ms 9812 KB Output is correct
3 Correct 7 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 9 ms 9804 KB Output is correct
6 Correct 4 ms 9660 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 9 ms 9684 KB Output is correct
12 Correct 8 ms 9684 KB Output is correct
13 Correct 13 ms 9748 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 6 ms 9576 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Execution timed out 3085 ms 11648 KB Time limit exceeded
19 Halted 0 ms 0 KB -