답안 #877769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877769 2023-11-23T14:15:05 Z Cookie Joker (BOI20_joker) C++14
0 / 100
211 ms 19656 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1000001;
const int mxn = 3e5 + 1, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5, p = 31;
//const int base = (1 << 18);
const ll inf = 1e9 + 5, neg = -69420;
struct DSU{
    vt<pii>past_p, past_sz;
    vt<int>checkpoint;
    int p[2 * mxn + 1], sz[2 * mxn + 1];
    void init(){
        for(int i = 0; i <= 2 * mxn; i++){
            p[i] = i; sz[i]  = 1;
        }
        past_sz.clear(); past_p.clear(); checkpoint.clear();
    }
    int fp(int a){
        if(p[a] == a)return(a);
        return(fp(p[a]));
    }
    bool check(int u, int v){
        return(fp(u) == fp(v));
    }
    void unon(int a, int b){
 
        a = fp(a); b = fp(b);
        if(sz[a] < sz[b])swap(a, b);
        past_sz.pb({a, sz[a]}); past_p.pb({b, p[b]});
        if(a != b){
            sz[a] += sz[b]; p[b] = a;
        }
    }
    void rollback(){
        //int before = p[past_p.back().fi], after = past_p.back().se;
        //if(before != past_p.back().fi && after == past_p.back().fi)cnt++;
        p[past_p.back().fi] = past_p.back().se; past_p.pop_back();
        sz[past_sz.back().fi] = past_sz.back().se; past_sz.pop_back();
    }
    void save(){
        checkpoint.pb(past_sz.size());
    }
    void to_last(){
        while(past_sz.size() != checkpoint.back())rollback();
        checkpoint.pop_back();
        
    }
};
DSU dsu;
int n, m, q;
int u[mxn + 1], v[mxn + 1];
int to[mxn + 1];
void input(){
    cin >> n >> m >> q;
    for(int i = 0; i < m; i++){
        cin >> u[i] >> v[i];
        
    }
 
}
void solve(int l, int r, int candl, int candr){
    if(candr == -1 || candl == candr){
        for(int i = l; i <= r; i++)to[i] = candr;
        return;
    }
    if(l > r)return;
    int mid = (l + r) >> 1;
    dsu.save(); dsu.save(); dsu.save();
    for(int i = l; i < mid; i++){
        dsu.unon(u[i] << 1, v[i] << 1 | 1); dsu.check(u[i] << 1 | 1, v[i] << 1);
    }
    int last = mid;
   
    for(int i = candr; i >= max(mid + 1, candl); i--){
        dsu.unon(u[i] << 1, v[i] << 1 | 1); dsu.check(u[i] << 1 | 1, v[i] << 1);
        if(dsu.check(u[i] << 1, u[i] << 1 | 1) || dsu.check(v[i] << 1, v[i] << 1 | 1)){
            last = i;
            break;
        }
        
    }
    to[mid] = last;
    
    dsu.to_last();
    bool bad = 0;
    for(int i = candr; i > last; i--){
        dsu.unon(u[i] << 1, v[i] << 1 | 1); dsu.check(u[i] << 1 | 1, v[i] << 1);
        if(dsu.check(u[i] << 1, u[i] << 1 | 1) || dsu.check(v[i] << 1, v[i] << 1 | 1)){
            bad = 1;
            break;
        }
    }
    if(bad == 1){
        for(int i = l; i < mid; i++)to[i] = last;
        
    }else{
        solve(l, mid - 1, candl, last);
    }
    dsu.to_last();
    for(int i = l; i <= mid; i++){
        dsu.unon(u[i] << 1, v[i] << 1 | 1); dsu.check(u[i] << 1 | 1, v[i] << 1);
    }
    solve(mid + 1, r, last, candr);
    dsu.to_last();
}
void process(){
   dsu.init();
   int last = m;
   for(int i = 0; i < m; i++){
       dsu.unon(u[i] << 1, v[i] << 1 | 1); dsu.unon(u[i] << 1 | 1, v[i] << 1);
       if(dsu.check(u[i] << 1, u[i] << 1 | 1) || dsu.check(v[i] << 1, v[i] << 1 | 1)){
           last = i; break;
       }
   }
   for(int i = last + 1; i < m; i++){
       to[i] = m;
   }
   dsu.init();
   solve(0, last, 0, m - 1); 
  
   while(q--){
      int l, r; cin >> l >> r; --l; --r;
      cout << ((to[l] > r) ? "YES" : "NO") << "\n";
    
   }
}
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    input();
    process();
    return(0);
}

Compilation message

Joker.cpp: In member function 'void DSU::to_last()':
Joker.cpp:62:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   62 |         while(past_sz.size() != checkpoint.back())rollback();
      |               ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Incorrect 2 ms 8284 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Incorrect 2 ms 8284 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 134 ms 14852 KB Output is correct
4 Correct 211 ms 18660 KB Output is correct
5 Incorrect 178 ms 19656 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Incorrect 2 ms 8284 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Incorrect 2 ms 8284 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Incorrect 2 ms 8284 KB Output isn't correct
5 Halted 0 ms 0 KB -