結果

問題 No.2338 Range AtCoder Query
ユーザー milanis48663220milanis48663220
提出日時 2023-06-03 00:08:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,583 ms / 4,000 ms
コード長 5,358 bytes
コンパイル時間 1,650 ms
コンパイル使用メモリ 141,540 KB
実行使用メモリ 33,796 KB
最終ジャッジ日時 2023-08-28 06:51:23
合計ジャッジ時間 30,418 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 846 ms
20,332 KB
testcase_12 AC 886 ms
21,816 KB
testcase_13 AC 891 ms
20,608 KB
testcase_14 AC 869 ms
20,492 KB
testcase_15 AC 1,019 ms
21,768 KB
testcase_16 AC 1,569 ms
31,360 KB
testcase_17 AC 1,515 ms
31,384 KB
testcase_18 AC 1,583 ms
31,420 KB
testcase_19 AC 1,571 ms
31,320 KB
testcase_20 AC 1,534 ms
31,488 KB
testcase_21 AC 1,530 ms
31,324 KB
testcase_22 AC 1,483 ms
31,312 KB
testcase_23 AC 1,485 ms
31,348 KB
testcase_24 AC 1,525 ms
31,320 KB
testcase_25 AC 1,544 ms
31,332 KB
testcase_26 AC 109 ms
26,048 KB
testcase_27 AC 104 ms
25,832 KB
testcase_28 AC 105 ms
25,828 KB
testcase_29 AC 1,543 ms
31,400 KB
testcase_30 AC 1,553 ms
31,352 KB
testcase_31 AC 544 ms
33,728 KB
testcase_32 AC 541 ms
33,796 KB
testcase_33 AC 202 ms
31,308 KB
testcase_34 AC 203 ms
31,328 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