답안 #878067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878067 2023-11-24T04:07:00 Z sleepntsheep 늑대인간 (IOI18_werewolf) C++17
100 / 100
988 ms 208888 KB
#define SHINLENA2

#ifndef SHINLENA
#include "werewolf.h"
#endif 


#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstddef>
#include <cassert>
using namespace std;
#define NN 200000
 
 
struct kruskal_reconstruction_tree
{
    vector<int> p, tin, tout, rv;
    int l;
    vector<vector<int>> g;
 
    kruskal_reconstruction_tree(int n, int n0) : p(n), tin(n), tout(n), rv(n), l(n0), g(n)
    {
        iota(p.begin(), p.end(), 0);
    }
 
    int find(int x)
    {
        return x == p[x] ? x : p[x] = find(p[x]);
    }
 
    int unite(int x, int y)
    {
        x = find(x); y = find(y);
        if (x == y) return 0;
        g[p[x] = p[y] = ++l] = {x, y};
        return 1;
    }
 
    int timer = 0;
    void dfs(int u, int p)
    {
        rv[tin[u] = timer++] = u;
        for (auto v : g[u]) if (v != p) dfs(v, u);
        tout[u] = timer - 1;
    }
 
    void tour()
    {
        for (int i = l +1; i--;) if (!tin[i]) dfs(i, i);
    }
};
 

vector<int> t[NN<<4];
 
void add(int v, int l, int r, int p, int k)
{
    t[v].push_back(k);
    if (l == r) return;
    if (p <= (l+r)/2) add(2*v+1, l, (l+r)/2, p, k);
    else add(2*v+2, (l+r)/2+1, r, p, k);
}
 
int qry(int v, int l, int r, int x, int y, int k)
{
    if (r < x || y < l) return 1e9;
    if (x <= l && r <= y)
    {
        auto it = lower_bound(t[v].begin(), t[v].end(), k);
        return it == t[v].end() ? 1e9 : *it;
    }
    return min(qry(2*v+1, l, (l+r)/2, x, y, k), qry(2*v+2, (l+r)/2+1, r, x, y, k));
}
 
vector<int> check_validity(int N, vector<int> X, vector<int> Y,
        vector<int> S, vector<int> E,
        vector<int> L, vector<int> R) {
    int M = X.size();
    kruskal_reconstruction_tree krt1(N+M, N-1);
    kruskal_reconstruction_tree krt2(N+M, N-1);
 
    int Q = S.size();
    vector<int> A(Q);
    vector<vector<int>> g(N);
 
    for (int i = 0; i < M; ++i) g[X[i]].push_back(Y[i]), g[Y[i]].push_back(X[i]);
 
    /* build krt */
    vector<int> rtl(Q), rtr(Q);
    {
        vector<vector<int>> byl(N), byr(N);
        for (int i = 0; i < Q; ++i) byl[L[i]].push_back(i), byr[R[i]].push_back(i);
        for (int i = N; i--;)
        {
            for (auto v : g[i]) if (v >= i) krt1.unite(v, i);
            for (auto j : byl[i]) rtl[j] = krt1.find(S[j]);
        }
        for (int i = 0; i < N; ++i)
        {
            for (auto v : g[i]) if (v <= i) krt2.unite(v, i);
            for (auto j : byr[i]) rtr[j] = krt2.find(E[j]);
        }
    }
 
    krt1.tour();
    krt2.tour();
    
     int LL = krt1.timer - 1;
    /* build merge sort tree */
    {
        vector<pair<int, int>> ins;
        for (int i = 0; i < N; ++i)
             ins.emplace_back(krt2.tin[i], krt1.tin[i]);
             sort(ins.begin(), ins.end());
             for (auto [x, y] : ins) add(0, 0, LL, y, x);
    }
    
    for (int i = 0; i < Q; ++i)
    {
        A[i] = qry(0, 0, LL, krt1.tin[rtl[i]], krt1.tout[rtl[i]], krt2.tin[rtr[i]]) <= krt2.tout[rtr[i]];
    }
 
    return A;
}

#ifdef SHINLENA
int main()
{
    int n,m,q;
    cin>>n>>m>>q;
    vector<int>x,y,s,e,l,r;
    for (int u,v,i=0;i<m;++i)cin>>u>>v, x.push_back(u), y.push_back(v);
    
    for (int a,b,c,d,i=0;i<q;++i)
    {
        cin>>a>>b>>c>>d;
        s.push_back(a);
        e.push_back(b);
        l.push_back(c);
        r.push_back(d);
        
    }
    auto A = check_validity(n,x,y,s,e,l,r);
    for (auto x : A) cout << x << endl;
    return 0;
}

#endif

/*

6 6 3
5 1
1 3
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4

*/

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:116:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  116 |         for (int i = 0; i < N; ++i)
      |         ^~~
werewolf.cpp:118:14: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  118 |              sort(ins.begin(), ins.end());
      |              ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 75608 KB Output is correct
