結果

問題 No.743 Segments on a Polygon
ユーザー RTIA1227RTIA1227
提出日時 2018-10-05 23:19:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,668 bytes
コンパイル時間 1,394 ms
コンパイル使用メモリ 109,660 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-26 18:09:43
合計ジャッジ時間 2,860 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
9,216 KB
testcase_01 AC 98 ms
9,216 KB
testcase_02 AC 97 ms
9,216 KB
testcase_03 AC 100 ms
9,216 KB
testcase_04 AC 106 ms
9,216 KB
testcase_05 AC 101 ms
9,344 KB
testcase_06 AC 101 ms
9,216 KB
testcase_07 AC 103 ms
9,344 KB
testcase_08 AC 97 ms
9,344 KB
testcase_09 AC 51 ms
9,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>

using namespace std;

#define INF 1 << 29
#define LL long long int

LL const MOD = 1000000007;

template<typename T>
class SegmentTree{
    int n;
    int realsize;
    T initialvalue;
    vector<T> tree;

    public:
    
    SegmentTree(int size,T initial):realsize(size),initialvalue(initial){
        n = 1;
        while(n < size)n *= 2;

        tree = vector<T>(2*n + 1,initial);
    }

    //1-indexed
    void insert(int i,T data){
        int ind = n+i-1;
        tree[ind] = data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    void add(int i,T data){
        int ind = n+i-1;
        tree[ind] += data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    T get(int l,int r){
        if(l > realsize || r > realsize || l <= 0 || r <= 0)return initialvalue;
        l = n+l-1;
        r = n+r-1;
        if(l == r)return tree[l];
        int prel = l;
        int prer = r;
        T lnum = tree[l];
        T rnum = tree[r];
        l >>= 1;
        r >>= 1;
        while(l != r){
            if(l*2 == prel){
                lnum += tree[l*2+1];
            }
            if(r*2+1 == prer){
                rnum += tree[r*2];
            }
            prel = l;
            prer = r;
            l >>= 1;
            r >>= 1;
        }
        return lnum+rnum;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    LL n,m;
    cin >> n >> m;

    vector<LL> c(n);
    vector<pair<LL,LL>> a(n),b(n);
    for(int i = 0; i < n; i++){
        LL x,y;
        cin >> x >> y;
        x++;
        y++;
        if(x > y){
            swap(x,y);
        }
        a[i] = make_pair(x,(LL)i);
        b[i] = make_pair(y,(LL)i);
        c[i] = y;
    }

    sort(a.begin(),a.end());
    sort(b.begin(),b.end());

    LL t = 0;
    SegmentTree<LL> fr(n+1,0);

    for(int i = 0; i < n; i++){
        t -= fr.get(upper_bound(b.begin(),b.end(),make_pair(c[a[i].second],(LL)INF)) - b.begin(),n+1);
        LL x = (LL)(upper_bound(b.begin(),b.end(),make_pair(a[i].first,(LL)0)) - b.begin());
        if(x != 0){
            t -= fr.get(1,x);
        }
        fr.add(lower_bound(b.begin(),b.end(),make_pair(c[a[i].second],(LL)0)) - b.begin() + 1,1);
    }
    
    cout << (n*(n-1))/2 + t << endl;
    
    return 0;
}
0