답안 #1100365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100365 2024-10-13T15:37:50 Z 8pete8 Fire (BOI24_fire) C++17
100 / 100
486 ms 132532 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m;
void setIO(string name){		
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);	
}
int sz=0;
struct seg2{
    pii v[2*mxn+10];
    void init(){for(int i=0;i<=2*sz;i++)v[i]={minf,minf};}
    void update(int pos,pii val){
        pos+=sz;
        v[pos]=max(v[pos],val);
        for(int i=pos;i>0;i>>=1)v[i>>1]=max(v[i],v[i^1]);
    }
    pii qry(int l,int r){
        pii ans={minf,minf};
        for(l+=sz,r+=sz;l<=r;l>>=1,r>>=1){
            if(l&1)ans=max(ans,v[l++]);
            if(!(r&1))ans=max(ans,v[r--]);
        }
        return ans;
    }
}t2;
vector<int>adj[mxn+10];
int ans=inf;
vector<pii>v,v2;
vector<int>point;
int up[mxn+10][lg+1],up2[mxn+10],bruh[mxn+10];
int32_t main(){
    fastio
    cin>>n>>m;
    v.resize(n);
    for(int i=0;i<n;i++){
        cin>>v[i].f>>v[i].s;
        if(v[i].f>v[i].s){
            point.pb(v[i].f);
            point.pb(v[i].s+m+1);
        }
        else{
            point.pb(v[i].f);
            point.pb(v[i].s);
            point.pb(v[i].f+m+1);
            point.pb(v[i].s+m+1);
        }
    }
    sort(all(point));
    point.erase(unique(all(point)),point.end());
    int id=0;
    sz=point.size();
    t2.init();
    for(auto &i:v){
        v2.pb({i.f,i.s});
        if(i.f<i.s){
            t2.update(lb(all(point),i.f)-point.begin(),{lb(all(point),i.s)-point.begin(),id});
            t2.update(lb(all(point),i.f+m+1)-point.begin(),{lb(all(point),i.s+m+1)-point.begin(),id});
            i.f=lb(all(point),i.f)-point.begin(),i.s=lb(all(point),i.s)-point.begin();
        }
        else{
            t2.update(lb(all(point),i.f)-point.begin(),{lb(all(point),i.s+m+1)-point.begin(),id});
            i.f=lb(all(point),i.f)-point.begin(),i.s=lb(all(point),i.s+m+1)-point.begin();
        }
        id++;
    }
    for(int i=0;i<n;i++){
        if(v2[i].f<v2[i].s){
            int go=t2.qry(v[i].f,v[i].s).s;
            if(go!=minf&&go!=i)up[i][0]=go;
            else up[i][0]=i,bruh[i]=1;
        }
        else{
            int go=t2.qry(v[i].f,v[i].s).s;
            up[i][0]=i;
            if(go==i)bruh[i]=1;
            up2[i]=go;
        }
    }
    for(int i=1;i<=lg;i++)for(int j=0;j<n;j++)up[j][i]=up[up[j][i-1]][i-1];
    for(int i=0;i<n;i++){
        int x=up[i][lg],cnt=1,cur=i;
        if(bruh[cur])continue;
        for(int j=lg;j>=0;j--)if(up[cur][j]!=x)cur=up[cur][j],cnt+=(1LL<<j);
        if(cur!=x)cur=up[cur][0],cnt++;
        if(bruh[cur])continue;
        while(v2[cur].f>v2[cur].s){
            cur=up2[cur];
            cnt++;
            if(bruh[cur])break;
            if(v2[cur].s>=v2[i].f)break;
        }
        x=up[cur][lg];
        if(bruh[cur])continue;
        if(v2[cur].s>=v2[i].f){
            ans=min(ans,cnt);
            continue;
        }
        for(int j=lg;j>=0;j--)if(up[cur][j]!=x&&v2[up[cur][j]].s<v2[i].f)cur=up[cur][j],cnt+=(1LL<<j);
        if(bruh[cur])continue;
        cur=up[cur][0];
        if(bruh[cur])continue;
        cnt++;
        if(v2[cur].s>=v2[i].f||cur==x)ans=min(ans,cnt);
    }
    if(ans>=inf)cout<<-1;
    else cout<<ans;
}
/*
12
58
66
68
92
125
150
175
223
269
288
291
3

8 100
10 30
30 50
50 70
40 60
60 80
50 70
80 100
100 20

4 100
0 30
30 70
70 100
60 20

1 100
30 40
*/

Compilation message

Main.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
   32 | #pragma GCC optimize ("03,unroll-lopps")
      |                                        ^