2 Correct 17 ms 75616 KB Output is correct
3 Correct 17 ms 75612 KB Output is correct
4 Correct 17 ms 75356 KB Output is correct
5 Correct 17 ms 75612 KB Output is correct
6 Correct 17 ms 75632 KB Output is correct
7 Correct 17 ms 75612 KB Output is correct
8 Correct 19 ms 75608 KB Output is correct
9 Correct 17 ms 75612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 75608 KB Output is correct
2 Correct 17 ms 75616 KB Output is correct
3 Correct 17 ms 75612 KB Output is correct
4 Correct 17 ms 75356 KB Output is correct
5 Correct 17 ms 75612 KB Output is correct
6 Correct 17 ms 75632 KB Output is correct
7 Correct 17 ms 75612 KB Output is correct
8 Correct 19 ms 75608 KB Output is correct
9 Correct 17 ms 75612 KB Output is correct
10 Correct 23 ms 77144 KB Output is correct
11 Correct 23 ms 77148 KB Output is correct
12 Correct 22 ms 76892 KB Output is correct
13 Correct 22 ms 77148 KB Output is correct
14 Correct 24 ms 77032 KB Output is correct
15 Correct 23 ms 77404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 628 ms 181012 KB Output is correct
2 Correct 646 ms 186208 KB Output is correct
3 Correct 630 ms 184236 KB Output is correct
4 Correct 577 ms 183464 KB Output is correct
5 Correct 595 ms 183364 KB Output is correct
6 Correct 605 ms 183252 KB Output is correct
7 Correct 562 ms 182828 KB Output is correct
8 Correct 630 ms 186020 KB Output is correct
9 Correct 494 ms 184056 KB Output is correct
10 Correct 525 ms 183140 KB Output is correct
11 Correct 527 ms 183212 KB Output is correct
12 Correct 489 ms 182776 KB Output is correct
13 Correct 576 ms 182956 KB Output is correct
14 Correct 589 ms 183136 KB Output is correct
15 Correct 634 ms 183064 KB Output is correct
16 Correct 585 ms 182948 KB Output is correct
17 Correct 553 ms 182884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 75608 KB Output is correct
2 Correct 17 ms 75616 KB Output is correct
3 Correct 17 ms 75612 KB Output is correct
4 Correct 17 ms 75356 KB Output is correct
5 Correct 17 ms 75612 KB Output is correct
6 Correct 17 ms 75632 KB Output is correct
7 Correct 17 ms 75612 KB Output is correct
8 Correct 19 ms 75608 KB Output is correct
9 Correct 17 ms 75612 KB Output is correct
10 Correct 23 ms 77144 KB Output is correct
11 Correct 23 ms 77148 KB Output is correct
12 Correct 22 ms 76892 KB Output is correct
13 Correct 22 ms 77148 KB Output is correct
14 Correct 24 ms 77032 KB Output is correct
15 Correct 23 ms 77404 KB Output is correct
16 Correct 628 ms 181012 KB Output is correct
17 Correct 646 ms 186208 KB Output is correct
18 Correct 630 ms 184236 KB Output is correct
19 Correct 577 ms 183464 KB Output is correct
20 Correct 595 ms 183364 KB Output is correct
21 Correct 605 ms 183252 KB Output is correct
22 Correct 562 ms 182828 KB Output is correct
23 Correct 630 ms 186020 KB Output is correct
24 Correct 494 ms 184056 KB Output is correct
25 Correct 525 ms 183140 KB Output is correct
26 Correct 527 ms 183212 KB Output is correct
27 Correct 489 ms 182776 KB Output is correct
28 Correct 576 ms 182956 KB Output is correct
29 Correct 589 ms 183136 KB Output is correct
30 Correct 634 ms 183064 KB Output is correct
31 Correct 585 ms 182948 KB Output is correct
32 Correct 553 ms 182884 KB Output is correct
33 Correct 802 ms 184976 KB Output is correct
34 Correct 242 ms 141808 KB Output is correct
35 Correct 988 ms 188960 KB Output is correct
36 Correct 766 ms 183976 KB Output is correct
37 Correct 864 ms 187564 KB Output is correct
38 Correct 790 ms 184752 KB Output is correct
39 Correct 673 ms 190288 KB Output is correct
40 Correct 646 ms 207376 KB Output is correct
41 Correct 777 ms 186348 KB Output is correct
42 Correct 629 ms 183764 KB Output is correct
43 Correct 908 ms 197748 KB Output is correct
44 Correct 830 ms 187820 KB Output is correct
45 Correct 579 ms 191156 KB Output is correct
46 Correct 610 ms 190788 KB Output is correct
47 Correct 595 ms 183652 KB Output is correct
48 Correct 562 ms 182956 KB Output is correct
49 Correct 585 ms 183464 KB Output is correct
50 Correct 560 ms 183344 KB Output is correct
51 Correct 609 ms 208888 KB Output is correct
52 Correct 580 ms 208812 KB Output is correct