結果

問題 No.2338 Range AtCoder Query
ユーザー milanis48663220milanis48663220
提出日時 2023-06-02 22:40:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,942 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 140,220 KB
実行使用メモリ 145,712 KB
最終ジャッジ日時 2023-08-28 04:50:31
合計ジャッジ時間 10,269 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
137,828 KB
testcase_01 AC 107 ms
137,812 KB
testcase_02 AC 108 ms
137,728 KB
testcase_03 AC 108 ms
137,768 KB
testcase_04 AC 108 ms
137,820 KB
testcase_05 AC 108 ms
137,940 KB
testcase_06 AC 110 ms
137,968 KB
testcase_07 AC 110 ms
137,996 KB
testcase_08 AC 124 ms
137,760 KB
testcase_09 AC 122 ms
137,804 KB
testcase_10 AC 124 ms
137,816 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class Mo{
    public:
    vector<int> l, r;
    int bucket_size;
    Mo(int bucket_size): l(vector<int>()), r(vector<int>()), bucket_size(bucket_size) {}
    void add_query(int l_, int r_){
        l.push_back(l_);
        r.push_back(r_);
    }
    template<typename ADD_L, typename ADD_R, typename DEL_L, typename DEL_R, typename GET_ANS>
    vector<T> solve(ADD_L add_left, ADD_R add_right, DEL_L del_left, DEL_R del_right, GET_ANS get_ans){
        int sz = l.size();
        vector<int> ord(sz);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](const int a, const int b){
            const int c = l[a] / bucket_size, d = l[b] / bucket_size;
            return (c == d) ? ((c & 1) ? (r[b] < r[a]) : (r[a] < r[b])) : (c < d);
        });
        int li = 0, ri = 0;
        vector<T> ans(sz);
        for(const int i : ord){
            while(li > l[i]) add_left(--li);
            while(ri < r[i]) add_right(ri++);
            while(li < l[i]) del_left(li++);
            while(ri > r[i]) del_right(--ri);
            ans[i] = get_ans();
        }
        return ans;
    }
};

const int N = 200000;
using P = pair<bool, int>;

deque<P> dq[N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, q; cin >> n >> m >> q;
    vector<int> p(n);
    vector<bool> s(n);
    for(int i = 0; i < n; i++){
        int pp; cin >> pp; pp--;
        string ss; cin >> ss;
        p[i] = pp;
        s[i] = ss == "AC";
    }
    int ac = 0, penalty = 0;
    auto get_current_ac = [&](int idx){
        if(dq[idx].empty()) return 0;
        if(dq[idx].size() >= 2) return 1;
        return dq[idx][0].first ? 1 : 0;
    };
    auto get_current_penalty = [&](int idx){
        if(dq[idx].empty()) return 0;
        if(dq[idx][0].first) return 0;
        if(get_current_ac(idx) == 0) return 0;
        return dq[idx][0].second;
    };
    auto subtract_current = [&](int idx){
        ac -= get_current_ac(idx);
        penalty -= get_current_penalty(idx);
    };
    auto add_current = [&](int idx){
        ac += get_current_ac(idx);
        penalty += get_current_penalty(idx);
    };
    
    auto add_left = [&](int i){
        subtract_current(p[i]);
        if(dq[p[i]].empty()){
            dq[p[i]].push_front({s[i], 1});
        }else{
            auto [f, cnt] = dq[p[i]].front();
            if(f == s[i]){
                dq[p[i]].front().second++;
            }else{
                dq[p[i]].push_front({s[i], 1});
            }
        }
        add_current(p[i]);
    };

    auto add_right = [&](int i){
        subtract_current(p[i]);
        if(dq[p[i]].empty()){
            dq[p[i]].push_back({s[i], 1});
        }else{
            auto [f, cnt] = dq[p[i]].back();
            if(f == s[i]){
                dq[p[i]].back().second++;
            }else{
                dq[p[i]].push_back({s[i], 1});
            }
        }
        add_current(p[i]);
    };

    auto del_left = [&](int i){
        subtract_current(p[i]);
        dq[p[i]].front().second--;
        if(dq[p[i]].front().second == 0) dq[p[i]].pop_front();
        add_current(p[i]);
    };

    auto del_right = [&](int i){
        subtract_current(p[i]);
        dq[p[i]].back().second--;
        if(dq[p[i]].back().second == 0) dq[p[i]].pop_back();
        add_current(p[i]);
    };

    auto get_ans = [&](){
        return make_pair(ac, penalty);
    };

    int sq = sqrt(n);

    auto mo = Mo<pair<int, int>>(sq);
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r; l--;
        mo.add_query(l, r);
    }
    auto ans = mo.solve(add_left, add_right, del_left, del_right, get_ans);
    for(auto [a, p]: ans){
        cout << a << ' ' << p << endl;
    }
}
0