답안 #1037844

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037844 2024-07-29T09:07:29 Z Ludissey Fire (BOI24_fire) C++17
0 / 100
1 ms 348 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 {0,0};
	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<pair<int,int>> a;
vector<int> depth;
vector<int> tree;
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);
    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,0});
    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;
        if(p!=i) child[p].push_back(i);
        else roots.push_back(i);
    }
    for (int i = 0; i < sz(roots); i++){
        tree[roots[i]]=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(tree[p]!=tree[i]||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 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 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 Incorrect 1 ms 348 KB Output isn't correct
5 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 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -