提出 #70626512


ソースコード 拡げる

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;



using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)

const ll dy[] = {-1, 0, 0, 1};
const ll dx[] = {0, -1, 1, 0};

template <class T, class T1, class T2> bool isrange(T target, T1 low, T2 high) { return low <= target && target < high; }
template <class T, class U> T min(const T &t, const U &u) { return t < u ? t : u; }
template <class T, class U> T max(const T &t, const U &u) { return t < u ? u : t; }
template <class T, class U> bool chmin(T &t, const U &u) { if (t > u) { t = u; return true; } return false; }
template <class T, class U> bool chmax(T &t, const U &u) { if (t < u) { t = u; return true; } return false; }

// #include "titan_cpplib/others/print.cpp"

#include <iostream>
#include <vector>
#include <set>
#include <tuple>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;

// print

// -------------------------
// pair<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const pair<K, V>& p);
// tuple<T1, T2, T3>
template<typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t);
// tuple<T1, T2, T3, T4>
template<typename T1, typename T2, typename T3, typename T4> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t);
// vector<T>
template<typename T> ostream& operator<<(ostream& os, const vector<T>& a);
// vector<vector<T>>
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& a);
// array<T, 2>
template<typename T> ostream& operator<<(ostream& os, const array<T, 2>& a);
// set<T>
template<typename T> ostream& operator<<(ostream& os, const set<T>& s);
// multiset<T>
template<typename T> ostream& operator<<(ostream& os, const multiset<T>& s);
// unordered_set<T>
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& a);
// map<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const map<K, V>& mp);
// unordered_map<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const unordered_map<K, V>& mp);
// -------------------------

// color
static const string PRINT_RED = "\033[91m"; // 赤字
static const string PRINT_GREEN = "\033[92m"; // 緑字
static const string PRINT_BLUE = "\033[94m";  // 青字
static const string PRINT_NONE = "\033[m"; // 色を元に戻す
static const string PRINT_BOLD = "\u001b[1m"; // 太字

string to_red(const string &s)   { return PRINT_RED + s + PRINT_NONE; }
string to_green(const string &s) { return PRINT_GREEN + s + PRINT_NONE; }
string to_blue(const string &s)  { return PRINT_BLUE + s + PRINT_NONE; }
string to_bold(const string &s)  { return PRINT_BOLD + s + PRINT_NONE; }

string spacefill(const string s, const int f) {
    int n = s.size();
    string t;
    for (int i = 0; i < f-n; ++i) t += " ";
    t += s;
    return t;
}

string zfill(const string s, const int f) {
    int n = s.size();
    string t;
    for (int i = 0; i < f-n; ++i) t += "0";
    t += s;
    return t;
}

string bin(long long s) {
    string t;
    while (s) {
        t += (s & 1) ? '1' : '0';
        s >>= 1;
    }
    reverse(t.begin(), t.end());
    return t;
}

void DEBUG_LINE() { cerr << "[Line] : " << __LINE__ << std::endl; }

// pair<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const pair<K, V>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

// tuple<T1, T2, T3>
template<typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
    os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
    return os;
}

// tuple<T1, T2, T3, T4>
template<typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
    os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ")";
    return os;
}

// array<T, 2>
template<typename T>
ostream& operator<<(ostream& os, const array<T, 2>& a) {
    os << "[";
    for (int i = 0; i < (int)a.size(); ++i) {
        if (i > 0) os << ", ";
        os << a[i];
    }
    os << "]";
    return os;
}

// vector<T>
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a) {
    os << "[";
    for (int i = 0; i < (int)a.size(); ++i) {
        if (i > 0) os << ", ";
        os << a[i];
    }
    os << "]";
    return os;
}

// vector<vector<T>>
template<typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& a) {
    os << "[\n";
    int h = (int)a.size();
    for (int i = 0; i < h; ++i) {
        os << "  " << a[i] << '\n';
    }
    os << "]";
    return os;
}

