PS

BOJ 12895 : 화려한 마을

lickelon 2024. 11. 25. 22:54
  • 문제 링크 : boj.kr/12895
  • 난이도 : P3
  • 태그 : 느리게 갱신되는 세그먼트 트리

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

vector<ll> arr, lazy, tree;

ll merge(ll a, ll b) {
    return a | b;
}

void push(int node, int s, int e, ll value) {
    if(value == 0) return;
    tree[node] = value;
    if(s != e) {
        lazy[node*2] = value;
        lazy[node*2+1] = value;
    }
    lazy[node] = 0;
}

void update(int node, int s, int e, int l, int r, ll value) {
    push(node, s, e, lazy[node]);
    if(e < l || r < s) return;
    if(l <= s && e <= r) {
        push(node, s, e, value);
        return;
    }
    update(node*2, s, (s+e)/2, l, r, value);
    update(node*2+1, (s+e)/2+1, e, l, r, value);
    tree[node] = merge(tree[node*2], tree[node*2+1]);
}

ll query(int node, int s, int e, int l, int r) {
    push(node, s, e, lazy[node]);
    if(e < l || r < s) return 0;
    if(l <= s && e <= r) return tree[node];

    ll lq = query(node*2, s, (s+e)/2, l, r);
    ll rq = query(node*2+1, (s+e)/2+1, e, l, r);
    return merge(lq, rq);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, t, q;
    cin >> n >> t >> q;
    lazy.resize(n*4, 0);
    tree.resize(n*4, 1);
    
    for(int i = 0; i < q; i++) {
        string s;
        int a, b;
        cin >> s >> a >> b;
        if(a > b) swap(a, b);
        if(s == "C") {
            int c;
            cin >> c;
            update(1, 1, n, a, b, 1 << (c-1));
        }
        if(s == "Q") {
            int ret = query(1, 1, n, a, b);
            int ans = 0;
            for(int i = 0; i < t; i++) {
                if(ret & (1 << i)) ans++;
            }
            cout << ans << "\n";
        }
    }

    return 0;
}

풀이

or 쿼리를 사용한다.

x > y인 쿼리가 존재함을 유의해야 한다....

728x90

'PS' 카테고리의 다른 글

BOJ 3002 : 아날로그 다이얼  (0) 2024.11.27
BOJ 18407 : 가로 블록 쌓기  (0) 2024.11.26
BOJ 11962 : Counting Haybales  (0) 2024.11.24
BOJ 2934 : LRH 식물  (0) 2024.11.23
BOJ 17353 : 하늘에서 떨어지는 1, 2, ..., R-L+1개의 별  (0) 2024.11.22