Submission #1086353

# Submission time Handle Problem Language Result Execution time Memory
1086353 2024-09-10T10:06:09 Z steveonalex Tropical Garden (IOI11_garden) C++17
100 / 100
90 ms 42836 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.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()
#define block_of_code if(true)
 
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(ll 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 cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - 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 = 150069;

int n, m, p, q;
vector<pair<int,int>> graph[N];
int mi[N];
int nxt[N * 2];

int visited[N * 2];
int cycle[N * 2];

void reset(){
    for(int i = 0; i<n; ++i){
        graph[i].clear();
    }
}

void dfs(int u){
    if (visited[u] == 2) return;
    if (visited[u] == 1){
        int v = u;
        while(!cycle[v]){
            cycle[v] = 1;
            v = nxt[v];
        }
        return;
    }
    visited[u] = 1;
    dfs(nxt[u]);
    visited[u] = 2;
}

vector<int> function_graph[N * 2];
vector<int> answer_queries(int p, int G[]){
    vector<int> ans(q);
    for(int i =0; i < n * 2; ++i){
        visited[i] = cycle[i] = 0;
        function_graph[i].clear();
    }
    dfs(p);

    for(int i = 0; i<n*2; ++i) if (i != p) function_graph[nxt[i]].push_back(i);

    if (cycle[p]){
        int cycle_sz = 0;
        for(int i = 0; i<n*2; ++i) cycle_sz += cycle[i];

        vector<vector<int>> sigma(cycle_sz);

        deque<pair<int, int>> q;
        q.push_back({p, 0});
        while(q.size()){
            pair<int,int> u = q.front(); q.pop_front();
            if (u.first % 2 == 0)  {
                sigma[u.second % cycle_sz].push_back(u.second);
            }
            for(int v: function_graph[u.first])
                q.push_back({v, u.second + 1});
        }

        for(int i = 0; i<cycle_sz; ++i) sort(ALL(sigma[i]));

        for(int i = 0; i < ans.size(); ++i) {
            int r = G[i] % cycle_sz;
            ans[i] = upper_bound(ALL(sigma[r]), G[i]) - sigma[r].begin();
        }

    }
    else{
        map<int, int> mp;
        deque<pair<int, int>> q;
        q.push_back({p, 0});
        while(q.size()){
            pair<int,int> u = q.front(); q.pop_front();
            if (u.first % 2 == 0)  mp[u.second]++;
            for(int v: function_graph[u.first])
                q.push_back({v, u.second + 1});
        }

        for(int i = 0; i < ans.size(); ++i) {
            ans[i] = mp[G[i]];
        }
    }

    return ans;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    n = N, m = M, p = P, q = Q;
    reset();
    for(int i = 0; i<m; ++i){
        int u = R[i][0], v = R[i][1];
        graph[u].push_back({v, i});
        graph[v].push_back({u, i});
    }

    for(int i = 0; i<n; ++i){
        sort(ALL(graph[i]), [](pair<int,int> x, pair<int,int> y){return x.second < y.second;});
        mi[i] = graph[i][0].second;
    }

    for(int i = 0; i<n; ++i){
        for(int j = 0; j <= 1; ++j){
            int cur, color;
            tie(cur, color) = graph[i][min(j, graph[i].size() - 1)];
            if (color == mi[cur]) cur = cur * 2 + 1;
            else cur = cur * 2;

            nxt[i * 2 + j] = cur;
        }
    }

    // answering queries to check if vertices u will end up on p * 2
    vector<int> ans1 = answer_queries(p * 2, G);

    // answering queries to check if vertices u will end up on p * 2 + 1
    vector<int> ans2 = answer_queries(p * 2 + 1, G);

    for(int i = 0; i<q; ++i) answer(ans1[i] + ans2[i]);
}


Compilation message

garden.cpp: In function 'std::vector<int> answer_queries(int, int*)':
garden.cpp:118:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for(int i = 0; i < ans.size(); ++i) {
      |                        ~~^~~~~~~~~~~~
garden.cpp:135:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |         for(int i = 0; i < ans.size(); ++i) {
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11100 KB Output is correct
2 Correct 5 ms 11100 KB Output is correct
3 Correct 5 ms 11100 KB Output is correct
4 Correct 4 ms 10844 KB Output is correct
5 Correct 4 ms 10844 KB Output is correct
6 Correct 5 ms 11100 KB Output is correct
7 Correct 4 ms 10844 KB Output is correct
8 Correct 4 ms 11100 KB Output is correct
9 Correct 6 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11100 KB Output is correct
2 Correct 5 ms 11100 KB Output is correct
3 Correct 5 ms 11100 KB Output is correct
4 Correct 4 ms 10844 KB Output is correct
5 Correct 4 ms 10844 KB Output is correct
6 Correct 5 ms 11100 KB Output is correct
7 Correct 4 ms 10844 KB Output is correct
8 Correct 4 ms 11100 KB Output is correct
9 Correct 6 ms 11356 KB Output is correct
10 Correct 5 ms 10844 KB Output is correct
11 Correct 11 ms 13704 KB Output is correct
12 Correct 22 ms 15960 KB Output is correct
13 Correct 49 ms 35000 KB Output is correct
14 Correct 65 ms 27220 KB Output is correct
15 Correct 72 ms 28244 KB Output is correct
16 Correct 58 ms 24460 KB Output is correct
17 Correct 54 ms 23120 KB Output is correct
18 Correct 26 ms 15964 KB Output is correct
19 Correct 70 ms 27216 KB Output is correct
20 Correct 72 ms 28240 KB Output is correct
21 Correct 59 ms 24144 KB Output is correct
22 Correct 54 ms 22868 KB Output is correct
23 Correct 63 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11100 KB Output is correct
2 Correct 5 ms 11100 KB Output is correct
3 Correct 5 ms 11100 KB Output is correct
4 Correct 4 ms 10844 KB Output is correct
5 Correct 4 ms 10844 KB Output is correct
6 Correct 5 ms 11100 KB Output is correct
7 Correct 4 ms 10844 KB Output is correct
8 Correct 4 ms 11100 KB Output is correct
9 Correct 6 ms 11356 KB Output is correct
10 Correct 5 ms 10844 KB Output is correct
11 Correct 11 ms 13704 KB Output is correct
12 Correct 22 ms 15960 KB Output is correct
13 Correct 49 ms 35000 KB Output is correct
14 Correct 65 ms 27220 KB Output is correct
15 Correct 72 ms 28244 KB Output is correct
16 Correct 58 ms 24460 KB Output is correct
17 Correct 54 ms 23120 KB Output is correct
18 Correct 26 ms 15964 KB Output is correct
19 Correct 70 ms 27216 KB Output is correct
20 Correct 72 ms 28240 KB Output is correct
21 Correct 59 ms 24144 KB Output is correct
22 Correct 54 ms 22868 KB Output is correct
23 Correct 63 ms 28752 KB Output is correct
24 Correct 5 ms 11096 KB Output is correct
25 Correct 11 ms 13912 KB Output is correct
26 Correct 21 ms 15908 KB Output is correct
27 Correct 45 ms 35008 KB Output is correct
28 Correct 64 ms 27264 KB Output is correct
29 Correct 70 ms 28240 KB Output is correct
30 Correct 55 ms 24400 KB Output is correct
31 Correct 57 ms 23060 KB Output is correct
32 Correct 27 ms 16212 KB Output is correct
33 Correct 55 ms 27216 KB Output is correct
34 Correct 64 ms 28244 KB Output is correct
35 Correct 59 ms 24160 KB Output is correct
36 Correct 52 ms 23120 KB Output is correct
37 Correct 57 ms 28752 KB Output is correct
38 Correct 90 ms 42836 KB Output is correct