結果

問題 No.2338 Range AtCoder Query
ユーザー milanis48663220milanis48663220
提出日時 2023-06-03 00:08:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,120 ms / 4,000 ms
コード長 5,358 bytes
コンパイル時間 1,587 ms
コンパイル使用メモリ 143,328 KB
実行使用メモリ 33,944 KB
最終ジャッジ日時 2024-06-09 02:34:01
合計ジャッジ時間 22,471 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 595 ms
20,380 KB
testcase_12 AC 693 ms
21,692 KB
testcase_13 AC 708 ms
20,644 KB
testcase_14 AC 675 ms
20,384 KB
testcase_15 AC 816 ms
21,928 KB
testcase_16 AC 1,078 ms
31,552 KB
testcase_17 AC 936 ms
31,440 KB
testcase_18 AC 1,084 ms
31,664 KB
testcase_19 AC 1,085 ms
31,452 KB
testcase_20 AC 940 ms
31,568 KB
testcase_21 AC 1,059 ms
31,568 KB
testcase_22 AC 1,120 ms
31,440 KB
testcase_23 AC 1,070 ms
31,572 KB
testcase_24 AC 1,078 ms
31,440 KB
testcase_25 AC 1,074 ms
31,448 KB
testcase_26 AC 86 ms
25,940 KB
testcase_27 AC 70 ms
25,984 KB
testcase_28 AC 74 ms
25,804 KB
testcase_29 AC 920 ms
31,668 KB
testcase_30 AC 1,075 ms
31,572 KB
testcase_31 AC 468 ms
33,820 KB
testcase_32 AC 494 ms
33,944 KB
testcase_33 AC 166 ms
31,444 KB
testcase_34 AC 162 ms
31,576 KB
権限があれば一括ダウンロードができます

ソースコード

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>
#include <random>

std::random_device rnd;
std::mt19937 mt(rnd());
std::mt19937_64 mt_ll(rnd());

int randint(const int l, const int r){
    return mt()%(r - l) + l;
}

long long randll(const long long l, const long long r){
    return mt_ll()%(r - l) + l;
}


#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;
}

// https://take44444.github.io/Algorithm-Book/range/mo/main.html
int maxn;
ll hilbertorder(int x, int y) {
  ll rx, ry, d = 0;
  for (ll s=maxn>>1; s; s>>=1) {
    rx = (x & s)>0, ry = (y & s)>0;
    d += s * s * ((rx * 3) ^ ry);
    if (ry) continue;
    if (rx) {
      x = maxn-1 - x;
      y = maxn-1 - y;
    }
    swap(x, y);
  }
  return d;
}

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);
        int q = l.size();
        vector<ll> eval(q);
        for(int i = 0; i < q; i++) eval[i] = hilbertorder(l[i], r[i]);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](const int a, const int b){
            return eval[a] < eval[b];
        });
        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>;

const int inf = 1e9;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, q; cin >> n >> m >> q;
    maxn = 1;
    while(maxn < n ) maxn <<= 1;
    vector<vector<int>> idx(m);
    vector<int> p(n);
    vector<int> ord(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";
        ord[i] = idx[p[i]].size();
        idx[p[i]].push_back(i);
    }
    vector<vector<int>> next_ac(m);
    for(int i = 0; i < m; i++){
        next_ac[i].resize(idx[i].size()+1);
        int last = inf;
        next_ac[i][idx[i].size()] = inf;
        for(int j = idx[i].size()-1; j >= 0; j--){
            int ii = idx[i][j];
            if(s[ii]) {
                last = j;
            }
            next_ac[i][j] = last;
        }

    }
    vector<int> l_pos(m), r_pos(m);
    int ac = 0, penalty = 0;
    auto get_current_ac = [&](int pp){
        if(next_ac[pp][l_pos[pp]] < r_pos[pp]) return 1;
        return 0;
    };
    auto get_current_penalty = [&](int pp){
        if(get_current_ac(pp) == 0) return 0;
        return next_ac[pp][l_pos[pp]]-l_pos[pp];
    };
    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]);
        l_pos[p[i]]--;
        add_current(p[i]);
    };

    auto add_right = [&](int i){
        subtract_current(p[i]);
        r_pos[p[i]]++;
        add_current(p[i]);
    };

    auto del_left = [&](int i){
        subtract_current(p[i]);
        l_pos[p[i]]++;
        add_current(p[i]);
    };

    auto del_right = [&](int i){
        subtract_current(p[i]);
        r_pos[p[i]]--;
        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 << '\n';
    }
}
0