結果

問題 No.789 範囲の合計
ユーザー beetbeet
提出日時 2019-02-08 21:35:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 1,000 ms
コード長 2,036 bytes
コンパイル時間 2,701 ms
コンパイル使用メモリ 217,884 KB
実行使用メモリ 45,408 KB
最終ジャッジ日時 2023-09-11 04:05:43
合計ジャッジ時間 7,404 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 398 ms
41,084 KB
testcase_03 AC 72 ms
15,684 KB
testcase_04 AC 367 ms
39,560 KB
testcase_05 AC 294 ms
41,716 KB
testcase_06 AC 313 ms
41,248 KB
testcase_07 AC 68 ms
15,432 KB
testcase_08 AC 203 ms
24,380 KB
testcase_09 AC 186 ms
22,680 KB
testcase_10 AC 443 ms
45,408 KB
testcase_11 AC 324 ms
39,908 KB
testcase_12 AC 318 ms
39,924 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


template<typename T>
vector<T> compress(vector<T> v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}

template<typename T>
map<T, Int> dict(const vector<T> &v){
  map<T, Int> res;
  for(Int i=0;i<(Int)v.size();i++)
    res[v[i]]=i;
  return res;
}


template<typename T> 
struct BIT{
  Int n;
  vector<T> bit;
  //1-indexed
  BIT():n(-1){}
  BIT(Int n_,T d):n(n_),bit(n_+1,d){}
  
  T sum(Int i){
    T s=bit[0];
    for(Int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }
  void add(Int i,T a){
    if(i==0) return;
    for(Int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }
  
  Int lower_bound(Int w){
    if(w<=0) return 0;
    Int x=0,r=1;
    while(r<n) r<<=1;
    for(Int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }
  
  T sum0(Int i){
    return sum(i+1);
  }
  void add0(Int i,T a){
    add(i+1,a);
  }

  T query(Int l,Int r){
    return sum(r-1)-sum(l-1);
  }

  T query0(Int l,Int r){
    return sum(r)-sum(l);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int q;
  cin>>q;
  vector<Int> ts(q),xs(q),ys(q);
  for(Int i=0;i<q;i++) cin>>ts[i]>>xs[i]>>ys[i];

  vector<Int> vs;
  vs.emplace_back(-1);
  vs.emplace_back(1e9+1);
  for(Int i=0;i<q;i++){
    vs.emplace_back(xs[i]-1);
    vs.emplace_back(xs[i]);
    vs.emplace_back(xs[i]+1);
    vs.emplace_back(ys[i]-1);
    vs.emplace_back(ys[i]);
    vs.emplace_back(ys[i]+1);
  }

  vs=compress(vs);
  auto mv=dict(vs);
  BIT<Int> bit(mv.size(),0);
  Int ans=0;
  for(Int i=0;i<q;i++){
    if(ts[i]==0){
      bit.add(mv[xs[i]],ys[i]);
    }
    if(ts[i]==1){
      ans+=bit.query(mv[xs[i]],mv[ys[i]+1]);
    }
  }
  cout<<ans<<endl;
  return 0;
}
0