Loading [MathJax]/jax/output/CommonHTML/jax.js
본문 바로가기

Problem Solving/Baekjoon Online Judge (Gold)

백준 29618 - 미술 시간 (C++)

문제

  • 문제 링크
  •   
       BOJ 29618 - 미술 시간 
      
  • 문제 요약
  • 칸의 길이 N과 아래와 같은 Q개의 쿼리가 주어진다.
    모든 쿼리의 처리가 끝난 후 각 칸의 상태를 출력하자.

  • a b x : a번째 칸부터 b번째 칸까지, 색칠되지 않은 칸을 x번 색으로 칠한다.

  • 제한
  • TL : 0.5 sec, ML : 512 MB

  • 1N,Q100,000
  • 1x109

알고리즘 분류

  • 자료 구조 (data_structures)
  • 트리를 사용한 집합과 맵 (tree _ set / map)

풀이

제한을 보면 당연히 나이브하게 돌 수 없다.

간단하게, std::set에서 색칠되지 않은 칸들을 들고 있다고 해보자.

그럼 임의의 쿼리 ai,bi,xi에서 ai의 위치lower_bound로 찾을 수 있다.
이후 bi보다 작은 동안 찾아지는 모든 칸에 xi를 기록하고, set에서 제거해주면 된다.

시간 복잡도는 O(NlogN).

전체 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, q; cin >> n >> q;
 
    set <int> S;
    for (int i(1); i <= n; S.insert(i++));
 
    vector <int> ans(n + 1);
    while (q--)
    {
        int a, b, x; cin >> a >> b >> x;
 
        queue <int> Q;
        for (auto it(S.lower_bound(a)); it != S.end() && *it <= b; it++)
            ans[*it] = x, Q.push(*it);
 
        for (; Q.size(); Q.pop())
            S.erase(Q.front());
    }
 
    for (int i(1); i <= n; cout << ans[i++<< ' ');
}
cs


comment