結果

問題 No.743 Segments on a Polygon
ユーザー mamekinmamekin
提出日時 2018-10-05 22:43:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,413 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 116,028 KB
実行使用メモリ 5,548 KB
最終ジャッジ日時 2023-08-09 02:40:44
合計ジャッジ時間 2,804 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
5,304 KB
testcase_01 AC 67 ms
5,304 KB
testcase_02 AC 68 ms
5,300 KB
testcase_03 AC 68 ms
5,440 KB
testcase_04 AC 67 ms
5,348 KB
testcase_05 AC 67 ms
5,348 KB
testcase_06 AC 67 ms
5,360 KB
testcase_07 AC 67 ms
5,548 KB
testcase_08 AC 65 ms
5,300 KB
testcase_09 AC 59 ms
5,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class BinaryIndexedTree
{
private:
    int n;
    vector<int> data;
public:
    BinaryIndexedTree(int n){ // コンストラクタ
        this->n = n;
        data.assign(n+1, 0);
    }
    void add(int k, int x){ // k番目の要素にxを加算する
        ++ k;
        while(k <= n){
            data[k] += x;
            k += k & -k;
        }
    }
    int sum(int k){ // 区間[0,k]の総和を返す
        ++ k;
        int ret = 0;
        while(k > 0){
            ret += data[k];
            k -= k & -k;
        }
        return ret;
    }
    int sum(int a, int b){ // 区間[a,b]の総和を返す
        return sum(b) - sum(a-1);
    }
    int upper_bound(int x){ // 総和が初めてxを超える位置を返す(ただし、各位置の数値が非負数であることを前提とする)
        int b = 1;
        while(b < n)
            b *= 2;
        int a = 0;
        while(b > 0){
            if(a+b <= n && x >= data[a+b]){
                x -= data[a+b];
                a += b;
            }
            b /= 2;
        }
        return (a < n)? a : -1;
    }
    int lower_bound(int x){ // 総和が初めてx以上になる位置を返す(ただし、各位置の数値が非負数であることを前提とする)
        return upper_bound(x-1);
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n);
    vector<int> v(m, -1);
    for(int i=0; i<n; ++i){
        cin >> a[i] >> b[i];
        v[a[i]] = v[b[i]] = i;
        if(a[i] > b[i])
            swap(a[i], b[i]);
    }

    BinaryIndexedTree bit(m);
    int cnt = 0;
    long long ans = 0;
    for(int i=0; i<m; ++i){
        if(v[i] == -1)
            continue;
        if(a[v[i]] == i){
            bit.add(i, 1);
        }
        else{
            bit.add(a[v[i]], -1);
            ans += bit.sum(a[v[i]], b[v[i]]);
        }
    }
    cout << ans << endl;

    return 0;
}
0