結果

問題 No.877 Range ReLU Query
ユーザー tubuanntubuann
提出日時 2019-08-09 11:11:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,351 ms / 2,000 ms
コード長 4,606 bytes
コンパイル時間 3,029 ms
コンパイル使用メモリ 233,192 KB
実行使用メモリ 61,744 KB
最終ジャッジ日時 2023-08-08 04:11:23
合計ジャッジ時間 17,210 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 1,252 ms
59,972 KB
testcase_12 AC 1,093 ms
58,064 KB
testcase_13 AC 838 ms
36,656 KB
testcase_14 AC 900 ms
53,612 KB
testcase_15 AC 1,351 ms
61,376 KB
testcase_16 AC 1,249 ms
60,832 KB
testcase_17 AC 1,282 ms
61,708 KB
testcase_18 AC 1,202 ms
61,692 KB
testcase_19 AC 856 ms
58,996 KB
testcase_20 AC 1,198 ms
61,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}

#ifndef SEGMENT_TREE
#define SEGMENT_TREE
 
template<typename Data,typename Mono>
class SegTree{
private:
  typedef function<void(Data&,Data&)> MergeData;
  typedef function<void(Mono&,Mono&)> MergeMono;
  typedef function<void(Data&,Mono&)> Culc;
  typedef function<bool(Data&,Mono&,int&,int&)> UpdateQuery; //data,mono,left,right true::Data,Monoに作用する false::子に降りる
 
 
  int size,high;
  vector<int> left,right;
  vector<Data> dv;
  vector<Mono> mv;
   
  Data de;
  Mono me;
  MergeData df;
  MergeMono mf;
  Culc cf;
   
 
  inline bool nx(int &idx){
    while(idx&1){idx>>=1; if(idx){reculc(idx);}}
    return idx?idx|=1:idx;
  }
 
  inline void reculc(int idx){
    dv[idx]=dv[idx<<1];
    df(dv[idx],dv[(idx<<1)|1]);
    cf(dv[idx],mv[idx]);
  }
 
  inline void down(int idx){
    cf(dv[idx<<1],mv[idx]);
    mf(mv[idx<<1],mv[idx]);
    cf(dv[(idx<<1)|1],mv[idx]);
    mf(mv[(idx<<1)|1],mv[idx]);
    mv[idx]=me;
  }
 
public:
  SegTree(Data de,Mono me,MergeData df,MergeMono mf,Culc cf,int n=0):de(de),me(me),df(df),mf(mf),cf(cf){init(n);}
 
  void init(int n){
    size=1;
    high=0;
    while(size<n){size<<=1; high++;}
    dv.assign(size<<1,de);
    mv.assign(size<<1,me);
    left.resize(size<<1);
    right.resize(size<<1);
    for(int i=0;i<size;i++){left[size|i]=i; right[size|i]=i+1;}
    for(int i=size-1;i>0;i--){left[i]=left[i<<1]; right[i]=right[(i<<1)|1];}
  }
 
  void build(const vector<Data> &ary){
    int n=ary.size();
    init(n);
    for(int i=0;i<n;i++){dv[size|i]=ary[i];}
    for(int i=size-1;i>0;i--){dv[i]=dv[i<<1]; df(dv[i],dv[(i<<1)|1]);}
  }
 
  //[lf,rg)
  Data query(int lf,int rg){
    Data ret=de;
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){df(ret,dv[idx]); j&=nx(idx);}
      else{down(idx); idx<<=1;}
    }
    return ret;
  }
 
  void update(int lf,int rg,Mono m){
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){
        cf(dv[idx],m); mf(mv[idx],m); j&=nx(idx);
      }
      else{down(idx); idx<<=1;}
    }
  }
 
  void update(int lf,int rg,UpdateQuery br){
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){
        if(br(dv[idx],mv[idx],left[idx],right[idx])){j&=nx(idx);}
        else{down(idx); idx<<=1;}
      }
      else{down(idx); idx<<=1;}
    }
  }
 
  void DEBUG(){cout<<dv<<endl;}
};
 
#endif /*SEGMENT_TREE*/

int main(){
  ll n,q;
  cin>>n>>q;
  vector<ll> A(n);
  cin>>A;
  vector<pair<pll,ll>> Q(q);
  for(auto &I:Q){
    int c;
    cin>>c>>I;
  }
  map<ll,ll> M;
  for(auto &I:A){M[I]=0;}
  for(auto &I:Q){M[I.S]=0;}
  ll cnt=0;
  for(auto &I:M){I.S=cnt; cnt++;}

  vector<pair<pll,pll>> Q2;//idx,x,number,keisuu
  for(int i=0;i<q;i++){
    auto I=Q[i];
    I.F.F--; I.F.S--;
    if(I.F.F!=0){
      I.F.F--;
      Q2.push_back({{I.F.F,I.S},{i,-1}});
    }
    Q2.push_back({{I.F.S,I.S},{i,1}});
  }
  vector<ll> ans(q,0);
  sort(Q2.begin(),Q2.end());
  
  auto df=[](pll &a,pll &b)->void{a.F+=b.F; a.S+=b.S;};
  auto mf=[](ll &a,ll &b)->void{a+=b;};
  auto cf=[](pll &a,ll &b)->void{a.F+=b*a.S;};
  pll de={0,0};
  ll me=0;
  SegTree<pll,ll> Tr(de,me,df,mf,cf),Tr2(de,me,df,mf,cf);
  Tr.build(vector<pll>(cnt,{0,1}));
  Tr2.build(vector<pll>(cnt,{0,1}));

  int idx=0;
  for(int i=0;i<=n;i++){
    while(idx<(int)Q2.size() && Q2[idx].F.F<i){
      auto I=Q2[idx];
      ans[I.S.F]+=I.S.S*(Tr.query(M[I.F.S],cnt).F-Tr2.query(M[I.F.S],cnt).F*I.F.S);
      idx++;
    }
    if(i==n){break;}
    Tr.update(M[A[i]],M[A[i]]+1,A[i]);
    Tr2.update(M[A[i]],M[A[i]]+1,1);
  }
  for(auto &I:ans){cout<<I<<endl;}


  return 0;
}
0