답안 #635640

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635640 2022-08-26T14:35:26 Z PoonYaPat RMQ (NOI17_rmq) C++14
100 / 100
32 ms 3660 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

int n,m,ans[100001];
pii pos[100001],wide[100001];
set<pii> s;
queue<int> q;
bool have[100001];

void exit() {
    for (int i=0; i<n; ++i) cout<<-1<<" ";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m;
    memset(ans,-1,sizeof(ans));
    while (m--) {
        int l,r,val;
        cin>>l>>r>>val;
        if (!have[val]) {
            pos[val]={l,r};
            wide[val]={l,r};
            have[val]=true;
        } else {
            pos[val]={max(l,pos[val].first),min(r,pos[val].second)};
            wide[val]={min(l,wide[val].first),max(r,wide[val].second)};
        }
    }

    for (int i=n-1; i>=0; --i) {
        if (!have[i]) q.push(i);
        else {
            bool can=false;
            auto it=s.upper_bound(pii(wide[i].first,wide[i].second));
            int j=0;

            if (s.size()==0) {
                for (int j=wide[i].first; j<=wide[i].second; ++j) {
                    if (ans[j]==-1) {
                        if (!can && pos[i].first<=j && j<=pos[i].second) {
                            can=true;
                            ans[j]=i;
                        } else if (q.empty()) {
                            exit(); return 0;
                        } else {
                            ans[j]=q.front();
                            q.pop();
                        }
                    }
                }
                continue;
            }

            if (it==s.begin()) {
                for (j; j<(*it).second && j<=wide[i].second; ++j) {
                    if (ans[j]==-1) {
                        if (!can && pos[i].first<=j && j<=pos[i].second) {
                            can=true;
                            ans[j]=i;
                        } else if (q.empty()) {
                            exit(); return 0;
                        } else {
                            ans[j]=q.front();
                            q.pop();
                        }
                    }
                }
                ++it;
            }

            auto h=it; --h;
            while (j<=wide[i].second) {
                for (j=(*h).first+1; j<(*it).second && j<=wide[i].second; ++j) {
                    if (ans[j]==-1) {
                        if (!can && pos[i].first<=j && j<=pos[i].second) {
                            can=true;
                            ans[j]=i;
                        } else if (q.empty()) {
                            exit(); return 0;
                        } else {
                            ans[j]=q.front();
                            q.pop();
                        }
                    }
                }
                ++h; ++it;
            }


            if (!can) {
                exit();
                return 0;
            }

            s.insert(pii(wide[i].second,wide[i].first));
        }
    }

    for (int i=0; i<n; ++i) {
        if (ans[i]==-1) {
            if (q.empty()) {
                exit(); return 0;
            } else {
                ans[i]=q.front();
                q.pop();
            }
        }
    }

    for (int i=0; i<n; ++i) cout<<ans[i]<<" ";
}

Compilation message

rmq.cpp: In function 'int main()':
rmq.cpp:57:22: warning: statement has no effect [-Wunused-value]
   57 |                 for (j; j<(*it).second && j<=wide[i].second; ++j) {
      |                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 712 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 712 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 720 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 712 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 720 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 29 ms 2992 KB Output is correct
28 Correct 24 ms 2820 KB Output is correct
29 Correct 20 ms 2536 KB Output is correct
30 Correct 25 ms 2900 KB Output is correct
31 Correct 20 ms 2320 KB Output is correct
32 Correct 24 ms 2444 KB Output is correct
33 Correct 10 ms 1476 KB Output is correct
34 Correct 5 ms 1236 KB Output is correct
35 Correct 13 ms 1728 KB Output is correct
36 Correct 18 ms 3436 KB Output is correct
37 Correct 26 ms 3660 KB Output is correct
38 Correct 4 ms 980 KB Output is correct
39 Correct 32 ms 2536 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 716 KB Output is correct