답안 #850900

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850900 2023-09-17T17:33:24 Z annabeth9680 RMQ (NOI17_rmq) C++17
100 / 100
178 ms 13220 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 1e5+11;
struct segtree{
    int T[MAXN<<2];
    void init(){
        fill(T,T+(MAXN<<2),-1);
    }
    void update(int findl, int findr, int val, int l, int r, int cur){
        if(findr < l || r < findl) return;
        if(findl <= l && r <= findr){
            T[cur] = max(T[cur],val);
            return;
        }
        T[2*cur+1] = max(T[cur],T[2*cur+1]);
        T[2*cur+2] = max(T[cur],T[2*cur+2]);
        int m = (l+r)/2;
        update(findl,findr,val,l,m,2*cur+1);
        update(findl,findr,val,m+1,r,2*cur+2);
        T[cur] = min(T[2*cur+1],T[2*cur+2]);
    }
    int query(int findl, int findr, int l, int r, int cur){
        if(findr < l || r < findl) return (1<<30);
        if(findl <= l && r <= findr) return T[cur];
        T[2*cur+1] = max(T[cur],T[2*cur+1]);
        T[2*cur+2] = max(T[cur],T[2*cur+2]);
        int m = (l+r)/2;
        return min(query(findl,findr,l,m,cur*2+1),query(findl,findr,m+1,r,cur*2+2));
    }
}seg1,seg2;
struct rmq{
    int l,r,x;
};
bool cmp(rmq a, rmq b){
    return a.r-a.l < b.r-b.l;
}
vector<rmq> qs[MAXN];
int LL[MAXN], RR[MAXN], L[MAXN], R[MAXN], ans[MAXN];
bool used[MAXN], used2[MAXN];
int main()
{
    int N,Q; cin >> N >> Q;
    vector<rmq> queries;
    for(int i = 0;i<Q;++i){
        int l,r,x; cin >> l >> r >> x;
        queries.push_back({l,r,x});
        qs[x].push_back({l,r,x});
        seg1.update(l,r,x,0,N-1,0);
    }
    auto ret = [&N](){
        for(int i = 0;i<N;++i) cout << -1 << " ";
        cout << "\n";
        exit(0);
	};
    for(int i = 0;i<Q;++i){
        int c = seg1.query(queries[i].l,queries[i].r,0,N-1,0);
        if(c != queries[i].x) ret();
    }
    for(int i = 0;i<N;++i){
        int curl = 0, curr = N-1;
        for(int j = 0;j<qs[i].size();++j){
            curl = max(curl,qs[i][j].l);
            curr = min(curr,qs[i][j].r);
            //cout << i << " " << qs[i][j].l << " " << qs[i][j].r << "\n";
        }
        if(curl > curr) ret();
        LL[i] = curl; RR[i] = curr;
    }
    for(int i = 0;i<N;++i){
        L[i] = 1<<30; R[i] = -1<<30;
    }
    for(int i = 0;i<N;++i){
        int c = seg1.query(i,i,0,N-1,0);
        if(c == -1) continue;
        if(LL[c] <= i && i <= RR[c]){
            L[c] = min(L[c],i);
            R[c] = max(R[c],i);
        }
    }
    vector<rmq> nxt;
    for(int i = 0;i<N;++i){
        if(qs[i].size()){
            if(L[i] > R[i]){
                ret();
            }
            nxt.push_back({L[i],R[i],i});
        }
    }
    sort(nxt.begin(),nxt.end(),cmp);
    auto sign = [](int i, int val){
		used[i] = true, used2[val] = true;
		ans[i] = val;
	};
	for(auto s : nxt){
        for(int j = s.l;j <= s.r;++j){
            if(!used[j]){
                sign(j,s.x);
                break;
            }
        }
	}
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
	for(int i = 0;i<N;++i){
        if(!used[i]){
            int c = seg1.query(i,i,0,N-1,0);
            pq.push({c,i});
        }
	}
	for(int i = 0;i<N;++i){
        if(!used2[i]){
            int se = pq.top().s; pq.pop();
            ans[se] = i;
        }
	}
	for(int i = 0;i<N;++i) seg2.update(i,i,ans[i],0,N-1,0);
	//for(int i = 0;i<N;++i) cout << ans[i] << " ";
	//cout << "\n";
	for(int i = 0;i<Q;++i){
        int c = seg2.query(queries[i].l,queries[i].r,0,N-1,0);
        //cout << c << "\n";
        if(c != queries[i].x) ret();
	}
	for(int i = 0;i<N;++i){
        cout << ans[i] << " ";
	}
	cout << "\n";
    return 0;
}

Compilation message

rmq.cpp: In function 'int main()':
rmq.cpp:63:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<rmq>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for(int j = 0;j<qs[i].size();++j){
      |                       ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6488 KB Output is correct
16 Correct 2 ms 6488 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6488 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 1 ms 6488 KB Output is correct
21 Correct 2 ms 6488 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 2 ms 6488 KB Output is correct
25 Correct 1 ms 6488 KB Output is correct
26 Correct 1 ms 6592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6488 KB Output is correct
16 Correct 2 ms 6488 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6488 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 1 ms 6488 KB Output is correct
21 Correct 2 ms 6488 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 2 ms 6488 KB Output is correct
25 Correct 1 ms 6488 KB Output is correct
26 Correct 1 ms 6592 KB Output is correct
27 Correct 178 ms 13220 KB Output is correct
28 Correct 157 ms 12888 KB Output is correct
29 Correct 127 ms 11612 KB Output is correct
30 Correct 168 ms 13052 KB Output is correct
31 Correct 139 ms 11440 KB Output is correct
32 Correct 153 ms 11664 KB Output is correct
33 Correct 39 ms 8400 KB Output is correct
34 Correct 28 ms 7780 KB Output is correct
35 Correct 59 ms 9132 KB Output is correct
36 Correct 60 ms 10392 KB Output is correct
37 Correct 100 ms 11808 KB Output is correct
38 Correct 5 ms 6744 KB Output is correct
39 Correct 70 ms 10900 KB Output is correct
40 Correct 2 ms 6488 KB Output is correct
41 Correct 1 ms 6488 KB Output is correct