結果

問題 No.743 Segments on a Polygon
ユーザー beetbeet
提出日時 2018-10-05 21:54:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,350 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 196,196 KB
実行使用メモリ 129,204 KB
最終ジャッジ日時 2024-04-20 17:11:43
合計ジャッジ時間 8,855 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds;
template <typename T>
using gtree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// usage:
// find_by_order(Int k): return the iterator of k-th smallest element (0-indexed)
// order_of_key(T key):  return the index of key in tree

//INSERT ABOVE HERE
signed main(){
  using T = gtree<Int>;
  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]);

  Int h=0;
  while((1<<h)<m) h++;
  vector<T> dat(2<<h);

  auto calc=
    [&](T &G,Int x,Int y)->Int{
      return G.order_of_key(y)-G.order_of_key(x);      
    };

  // [a, b) * [x, y)
  auto query=
    [&](Int a,Int b,Int x,Int y){
      Int res=0;
      for(Int l=a+m,r=b+m;l<r;l>>=1,r>>=1){
	if(l&1) res+=calc(dat[l++],x,y);
	if(r&1) res+=calc(dat[--r],x,y);	
      }
      return res;
    };

  auto update=
    [&](Int a,Int b){
      a+=m;
      while(a){
	dat[a].insert(b);
	a>>=1;
      }
    };

  Int ans=0;
  for(Int i=0;i<n;i++){
    if(a[i]>b[i]) swap(a[i],b[i]);
    ans+=query(0,a[i],a[i],b[i]);
    ans+=query(a[i],b[i],b[i],m);
    update(a[i],b[i]);
  }

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