Submission #634649

# Submission time Handle Problem Language Result Execution time Memory
634649 2022-08-24T16:24:29 Z Do_you_copy Joker (BOI20_joker) C++17
0 / 100
18 ms 2736 KB
#include <bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n, m, q;
int cnt;
struct TDSU{
    vector <pii> save;
    vector <int> lab;
    TDSU(){}
    TDSU(int _n){
        lab.resize(_n, -1);
    }
    inline void resize(int _n){
        lab.resize(_n, -1);
    }
    inline int find_set(int u){
        if (lab[u] < 0) return u;
        return lab[u] = find_set(lab[u]);
    }
    inline int find(int u){
        ++cnt;
        if (lab[u] < 0) return u;
        return find(lab[u]);
    }
    inline void merge(int u, int v){
        if (lab[u] > lab[v]) swap(u, v);
        save.pb({v, lab[v]});
        lab[u] += lab[v];
        lab[v] = u;
    }
    inline void undo(){

        int v = save.back().fi;
        int u = lab[v];
        lab[v] = save.back().se;
        lab[u] -= lab[v];
        save.pop_back();
    }
    inline void clear(){
        lab.clear();
        save.clear();
    }
    inline void reset(int _n){
        clear();
        resize(_n);
    }
    inline void roll_back(int _n){
        while (save.size() > _n) undo();
    }
};

TDSU dsu;
pii e[maxN];
int last[maxN];

void DnC(int l, int r, int from, int to){
    if (l > r) return;
    int mid = (l + r) / 2;
    int sz = dsu.save.size();
    for (int i = l; i <= mid; ++i){
        int cnt1 = 0, cnt2 = 0;
        cnt = 0;
        int x = dsu.find(e[i].fi);
        cnt1 = cnt; cnt = 0;
        int y = dsu.find(e[i].se);
        cnt2 = cnt;
        if (x == y){
            if (cnt1 ^ cnt2 & 1){
                last[mid] = i;
                break;
            }
        }
        else dsu.merge(x, y);
    }
    if (last[mid] == m + 1){
        int szz = dsu.save.size();
        for (int i = min(to, m); i >= max(mid + 1, from); --i){
            int cnt1 = 0, cnt2 = 0;
            cnt = 0;
            int x = dsu.find(e[i].fi);
            cnt1 = cnt; cnt = 0;
            int y = dsu.find(e[i].se);
            cnt2 = cnt;
            if (x == y){
                if (cnt1 ^ cnt2 & 1){
                    last[mid] = i;
                    break;
                }
            }
            else dsu.merge(x, y);
        }
        if (last[mid] == m + 1) last[mid] = 0;
        dsu.roll_back(szz);
        DnC(mid + 1, r, last[mid], to);
    }
    else last[mid] = m + 1;
    dsu.roll_back(sz);
    for (int i = to; i >= last[mid] + 1; --i){
        int x = dsu.find(e[i].fi);
        int y = dsu.find(e[i].se);
        if (x != y) dsu.merge(x, y);
    }
    DnC(l, mid - 1, from, last[mid]);
    dsu.roll_back(sz);
}

void Init(){
    cin >> n >> m >> q;
    fill(last + 1, last + m + 1, m + 1);
    for (int i = 1; i <= m; ++i){
        cin >> e[i].fi >> e[i].se;
    }
    dsu.resize(n + 1);
    DnC(1, m, 1, m);
    for (int i = m; i >= 1; --i){
        int cnt1 = 0, cnt2 = 0;
        cnt = 0;
        int x = dsu.find(e[i].fi);
        cnt1 = cnt; cnt = 0;
        int y = dsu.find(e[i].se);
        cnt2 = cnt;
        if (x == y){
            if (cnt1 ^ cnt2 & 1){
                last[0] = i;
                break;
            }
        }
        else dsu.merge(x, y);
    }
    if (last[0] == m + 1) last[0] = 0;
    for (int i = 1; i <= q; ++i){
        int x, y; cin >> x >> y;
        --x; ++y;
        if (last[x] >= y) cout << "YES\n";
        else cout << "NO\n";
    }
}

#define debu
#define taskname "test"
signed main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        #ifdef debug
        freopen(taskname".out", "w", stdout);
        #endif
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << 1000 * double(clock()) / CLOCKS_PER_SEC;
        #ifndef debug
        cerr << "\nTime elapsed: " << 1000 * double(clock()) / CLOCKS_PER_SEC << "ms\n";
        #endif
    }
}

Compilation message

Joker.cpp: In member function 'void TDSU::roll_back(int)':
Joker.cpp:73:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   73 |         while (save.size() > _n) undo();
      |                ~~~~~~~~~~~~^~~~
Joker.cpp: In function 'void DnC(int, int, int, int)':
Joker.cpp:93:29: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   93 |             if (cnt1 ^ cnt2 & 1){
      |                        ~~~~~^~~
Joker.cpp:110:33: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  110 |                 if (cnt1 ^ cnt2 & 1){
      |                            ~~~~~^~~
Joker.cpp: In function 'void Init()':
Joker.cpp:148:29: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  148 |             if (cnt1 ^ cnt2 & 1){
      |                        ~~~~~^~~
Joker.cpp: In function 'int main()':
Joker.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Runtime error 18 ms 2736 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -