結果

問題 No.2300 Substring OR Sum
ユーザー ococonomy1ococonomy1
提出日時 2023-05-12 21:50:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 7,555 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 119,172 KB
実行使用メモリ 26,108 KB
最終ジャッジ日時 2024-05-06 11:38:18
合計ジャッジ時間 5,029 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 93 ms
7,040 KB
testcase_04 AC 85 ms
6,912 KB
testcase_05 AC 105 ms
7,296 KB
testcase_06 AC 54 ms
5,376 KB
testcase_07 AC 368 ms
15,392 KB
testcase_08 AC 146 ms
9,084 KB
testcase_09 AC 110 ms
7,296 KB
testcase_10 AC 272 ms
12,824 KB
testcase_11 AC 323 ms
14,172 KB
testcase_12 AC 358 ms
14,832 KB
testcase_13 AC 115 ms
26,108 KB
testcase_14 AC 40 ms
5,376 KB
testcase_15 AC 148 ms
9,088 KB
testcase_16 AC 269 ms
12,832 KB
testcase_17 AC 339 ms
14,676 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <tuple>
#include <queue>
#include <bitset>
using namespace std;
using lg = long long;
#define TEST clog << "TEST" << endl
#define IINF 2147483647
#define LLINF 9223372036854775807LL
//#define AMARI 1000000007
#define AMARI 998244353
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'

class ococo_unionfind {
    //できること
    //点の挿入
    //その点の根を求める関数
    //辺の挿入
    //連結判定
    //島が何個あるかの出力
    //それぞれの島について、何個の点があるかの出力
public:
    ococo_unionfind(int n = 0) {
        for (int i = 0; i < n; i++)vinsert();
    }
    int simakosuu = 0;
    //g[i] = {その点の一個上の点,その点のrank}
    //その点のrank:その点の下に何個点があるか(上に何個あるかに変えた方がいいかも?)
    vector<pair<int, int>> g;

    //rs[i] = その点が含まれている連結成分に何個の点があるか
    //その連結成分の根について聞かないと返さない
    vector<int> rs;
    //点の挿入 O(1)
    void vinsert(void) {
        g.emplace_back(g.size(), 1);
        simakosuu++;
        rs.push_back(1);
    }
    //ある点の根を求める関数 O(α(N))
    int ne(int a) {
        if (g[a].first == a)return a;
        else {
            return g[a].first = ne(g[a].first);
        }
    }
    //辺の挿入 O(logN)
    void einsert(int a, int b) {
        if (a != b) {
            int a1 = ne(a), a2 = ne(b);
            if (a1 != a2) {
                simakosuu--;
                int rs12sum = rs[a1] + rs[a2];
                rs[a1] = rs12sum;
                rs[a2] = rs12sum;
                if (g[a1].second < g[a2].second) {
                    g[a1].first = a2;
                    g[a2].second = max(g[a1].second + 1, g[a2].second);
                }
                else {
                    g[a2].first = a1;
                    g[a1].second = max(g[a2].second + 1, g[a1].second);
                }
            }
        }
    }
    //2つのノードが繋がっているか判定する関数 O(α(N))
    bool renketucheck(int a, int b) {
        if (ne(a) == ne(b))return true;
        else return false;
    }
    //何個の島に分かれているか出力する関数 O(1)
    int islandnum(void) {
        return simakosuu;
    }
    //ある点について、その点が含まれている連結成分が何個の点を持つか返す関数 O(α(N))
    int islandsize(int a) {
        return rs[ne(a)];
    }
};


//あらかじめunion-findのコードを貼っておく
class ococo_namori_graph {
private:
public:
    int n;
    ococo_unionfind ngouf;
    vector<vector<int>> g, tree;
    vector<bool> is_loop, visited;
    vector<int> bubunki_oya;
    bool rk = false;
    //ループの成分を入れておく
    vector<int> loop_seibun;
    //ococo_namori_graph(n)でn頂点に設定する O(N)
    ococo_namori_graph(int N = 0) {
        n = N;
        g.resize(n);
        is_loop.resize(n);
        visited.resize(n);
        for (int i = 0; i < n; i++) {
            is_loop[i] = false;
            visited[i] = false;
        }
        bubunki_oya.resize(n);
        for (int i = 0; i < n; i++)ngouf.vinsert();
        rk = false;
    }

    //uとvを繋ぐ辺を追加する O(α(N)) 辺の挿入により閉路が決定した時のみO(N)
    void einsert(int u, int v) {
        if (!rk) {
            if (ngouf.renketucheck(u, v)) {
                rk = true;
                //uからBFSを行う
                //vを見つけたらその時の経路がloopになる
                //{その点,前の点}
                queue<pair<int, int>> que;
                que.push({ u,-1 });
                vector<int> mae(n, -1);
                bool flag = false;
                while (!que.empty()) {
                    pair<int, int> temp = que.front();
                    que.pop();
                    //clog << temp.first << endl;
                    for (int i = 0; i < g[temp.first].size(); i++) {
                        if (mae[g[temp.first][i]] != -1)continue;
                        que.push({ g[temp.first][i] ,temp.first });
                        mae[g[temp.first][i]] = temp.first;
                        if (g[temp.first][i] == v) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)break;
                }
                int temp = v;
                loop_seibun.push_back(temp);
                is_loop[temp] = true;
                while (temp != u) {
                    temp = mae[temp];
                    loop_seibun.push_back(temp);
                    is_loop[temp] = true;
                }
                tree.resize(loop_seibun.size());
            }
            ngouf.einsert(u, v);
        }
        g[u].push_back(v);
        g[v].push_back(u);
    }
    //閉路のそれぞれの点について、その点が親となる部分木を求める O(N)
    void tree_kettei(void) {
        if (loop_seibun.size() == 0)return;
        for (int i = 0; i < loop_seibun.size(); i++) {
            queue<int> que;
            que.push(loop_seibun[i]);
            bubunki_oya[loop_seibun[i]] = i;
            while (!que.empty()) {
                int temp = que.front();
                que.pop();
                for (int j = 0; j < g[temp].size(); j++) {
                    if (visited[g[temp][j]] || is_loop[g[temp][j]])continue;
                    visited[g[temp][j]] = true;
                    que.push(g[temp][j]);
                    bubunki_oya[g[temp][j]] = i;
                }
            }
        }
    }
};

#define MULTI_TEST_CASE false
void solve(void) {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> cnt(28, 0);
    vector<vector<int>> place(28);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        int temp = a[i];
        for (int j = 0; j < 28; j++) {
            if (temp % 2 == 1) {
                cnt[j]++;
                place[j].push_back(i);
            }
            temp /= 2;
        }
    }

    lg ans = 0;
    for (int i = 0; i < n; i++) {
        int temp = a[i];
        for (int j = 0; j < 28; j++) {
            if (temp % 2 == 1) {
                ans += (1LL << j) * (lg)(n - i);
                cnt[j]--;
            }
            else {
                if (lower_bound(place[j].begin(), place[j].end(), i) != place[j].end()) {
                    ans += (1LL << j) * (lg)(n - (int)*lower_bound(place[j].begin(), place[j].end(), i));
                }
            }
            temp /= 2;
        }
        //clog << ans << el;
    }
    cout << ans << el;
    return;
}

void calc(void) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)cin >> a[i];
    lg ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            ans += a[i] | a[j];
        }
        clog << ans << el;
    }
    cout << ans << el;
    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    if (MULTI_TEST_CASE)cin >> t;
    while (t--) {
        solve();
    }
    //calc();
    return 0;
}
0