제출 #1343001

#제출 시각아이디문제언어결과실행 시간메모리
1343001tarjanmiciDeda (COCI17_deda)C++20
140 / 140
697 ms4508 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <climits>
#include <array>
using namespace std;
using ll = long long;
vector<int> t;
/*
void build(int ind, int tl, int tr) {
    if (tl == tr) t[ind] = a[tl];
    else {
        int tm = (tl + tr) / 2;
        build(ind * 2, tl, tm);
        build(ind * 2 + 1, tm + 1, tr);
        t[ind] = min(t[ind * 2], t[ind * 2 + 1]);
    }
}
*/
void update(int x, int val, int ind, int tl, int tr) {
    if (tl == tr) t[ind] = val;
    else {
        int tm = (tl + tr) / 2;
        if(x<=tm) update(x, val, ind * 2, tl, tm);
        else update(x, val, ind * 2 + 1, tm+1, tr);
        t[ind] = min(t[ind * 2], t[ind * 2 + 1]);
    }
}
int query(int l, int r, int tl, int tr, int ind) {
    if (tr<l || tl>r) return INT_MAX;
    if ((tl == tr) ||(l <= tl && r >= tr)) return t[ind];
    else {
        int tm = (tl + tr) / 2;
        return min(query(l, r, tl, tm, ind * 2), query(l, r, tm + 1, tr, ind * 2 + 1));
    }
}
int main() {
    ios::sync_with_stdio(false);    
    cin.tie(0);

    int n, q;
    cin >> n >>q;
    t.resize(4 * n + 1, INT_MAX);
    for (int i = 1; i <= q; i++) {
        char c;
        int a, b;
        cin >> c >> a >> b;
        if (c == 'M') update(b, a, 1, 1, n);
        else {
            int l=b, r=n;
            bool b = true;
            while (l < r) {
                int m = (l + r) / 2;
                if (query(l, m, 1, n, 1) <= a) r = m;
                else if (query(m + 1, r, 1, n, 1) <= a) l = m + 1;
                else {
                    b = false;
                    break;
                }
            }
            if (query(l, r, 1, n, 1) <= a && b) cout << l << '\n';
            else cout << "-1\n";
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...