// set<T>
template<typename T>
ostream& operator<<(ostream& os, const set<T>& s) {
    os << "{";
    auto it = s.begin();
    while (it != s.end()) {
        os << *it;
        ++it;
        if (it != s.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// multiset<T>
template<typename T>
ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << "{";
    auto it = s.begin();
    while (it != s.end()) {
        os << *it;
        ++it;
        if (it != s.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// unordered_set<T>
template<typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& a) {
    set<T> s(a.begin(), a.end());
    os << s;
    return os;
}

// map<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const map<K, V>& mp) {
    os << "{";
    auto it = mp.begin();
    while (it != mp.end()) {
        os << it->first << ": " << it->second;
        ++it;
        if (it != mp.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// unordered_map<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const unordered_map<K, V>& mp) {
    map<K, V> m(mp.begin(), mp.end());
    os << m;
    return os;
}


#include <vector>
#include <queue>
using namespace std;

// dijkstra
namespace titan23 {
/**
 * @brief dijkstra
 *
 * @tparam T weight type
 * @param G weighted graph
 * @param start start
 * @param INF inf
 * @return vector<T> dist from start
 */
template<typename T>
vector<T> dijkstra(
        const vector<vector<pair<int, T>>> &G,
        const int start,
        const T INF) {
    int n = G.size();
    vector<T> dist(n, INF);
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> hq;
    hq.emplace(0, start);
    dist[start] = 0;
    while (!hq.empty()) {
        auto [d, v] = hq.top();
        hq.pop();
        if (dist[v] < d) continue;
        for (const auto &[x, c] : G[v]) {
            if (dist[x] > d + c) {
                dist[x] = d + c;
                hq.emplace(dist[x], x);
            }
        }
    }
    return dist;
}
}  // namespace titan23


struct ST{
    long long value;
    int size;
};
using FT = long long;

ST op(ST a, ST b){ return {a.value+b.value, a.size+b.size}; }
ST e(){ return {0, 0}; }
ST mapping(FT f, ST x){ return {x.value + f*x.size, x.size}; }
FT composition(FT f, FT g){ return f+g; }
FT id(){ return 0; }
#include <iostream>
#include <vector>
// #include "titan_cpplib/data_structures/fenwick_tree.cpp"
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

// FenwickTree
namespace titan23 {

template<typename T>
struct FenwickTree {
    int _n, _s;
    vector<T> _tree;

    FenwickTree() {}

    FenwickTree(const int n) {
        _n = n;
        _s = 1 << (32 - __builtin_clz(_n));
        _tree.resize(n+1, 0);
    }

    FenwickTree(const vector<T> &a)
                : _n(a.size()),
                _s(1 << (32 - __builtin_clz(_n-1))),
                _tree(_n+1, 0) {
        for (int i = 1; i <= _n; ++i) _tree[i] = a[i-1];
        for (int i = 1; i < _n; ++i) {
            if (i + (i & (-i)) <= _n) {
                _tree[i + (i & (-i))] += _tree[i];
            }
        }
    }

    T pref(int r) const {
        T res = 0;
        while (r > 0) {
            res += _tree[r];
            r -= r & (-r);
        }
        return res;
    }

    T suff(int l) const {
        return pref(_n) - pref(l);
    }

    T sum(const int l, const int r) const {
        return pref(r) - pref(l);
    }

    void add(int k, const T x) {
        ++k;
        while (k <= _n) {
            _tree[k] += x;
            k += k & (-k);
        }
    }

    T get(const int k) const {
        return pref(k+1) - pref(k);
    }

    void set(const int k, const T x) {
        T pre = get(k);
        add(k, x-pre);
    }

    int bisect_left(T w) const {
        int i = 0, s = _s;
        while (s) {
            if (i+s <= _n && _tree[i+s] < w) {
                w -= _tree[i+s];
                i += s;
            }
            s >>= 1;
        }
        return (w ? i : -1);
    }

    int bisect_right(T w) const {
        int i = 0, s = _s;
        while (s) {
            if (i+s <= _n && _tree[i+s] <= w) {
                w -= _tree[i+s];
                i += s;
            }
            s >>= 1;
        }
        return i;
    }

    vector<T> tovector() const {
        vector<T> sub(_n+1), res(_n);
        for (int i = 0; i <= _n; ++i) sub[i] = pref(i);
        for (int i = 0; i < _n; ++i) res[i] = sub[i+1] - sub[i];
        return res;
    }

    void clear() {
        fill(_tree.begin(), _tree.end(), 0);
    }

    void print() const {
        vector<T> fw = tovector();
        cout << "[";
        for (int i = 0; i < _n-1; ++i) cout << fw[i] << ", ";
        cout << fw[_n-1] << "]\n";
    }
};
}  // namespace titan23

using namespace std;

namespace titan23 {

/// 定数倍の軽い range add point get
template<typename T>
class FenwickTreeRAQ {
private:
    int n;
    titan23::FenwickTree<T> fw;

public:
    FenwickTreeRAQ() : n(0), fw(0) {}
    FenwickTreeRAQ(int n) : n(n), fw(n) {}
    FenwickTreeRAQ(vector<T> a) : n(n), fw(a) {}

    /// all 0
    void clear() {
        fw.clear();
    }

    /// add x to a[l, r)
    void add_range(int l, int r, T x) {
        fw.add(l, x);
        if (r < n) fw.add(r, -x);
    }

    /// a[k] += x;
    void add(int k, T x) {
        add_range(k, k+1, x);
    }

    /// a[k]
    T get(int k) const {
        return fw.pref(k+1);
    }

    /// a[k] <- x
    void set(int k, T x) {
        T pre = get(k);
        add(k, x-pre);
    }

    vector<T> tovector() const {
        vector<T> res(n);
        for (int i = 0; i < n; ++i) {
            res[i] = get(i);
        }
    }

    friend ostream& operator<<(ostream& os, const titan23::FenwickTreeRAQ<T> &fw) {
        os << fw.tovector();
        return os;
    }
};
} // namespace titan23

void solve() {
    int n; cin >> n;
    string S; cin >> S;
    vector<vector<pair<int, int>>> G(n+1);
    int START = n;
    rep(i, n-1) {
        if (S[i] == 'L') {
            G[i+1].push_back({i, 1});
        } else {
            G[i].push_back({i+1, 1});
        }
    }
    vector<int> deg(n, 0);
    rep(i, n) {
        for (auto [x, c] : G[i]) {
            deg[x]++;
        }
    }
    rep(i, n) if (deg[i] == 0) {
        G[START].push_back({i, 0});
    }
    // vector<int> dist = titan23::dijkstra<int>(G, START, 1e9);


    vector<int> dp1(n+1, 0);
    auto dfs = [&] (auto &&dfs, int v) -> void {
        dp1[v] = 1;
        for (auto [x, c] : G[v]) {
            dfs(dfs, x);
            dp1[v] += dp1[x];
        }
    };
    dfs(dfs, START);

    vector<vector<pair<int, int>>> F(n+1);
    START = n;
    rep(i, n-1) {
        if (S[i] == 'R') {
            F[i+1].push_back({i, 1});
        } else {
            F[i].push_back({i+1, 1});
        }
    }
    vector<int> deg2(n, 0);
    rep(i, n) {
        for (auto [x, c] : F[i]) {
            deg2[x]++;
        }
    }
    rep(i, n) if (deg2[i] == 0) {
        F[START].push_back({i, 0});
    }
    vector<int> dp2(n+1, 0);
    auto efs = [&] (auto &&efs, int v) -> void {
        dp2[v] = 1;
        for (auto [x, c] : F[v]) {
            efs(efs, x);
            dp2[v] += dp2[x];
        }
    };
    efs(efs, START);

    // cerr << dp1 << endl;
    // cerr << dp2 << endl;
    // atcoder::lazy_segtree<ST, op, e, FT, mapping, composition, id> seg(n);
    titan23::FenwickTreeRAQ<int> fw(n+1);
    vector<int> U(n);
    rep(i, n) {
        int r = n-(dp1[i]-1);
        int l = dp2[i]-1;
        if (r <= l) continue;
        // for (int j = l; j < r; ++j) U[j]++;
        fw.add_range(l, r, 1);
    }
    // cerr << U << endl;
    rep(i, n) {
        cout << fw.get(i) << " ";
    }
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }

    return 0;
}

提出情報

提出日時
問題 F - Back and Forth Filling
ユーザ titan23
言語 C++23 (GCC 15.2.0)
得点 500
コード長 13520 Byte
結果 AC
実行時間 63 ms
メモリ 52324 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 69
セット名 テストケース
Sample sample_01.txt
All sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt, test_60.txt, test_61.txt, test_62.txt, test_63.txt, test_64.txt, test_65.txt, test_66.txt, test_67.txt, test_68.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 2 ms 3512 KiB
test_01.txt AC 1 ms 3448 KiB
test_02.txt AC 1 ms 3572 KiB
test_03.txt AC 1 ms 3520 KiB
test_04.txt AC 1 ms 3512 KiB
test_05.txt AC 1 ms 3628 KiB
test_06.txt AC 1 ms 3628 KiB
test_07.txt AC 1 ms 3448 KiB
test_08.txt AC 1 ms 3592 KiB
test_09.txt AC 1 ms 3448 KiB
test_10.txt AC 1 ms 3432 KiB
test_11.txt AC 2 ms 3572 KiB
test_12.txt AC 3 ms 3600 KiB
test_13.txt AC 5 ms 3448 KiB
test_14.txt AC 9 ms 3664 KiB
test_15.txt AC 19 ms 3448 KiB
test_16.txt AC 39 ms 3496 KiB
test_17.txt AC 56 ms 52168 KiB
test_18.txt AC 56 ms 52216 KiB
test_19.txt AC 47 ms 35516 KiB
test_20.txt AC 47 ms 35420 KiB
test_21.txt AC 56 ms 52220 KiB
test_22.txt AC 56 ms 52176 KiB
test_23.txt AC 56 ms 52260 KiB
test_24.txt AC 56 ms 52324 KiB
test_25.txt AC 47 ms 3628 KiB
test_26.txt AC 47 ms 3432 KiB
test_27.txt AC 50 ms 3684 KiB
test_28.txt AC 50 ms 3600 KiB
test_29.txt AC 50 ms 3728 KiB
test_30.txt AC 50 ms 3728 KiB
test_31.txt AC 52 ms 5452 KiB
test_32.txt AC 52 ms 5564 KiB
test_33.txt AC 62 ms 22172 KiB
test_34.txt AC 63 ms 22004 KiB
test_35.txt AC 62 ms 38872 KiB
test_36.txt AC 62 ms 38888 KiB
test_37.txt AC 62 ms 38876 KiB
test_38.txt AC 59 ms 40124 KiB
test_39.txt AC 58 ms 40716 KiB
test_40.txt AC 58 ms 41200 KiB
test_41.txt AC 57 ms 41452 KiB
test_42.txt AC 62 ms 41808 KiB
test_43.txt AC 61 ms 42100 KiB
test_44.txt AC 61 ms 42012 KiB
test_45.txt AC 57 ms 42284 KiB
test_46.txt AC 56 ms 42364 KiB
test_47.txt AC 55 ms 42748 KiB
test_48.txt AC 54 ms 42560 KiB
test_49.txt AC 55 ms 43768 KiB
test_50.txt AC 56 ms 44152 KiB
test_51.txt AC 57 ms 52028 KiB
test_52.txt AC 57 ms 48444 KiB
test_53.txt AC 57 ms 52324 KiB
test_54.txt AC 57 ms 49312 KiB
test_55.txt AC 56 ms 47272 KiB
test_56.txt AC 57 ms 47396 KiB
test_57.txt AC 57 ms 49632 KiB
test_58.txt AC 57 ms 49080 KiB
test_59.txt AC 56 ms 48096 KiB
test_60.txt AC 56 ms 48172 KiB
test_61.txt AC 55 ms 45632 KiB
test_62.txt AC 56 ms 48352 KiB
test_63.txt AC 54 ms 42652 KiB
test_64.txt AC 55 ms 42720 KiB
test_65.txt AC 59 ms 42336 KiB
test_66.txt AC 54 ms 42544 KiB
test_67.txt AC 60 ms 42160 KiB
test_68.txt AC 58 ms 40732 KiB