結果

問題 No.743 Segments on a Polygon
ユーザー beetbeet
提出日時 2018-10-06 13:22:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,174 ms / 2,000 ms
コード長 1,948 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 179,608 KB
実行使用メモリ 65,776 KB
最終ジャッジ日時 2024-04-26 21:21:29
合計ジャッジ時間 14,788 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,138 ms
65,600 KB
testcase_01 AC 1,169 ms
65,748 KB
testcase_02 AC 1,174 ms
65,668 KB
testcase_03 AC 1,142 ms
65,776 KB
testcase_04 AC 1,145 ms
65,680 KB
testcase_05 AC 1,156 ms
65,648 KB
testcase_06 AC 1,158 ms
65,672 KB
testcase_07 AC 1,144 ms
65,540 KB
testcase_08 AC 1,137 ms
65,556 KB
testcase_09 AC 321 ms
63,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T>
struct RangeCount{
  struct BIT{
    vector<T> dat;
    BIT(){}
    BIT(int n){dat.assign(++n,0);}
    T sum(int k){
      T res=0;
      for(;k;k-=k&-k) res+=dat[k];
      return res;
    }
    void add(int k,T v){
      for(++k;k<(int)dat.size();k+=k&-k) dat[k]+=v;
    }    
  };
  int n;
  vector<vector<int> > val;
  vector<BIT> dat;
  RangeCount(){}
  RangeCount(int n_){
    n=1;
    while(n<n_) n<<=1;
    val.assign(n<<1,vector<int>());
    dat.reserve(n<<1);    
  }
  void preupdate(int a,int x){
    a+=n;
    while(a){
      val[a].emplace_back(x);
      a>>=1;
    }
  }
  void build(){    
    for(int i=0;i<n*2;i++){
      auto &v=val[i];
      sort(v.begin(),v.end());
      v.erase(unique(v.begin(),v.end()),v.end());
      dat.emplace_back(v.size());
    }
  }
  void update(int a,int x,int z){
    a+=n;
    while(a){
      auto &v=val[a];
      int k=lower_bound(v.begin(),v.end(),x)-v.begin();
      dat[a].add(k,z);
      a>>=1;
    }    
  }
  T calc(int k,int x,int y){
    auto &v=val[k];
    int p=lower_bound(v.begin(),v.end(),x)-v.begin();
    int q=lower_bound(v.begin(),v.end(),y)-v.begin();
    return dat[k].sum(q)-dat[k].sum(p);
  }
  // [a, b) * [x, y)
  T query(int a,int b,int x,int y){
    T res=0;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
      if(l&1) res+=calc(l++,x,y);
      if(r&1) res+=calc(--r,x,y); 
    }
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){  
  int n,m;
  scanf("%d %d",&n,&m);
  vector<int> a(n),b(n);
  for(int i=0;i<n;i++) scanf("%d %d",&a[i],&b[i]);

  RangeCount<int> seg(m);
  
  for(int i=0;i<n;i++){
    if(a[i]>b[i]) swap(a[i],b[i]);
    seg.preupdate(a[i],b[i]);
  }

  seg.build();
  
  long long ans=0;
  for(int i=0;i<n;i++){
    ans+=seg.query(0,a[i],a[i],b[i]);
    ans+=seg.query(a[i],b[i],b[i],m);
    seg.update(a[i],b[i],1);
  }

  printf("%lld\n",ans);
  return 0;
}
0