Main.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   38 | void setIO(string name){
      |                       ^
Main.cpp:46:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   46 |     void init(){for(int i=0;i<=2*sz;i++)v[i]={minf,minf};}
      |               ^
Main.cpp:47:32: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   47 |     void update(int pos,pii val){
      |                                ^
Main.cpp:52:24: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   52 |     pii qry(int l,int r){
      |                        ^
Main.cpp:66:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   66 | int32_t main(){
      |              ^
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31060 KB Output is correct
2 Correct 4 ms 29196 KB Output is correct
3 Correct 5 ms 31072 KB Output is correct
4 Correct 5 ms 31060 KB Output is correct
5 Correct 5 ms 31188 KB Output is correct
6 Correct 5 ms 31060 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 6 ms 31232 KB Output is correct
9 Correct 5 ms 31060 KB Output is correct
10 Correct 6 ms 31260 KB Output is correct
11 Correct 6 ms 31060 KB Output is correct
12 Correct 6 ms 31228 KB Output is correct
13 Correct 5 ms 31060 KB Output is correct
14 Correct 6 ms 31060 KB Output is correct
15 Correct 6 ms 31060 KB Output is correct
16 Correct 7 ms 31060 KB Output is correct
17 Correct 5 ms 29012 KB Output is correct
18 Correct 5 ms 31060 KB Output is correct
19 Correct 5 ms 31060 KB Output is correct
20 Correct 6 ms 31060 KB Output is correct
21 Correct 5 ms 31060 KB Output is correct
22 Correct 5 ms 31240 KB Output is correct
23 Correct 5 ms 31060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31060 KB Output is correct
2 Correct 4 ms 29196 KB Output is correct
3 Correct 5 ms 31072 KB Output is correct
4 Correct 5 ms 31060 KB Output is correct
5 Correct 5 ms 31188 KB Output is correct
6 Correct 5 ms 31060 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 6 ms 31232 KB Output is correct
9 Correct 5 ms 31060 KB Output is correct
10 Correct 6 ms 31260 KB Output is correct
11 Correct 6 ms 31060 KB Output is correct
12 Correct 6 ms 31228 KB Output is correct
13 Correct 5 ms 31060 KB Output is correct
14 Correct 6 ms 31060 KB Output is correct
15 Correct 6 ms 31060 KB Output is correct
16 Correct 7 ms 31060 KB Output is correct
17 Correct 5 ms 29012 KB Output is correct
18 Correct 5 ms 31060 KB Output is correct
19 Correct 5 ms 31060 KB Output is correct
20 Correct 6 ms 31060 KB Output is correct
21 Correct 5 ms 31060 KB Output is correct
22 Correct 5 ms 31240 KB Output is correct
23 Correct 5 ms 31060 KB Output is correct
24 Correct 6 ms 31060 KB Output is correct
25 Correct 5 ms 31060 KB Output is correct
26 Correct 6 ms 31060 KB Output is correct
27 Correct 6 ms 31060 KB Output is correct
28 Correct 6 ms 31060 KB Output is correct
29 Correct 6 ms 31060 KB Output is correct
30 Correct 6 ms 31060 KB Output is correct
31 Correct 6 ms 31060 KB Output is correct
32 Correct 6 ms 31060 KB Output is correct
33 Correct 5 ms 31060 KB Output is correct
34 Correct 5 ms 31060 KB Output is correct
35 Correct 6 ms 31224 KB Output is correct
36 Correct 6 ms 31060 KB Output is correct
37 Correct 6 ms 31060 KB Output is correct
38 Correct 5 ms 31060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31060 KB Output is correct
2 Correct 4 ms 29196 KB Output is correct
3 Correct 5 ms 31072 KB Output is correct
4 Correct 5 ms 31060 KB Output is correct
5 Correct 5 ms 31188 KB Output is correct
6 Correct 5 ms 31060 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 6 ms 31232 KB Output is correct
9 Correct 5 ms 31060 KB Output is correct
10 Correct 6 ms 31260 KB Output is correct
11 Correct 6 ms 31060 KB Output is correct
12 Correct 6 ms 31228 KB Output is correct
13 Correct 5 ms 31060 KB Output is correct
14 Correct 6 ms 31060 KB Output is correct
15 Correct 6 ms 31060 KB Output is correct
16 Correct 7 ms 31060 KB Output is correct
17 Correct 5 ms 29012 KB Output is correct
18 Correct 5 ms 31060 KB Output is correct
19 Correct 5 ms 31060 KB Output is correct
20 Correct 6 ms 31060 KB Output is correct
21 Correct 5 ms 31060 KB Output is correct
22 Correct 5 ms 31240 KB Output is correct
23 Correct 5 ms 31060 KB Output is correct
24 Correct 6 ms 31060 KB Output is correct
25 Correct 5 ms 31060 KB Output is correct
26 Correct 6 ms 31060 KB Output is correct
27 Correct 6 ms 31060 KB Output is correct
28 Correct 6 ms 31060 KB Output is correct
29 Correct 6 ms 31060 KB Output is correct
30 Correct 6 ms 31060 KB Output is correct
31 Correct 6 ms 31060 KB Output is correct
32 Correct 6 ms 31060 KB Output is correct
33 Correct 5 ms 31060 KB Output is correct
34 Correct 5 ms 31060 KB Output is correct
35 Correct 6 ms 31224 KB Output is correct
36 Correct 6 ms 31060 KB Output is correct
37 Correct 6 ms 31060 KB Output is correct
38 Correct 5 ms 31060 KB Output is correct
39 Correct 14 ms 33744 KB Output is correct
40 Correct 12 ms 33908 KB Output is correct
41 Correct 12 ms 33744 KB Output is correct
42 Correct 15 ms 33616 KB Output is correct
43 Correct 11 ms 33744 KB Output is correct
44 Correct 12 ms 33932 KB Output is correct
45 Correct 12 ms 33600 KB Output is correct
46 Correct 13 ms 33744 KB Output is correct
47 Correct 12 ms 33744 KB Output is correct
48 Correct 11 ms 33852 KB Output is correct
49 Correct 9 ms 33744 KB Output is correct
50 Correct 14 ms 33668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29028 KB Output is correct
2 Correct 5 ms 31060 KB Output is correct
3 Correct 5 ms 31076 KB Output is correct
4 Correct 5 ms 31300 KB Output is correct
5 Correct 5 ms 31060 KB Output is correct
6 Correct 4 ms 29200 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 5 ms 31060 KB Output is correct
9 Correct 5 ms 31060 KB Output is correct
10 Correct 5 ms 31060 KB Output is correct
11 Correct 5 ms 31060 KB Output is correct
12 Correct 5 ms 31060 KB Output is correct
13 Correct 5 ms 31060 KB Output is correct
14 Correct 5 ms 31060 KB Output is correct
15 Correct 11 ms 33812 KB Output is correct
16 Correct 11 ms 33744 KB Output is correct
17 Correct 11 ms 33744 KB Output is correct
18 Correct 11 ms 33744 KB Output is correct
19 Correct 10 ms 33744 KB Output is correct
20 Correct 70 ms 99772 KB Output is correct
21 Correct 397 ms 131768 KB Output is correct
22 Correct 381 ms 131764 KB Output is correct
23 Correct 423 ms 115392 KB Output is correct
24 Correct 433 ms 127684 KB Output is correct
25 Correct 292 ms 112820 KB Output is correct
26 Correct 398 ms 115544 KB Output is correct
27 Correct 413 ms 117940 KB Output is correct
28 Correct 345 ms 131636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29012 KB Output is correct
2 Correct 5 ms 31060 KB Output is correct
3 Correct 5 ms 31240 KB Output is correct
4 Correct 5 ms 31060 KB Output is correct
5 Correct 5 ms 31060 KB Output is correct
6 Correct 6 ms 29200 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 5 ms 31060 KB Output is correct
9 Correct 6 ms 31224 KB Output is correct
10 Correct 5 ms 31136 KB Output is correct
11 Correct 5 ms 31060 KB Output is correct
12 Correct 11 ms 33676 KB Output is correct
13 Correct 11 ms 33744 KB Output is correct
14 Correct 13 ms 33744 KB Output is correct
15 Correct 74 ms 100532 KB Output is correct
16 Correct 396 ms 114236 KB Output is correct
17 Correct 419 ms 130480 KB Output is correct
18 Correct 379 ms 126292 KB Output is correct
19 Correct 432 ms 128180 KB Output is correct
20 Correct 423 ms 116148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31060 KB Output is correct
2 Correct 4 ms 29196 KB Output is correct
3 Correct 5 ms 31072 KB Output is correct
4 Correct 5 ms 31060 KB Output is correct
5 Correct 5 ms 31188 KB Output is correct
6 Correct 5 ms 31060 KB Output is correct
7 Correct 5 ms 31060 KB Output is correct
8 Correct 6 ms 31232 KB Output is correct
9 Correct 5 ms 31060 KB Output is correct
10 Correct 6 ms 31260 KB Output is correct
11 Correct 6 ms 31060 KB Output is correct
12 Correct 6 ms 31228 KB Output is correct
13 Correct 5 ms 31060 KB Output is correct
14 Correct 6 ms 31060 KB Output is correct
15 Correct 6 ms 31060 KB Output is correct
16 Correct 7 ms 31060 KB Output is correct
17 Correct 5 ms 29012 KB Output is correct
18 Correct 5 ms 31060 KB Output is correct
19 Correct 5 ms 31060 KB Output is correct
20 Correct 6 ms 31060 KB Output is correct
21 Correct 5 ms 31060 KB Output is correct
22 Correct 5 ms 31240 KB Output is correct
23 Correct 5 ms 31060 KB Output is correct
24 Correct 6 ms 31060 KB Output is correct
25 Correct 5 ms 31060 KB Output is correct
26 Correct 6 ms 31060 KB Output is correct
27 Correct 6 ms 31060 KB Output is correct
28 Correct 6 ms 31060 KB Output is correct
29 Correct 6 ms 31060 KB Output is correct
30 Correct 6 ms 31060 KB Output is correct
31 Correct 6 ms 31060 KB Output is correct
32 Correct 6 ms 31060 KB Output is correct
33 Correct 5 ms 31060 KB Output is correct
34 Correct 5 ms 31060 KB Output is correct
35 Correct 6 ms 31224 KB Output is correct
36 Correct 6 ms 31060 KB Output is correct
37 Correct 6 ms 31060 KB Output is correct
38 Correct 5 ms 31060 KB Output is correct
39 Correct 14 ms 33744 KB Output is correct
40 Correct 12 ms 33908 KB Output is correct
41 Correct 12 ms 33744 KB Output is correct
42 Correct 15 ms 33616 KB Output is correct
43 Correct 11 ms 33744 KB Output is correct
44 Correct 12 ms 33932 KB Output is correct
45 Correct 12 ms 33600 KB Output is correct
46 Correct 13 ms 33744 KB Output is correct
47 Correct 12 ms 33744 KB Output is correct
48 Correct 11 ms 33852 KB Output is correct
49 Correct 9 ms 33744 KB Output is correct
50 Correct 14 ms 33668 KB Output is correct
51 Correct 5 ms 29028 KB Output is correct
52 Correct 5 ms 31060 KB Output is correct
53 Correct 5 ms 31076 KB Output is correct
54 Correct 5 ms 31300 KB Output is correct
55 Correct 5 ms 31060 KB Output is correct
56 Correct 4 ms 29200 KB Output is correct
57 Correct 5 ms 31060 KB Output is correct
58 Correct 5 ms 31060 KB Output is correct
59 Correct 5 ms 31060 KB Output is correct
60 Correct 5 ms 31060 KB Output is correct
61 Correct 5 ms 31060 KB Output is correct
62 Correct 5 ms 31060 KB Output is correct
63 Correct 5 ms 31060 KB Output is correct
64 Correct 5 ms 31060 KB Output is correct
65 Correct 11 ms 33812 KB Output is correct
66 Correct 11 ms 33744 KB Output is correct
67 Correct 11 ms 33744 KB Output is correct
68 Correct 11 ms 33744 KB Output is correct
69 Correct 10 ms 33744 KB Output is correct
70 Correct 70 ms 99772 KB Output is correct
71 Correct 397 ms 131768 KB Output is correct
72 Correct 381 ms 131764 KB Output is correct
73 Correct 423 ms 115392 KB Output is correct
74 Correct 433 ms 127684 KB Output is correct
75 Correct 292 ms 112820 KB Output is correct
76 Correct 398 ms 115544 KB Output is correct
77 Correct 413 ms 117940 KB Output is correct
78 Correct 345 ms 131636 KB Output is correct
79 Correct 5 ms 29012 KB Output is correct
80 Correct 5 ms 31060 KB Output is correct
81 Correct 5 ms 31240 KB Output is correct
82 Correct 5 ms 31060 KB Output is correct
83 Correct 5 ms 31060 KB Output is correct
84 Correct 6 ms 29200 KB Output is correct
85 Correct 5 ms 31060 KB Output is correct
86 Correct 5 ms 31060 KB Output is correct
87 Correct 6 ms 31224 KB Output is correct
88 Correct 5 ms 31136 KB Output is correct
89 Correct 5 ms 31060 KB Output is correct
90 Correct 11 ms 33676 KB Output is correct
91 Correct 11 ms 33744 KB Output is correct
92 Correct 13 ms 33744 KB Output is correct
93 Correct 74 ms 100532 KB Output is correct
94 Correct 396 ms 114236 KB Output is correct
95 Correct 419 ms 130480 KB Output is correct
96 Correct 379 ms 126292 KB Output is correct
97 Correct 432 ms 128180 KB Output is correct
98 Correct 423 ms 116148 KB Output is correct
99 Correct 367 ms 129716 KB Output is correct
100 Correct 420 ms 132532 KB Output is correct
101 Correct 486 ms 128180 KB Output is correct
102 Correct 463 ms 130488 KB Output is correct
103 Correct 427 ms 117168 KB Output is correct
104 Correct 389 ms 127664 KB Output is correct
105 Correct 202 ms 128536 KB Output is correct