結果

問題 No.743 Segments on a Polygon
ユーザー beetbeet
提出日時 2020-09-02 21:22:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,306 ms / 2,000 ms
コード長 2,249 bytes
コンパイル時間 2,892 ms
コンパイル使用メモリ 216,232 KB
実行使用メモリ 55,392 KB
最終ジャッジ日時 2024-05-01 16:18:44
合計ジャッジ時間 17,664 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,297 ms
55,268 KB
testcase_01 AC 1,297 ms
55,380 KB
testcase_02 AC 1,284 ms
55,352 KB
testcase_03 AC 1,288 ms
55,392 KB
testcase_04 AC 1,272 ms
55,216 KB
testcase_05 AC 1,289 ms
55,348 KB
testcase_06 AC 1,306 ms
55,236 KB
testcase_07 AC 1,299 ms
55,344 KB
testcase_08 AC 1,288 ms
55,328 KB
testcase_09 AC 344 ms
52,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/2063"

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

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename Key,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<Key> > val;
  vector<BIT> dat;
  RangeCount(){}
  RangeCount(int n):n(n){
    val.assign(n<<1,vector<Key>());
    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 &vs=val[i];
      sort(vs.begin(),vs.end());
      vs.erase(unique(vs.begin(),vs.end()),vs.end());
      dat.emplace_back(vs.size());
    }
  }
  void update(int a,Key x,T z){
    a+=n;
    while(a){
      auto &vs=val[a];
      int k=lower_bound(vs.begin(),vs.end(),x)-vs.begin();
      dat[a].add(k,z);
      a>>=1;
    }
  }
  T calc(int k,Key x,Key y){
    auto &vs=val[k];
    int p=lower_bound(vs.begin(),vs.end(),x)-vs.begin();
    int q=lower_bound(vs.begin(),vs.end(),y)-vs.begin();
    return dat[k].sum(q)-dat[k].sum(p);
  }
  // [a, b) * [x, y)
  T query(int a,int b,Key x,Key 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;
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
//END CUT HERE
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  const char newl = '\n';

  int n,m;
  cin>>n>>m;
  vector<int> as(n),bs(n);
  for(int i=0;i<n;i++) cin>>as[i]>>bs[i];

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

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

  cout<<ans<<newl;
  return 0;
}
0