답안 #1038304

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038304 2024-07-29T15:57:26 Z Ludissey Fire (BOI24_fire) C++17
100 / 100
597 ms 164608 KB
#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
 
using namespace std;


struct node {
	int sum,index;
	node* left, *right;
	void update() {
		sum = max(left->sum,right->sum);
        if(left->sum > right->sum) index=left->index;
        else index=right->index;
 	}
    node(){
        sum=0;
        index=0;
        left=NULL;
        right=NULL;
    }
};


pair<int,int> query(node* root, int left, int right, int qLeft, int qRight) {
	if (left > qRight || right < qLeft) return {-1e9,-1};
	if (left >= qLeft && right <= qRight) return {root->sum, root->index};
	int mid = (left + right) / 2;
    pair<int,int> l=query(root->left, left, mid, qLeft, qRight);
    pair<int,int> r=query(root->right, mid + 1, right, qLeft, qRight);
    if(l.first>r.first) return l;
    else return r;
}

void build(node* root, int left, int right, const vector<pair<int,int>>& v) {
	if (left == right) {
		root->sum = v[left].first;
        root->index=v[left].second;
		return;
	}
	int mid = (left + right) / 2;
	root->left = new node();
	root->right = new node();
	build(root->left, left, mid, move(v));
	build(root->right, mid+1, right, move(v));
	root->update();
}
node* root;
vector<vector<int>> parent;
const int LOG=31;
vector<int> depth;
vector<int> tree;
bool lca(int a, int b){
    if(tree[a]!=tree[b]||depth[b]<depth[a]) return false;
    int d=depth[b]-depth[a];
    for (int j = LOG-1; j>=0; j--)
    {
        if(d&(1<<j)) b=parent[b][j];
    }
    return (a==b);
}
vector<pair<int,int>> a;
vector<vector<int>> child;
int n,m;
void dfs(int x, int d){
    depth[x]=d;
    for (auto u : child[x]) {
        tree[u]=tree[x];
        dfs(u,d+1);
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m;
    a.resize(n);
    depth.resize(n);
    tree.resize(n);
    parent.resize(n,vector<int>(LOG));
    child.resize(n);
    unordered_map<int,int> conv;
    set<int> cord;
    cord.insert(0);
    cord.insert(m);
    for (int i = 0; i < n; i++){
        cin >> a[i].first >> a[i].second;
        cord.insert(a[i].first);
        cord.insert(a[i].second);
        if(a[i].first>a[i].second) {
            a[i].second+=m;
            cord.insert(a[i].second);
        }
    }
    int x=0;
    for (auto u : cord)
    {
        conv[u]=x;
        x++;
    }
    vector<pair<int,int>> buildSEGTREE(x,{-1e9,-1});
    sort(a.begin(),a.end());
    for (int i = 0; i < n; i++) {
        if(buildSEGTREE[conv[a[i].first]].first<conv[a[i].second]){
            buildSEGTREE[conv[a[i].first]]={conv[a[i].second],i};
        }
    }
    root=new node();
    build(root, 0,x-1,move(buildSEGTREE));
    vector<int> roots;
    for (int i = 0; i < n; i++)
    {
        int p=query(root,0,x-1,conv[a[i].first],conv[a[i].second]).second;
        parent[i][0]=i;
        if(p>=0&&p!=i) {
            child[p].push_back(i);
            parent[i][0]=p;
        }
        else roots.push_back(i);
    }
    for (int j = 1; j < LOG; j++)
    {
        for (int i = 0; i < n; i++)
        {
            parent[i][j]=parent[parent[i][j-1]][j-1];
        }
    }
    
    for (int i = 0; i < sz(roots); i++){
        dfs(roots[i],0);
    }
    int mn=1e12;
    for (int i = 0; i < n; i++){
        if(a[i].second>=m){
            int p=query(root,0,x-1,conv[0],conv[(a[i].second-m)]).second;
            if(p==-1||i==p||!lca(i,p)) continue;
            mn=min(abs(depth[p]-depth[i])+1,mn);
        }
    }
    if(mn==1e12) mn=-1;
    cout << mn << "\n";
    return 0;
}
/*
10 240
3 68
7 126
40 121
58 152
73 139
89 166
121 197
126 311
212 271
237 350
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 0 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 0 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 7 ms 3928 KB Output is correct
40 Correct 7 ms 4188 KB Output is correct
41 Correct 8 ms 3932 KB Output is correct
42 Correct 5 ms 3420 KB Output is correct
43 Correct 6 ms 2908 KB Output is correct
44 Correct 5 ms 3420 KB Output is correct
45 Correct 5 ms 3420 KB Output is correct
46 Correct 6 ms 3420 KB Output is correct
47 Correct 7 ms 3932 KB Output is correct
48 Correct 8 ms 3932 KB Output is correct
49 Correct 5 ms 4188 KB Output is correct
50 Correct 5 ms 3160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 424 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 6 ms 4064 KB Output is correct
16 Correct 4 ms 3420 KB Output is correct
17 Correct 5 ms 2908 KB Output is correct
18 Correct 5 ms 3344 KB Output is correct
19 Correct 6 ms 3932 KB Output is correct
20 Correct 87 ms 68392 KB Output is correct
21 Correct 448 ms 149588 KB Output is correct
22 Correct 461 ms 149444 KB Output is correct
23 Correct 240 ms 121984 KB Output is correct
24 Correct 484 ms 159104 KB Output is correct
25 Correct 356 ms 103728 KB Output is correct
26 Correct 286 ms 117980 KB Output is correct
27 Correct 358 ms 125820 KB Output is correct
28 Correct 432 ms 153032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 7 ms 3988 KB Output is correct
13 Correct 4 ms 3420 KB Output is correct
14 Correct 6 ms 3144 KB Output is correct
15 Correct 91 ms 68304 KB Output is correct
16 Correct 261 ms 122016 KB Output is correct
17 Correct 483 ms 154560 KB Output is correct
18 Correct 597 ms 158148 KB Output is correct
19 Correct 447 ms 161984 KB Output is correct
20 Correct 255 ms 116988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 0 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 7 ms 3928 KB Output is correct
40 Correct 7 ms 4188 KB Output is correct
41 Correct 8 ms 3932 KB Output is correct
42 Correct 5 ms 3420 KB Output is correct
43 Correct 6 ms 2908 KB Output is correct
44 Correct 5 ms 3420 KB Output is correct
45 Correct 5 ms 3420 KB Output is correct
46 Correct 6 ms 3420 KB Output is correct
47 Correct 7 ms 3932 KB Output is correct
48 Correct 8 ms 3932 KB Output is correct
49 Correct 5 ms 4188 KB Output is correct
50 Correct 5 ms 3160 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 424 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 600 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 0 ms 604 KB Output is correct
62 Correct 0 ms 604 KB Output is correct
63 Correct 0 ms 604 KB Output is correct
64 Correct 0 ms 604 KB Output is correct
65 Correct 6 ms 4064 KB Output is correct
66 Correct 4 ms 3420 KB Output is correct
67 Correct 5 ms 2908 KB Output is correct
68 Correct 5 ms 3344 KB Output is correct
69 Correct 6 ms 3932 KB Output is correct
70 Correct 87 ms 68392 KB Output is correct
71 Correct 448 ms 149588 KB Output is correct
72 Correct 461 ms 149444 KB Output is correct
73 Correct 240 ms 121984 KB Output is correct
74 Correct 484 ms 159104 KB Output is correct
75 Correct 356 ms 103728 KB Output is correct
76 Correct 286 ms 117980 KB Output is correct
77 Correct 358 ms 125820 KB Output is correct
78 Correct 432 ms 153032 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 344 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 0 ms 604 KB Output is correct
87 Correct 1 ms 604 KB Output is correct
88 Correct 1 ms 604 KB Output is correct
89 Correct 1 ms 604 KB Output is correct
90 Correct 7 ms 3988 KB Output is correct
91 Correct 4 ms 3420 KB Output is correct
92 Correct 6 ms 3144 KB Output is correct
93 Correct 91 ms 68304 KB Output is correct
94 Correct 261 ms 122016 KB Output is correct
95 Correct 483 ms 154560 KB Output is correct
96 Correct 597 ms 158148 KB Output is correct
97 Correct 447 ms 161984 KB Output is correct
98 Correct 255 ms 116988 KB Output is correct
99 Correct 501 ms 153788 KB Output is correct
100 Correct 506 ms 153236 KB Output is correct
101 Correct 466 ms 164608 KB Output is correct
102 Correct 448 ms 161684 KB Output is correct
103 Correct 300 ms 125828 KB Output is correct
104 Correct 398 ms 150716 KB Output is correct
105 Correct 288 ms 158140 KB Output is correct