답안 #1038302

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038302 2024-07-29T15:56:55 Z Ludissey Fire (BOI24_fire) C++17
40 / 100
507 ms 131564 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=16;
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 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 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 0 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 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 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 0 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 1 ms 460 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 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 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 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 0 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 1 ms 460 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 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 0 ms 348 KB Output is correct
39 Correct 7 ms 3676 KB Output is correct
40 Correct 6 ms 3676 KB Output is correct
41 Correct 7 ms 3676 KB Output is correct
42 Correct 4 ms 2904 KB Output is correct
43 Correct 5 ms 2468 KB Output is correct
44 Correct 5 ms 2908 KB Output is correct
45 Correct 5 ms 2908 KB Output is correct
46 Correct 7 ms 3136 KB Output is correct
47 Correct 6 ms 3672 KB Output is correct
48 Correct 6 ms 3676 KB Output is correct
49 Correct 6 ms 3676 KB Output is correct
50 Correct 5 ms 2752 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 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 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 348 KB Output is correct
9 Correct 0 ms 460 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 452 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 7 ms 3540 KB Output is correct
16 Correct 4 ms 2748 KB Output is correct
17 Correct 5 ms 2648 KB Output is correct
18 Correct 5 ms 2908 KB Output is correct
19 Correct 6 ms 3676 KB Output is correct
20 Correct 60 ms 47272 KB Output is correct
21 Correct 507 ms 131564 KB Output is correct
22 Correct 506 ms 131324 KB Output is correct
23 Incorrect 218 ms 103040 KB Output isn't correct
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 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 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 7 ms 3512 KB Output is correct
13 Correct 4 ms 2908 KB Output is correct
14 Correct 4 ms 2612 KB Output is correct
15 Correct 65 ms 47056 KB Output is correct
16 Incorrect 243 ms 102940 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 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 0 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 1 ms 460 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 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 0 ms 348 KB Output is correct
39 Correct 7 ms 3676 KB Output is correct
40 Correct 6 ms 3676 KB Output is correct
41 Correct 7 ms 3676 KB Output is correct
42 Correct 4 ms 2904 KB Output is correct
43 Correct 5 ms 2468 KB Output is correct
44 Correct 5 ms 2908 KB Output is correct
45 Correct 5 ms 2908 KB Output is correct
46 Correct 7 ms 3136 KB Output is correct
47 Correct 6 ms 3672 KB Output is correct
48 Correct 6 ms 3676 KB Output is correct
49 Correct 6 ms 3676 KB Output is correct
50 Correct 5 ms 2752 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 452 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 460 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 464 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 7 ms 3540 KB Output is correct
66 Correct 4 ms 2748 KB Output is correct
67 Correct 5 ms 2648 KB Output is correct
68 Correct 5 ms 2908 KB Output is correct
69 Correct 6 ms 3676 KB Output is correct
70 Correct 60 ms 47272 KB Output is correct
71 Correct 507 ms 131564 KB Output is correct
72 Correct 506 ms 131324 KB Output is correct
73 Incorrect 218 ms 103040 KB Output isn't correct
74 Halted 0 ms 0 KB -