結果

問題 No.469 区間加算と一致検索の問題
ユーザー beetbeet
提出日時 2018-11-04 14:27:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 301 ms / 5,000 ms
コード長 3,633 bytes
コンパイル時間 2,603 ms
コンパイル使用メモリ 224,656 KB
実行使用メモリ 67,448 KB
最終ジャッジ日時 2024-04-30 21:46:11
合計ジャッジ時間 10,343 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 14 ms
9,088 KB
testcase_03 AC 7 ms
6,940 KB
testcase_04 AC 8 ms
6,944 KB
testcase_05 AC 10 ms
6,940 KB
testcase_06 AC 11 ms
6,940 KB
testcase_07 AC 11 ms
6,944 KB
testcase_08 AC 12 ms
6,940 KB
testcase_09 AC 6 ms
6,940 KB
testcase_10 AC 10 ms
6,940 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 12 ms
6,940 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 10 ms
6,944 KB
testcase_18 AC 11 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 12 ms
6,944 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 79 ms
6,940 KB
testcase_24 AC 78 ms
6,940 KB
testcase_25 AC 78 ms
6,940 KB
testcase_26 AC 78 ms
6,944 KB
testcase_27 AC 78 ms
6,940 KB
testcase_28 AC 79 ms
6,940 KB
testcase_29 AC 78 ms
6,944 KB
testcase_30 AC 77 ms
6,940 KB
testcase_31 AC 77 ms
6,944 KB
testcase_32 AC 80 ms
7,040 KB
testcase_33 AC 297 ms
67,412 KB
testcase_34 AC 299 ms
67,384 KB
testcase_35 AC 298 ms
67,448 KB
testcase_36 AC 294 ms
67,384 KB
testcase_37 AC 301 ms
67,420 KB
testcase_38 AC 280 ms
66,036 KB
testcase_39 AC 280 ms
66,020 KB
testcase_40 AC 278 ms
66,000 KB
testcase_41 AC 279 ms
66,100 KB
testcase_42 AC 285 ms
66,048 KB
testcase_43 AC 281 ms
65,972 KB
testcase_44 AC 285 ms
65,976 KB
testcase_45 AC 284 ms
66,072 KB
testcase_46 AC 280 ms
66,044 KB
testcase_47 AC 281 ms
66,096 KB
testcase_48 AC 2 ms
6,940 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 282 ms
66,132 KB
testcase_51 AC 285 ms
65,984 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;}

template<typename T,T MOD = 1000000007>
struct Mint{
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(int k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }
  
  Mint inv(){return pow(MOD-2);}
  
  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}
  
  Mint operator+(Mint a){return Mint(v)+=a;};
  Mint operator-(Mint a){return Mint(v)-=a;};
  Mint operator*(Mint a){return Mint(v)*=a;};
  Mint operator/(Mint a){return Mint(v)/=a;};

  Mint operator-(){return v?MOD-v:v;}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  
  bool operator<(const Mint a)const{return v<a.v;}

  static vector<Mint> po,op;
  
  static void init(size_t n, T BASE){
    if(n+1<=po.size()) return;
    po.assign(n+1,1);
    op.assign(n+1,1);
    
    Mint x(BASE);
    Mint y=Mint(1)/x;
    for(int i=1;i<=(int)n;i++) po[i]=po[i-1]*x;
    for(int i=1;i<=(int)n;i++) op[i]=op[i-1]*y;
  }
};
template<typename T,T MOD>
vector<Mint<T, MOD> > Mint<T, MOD>::po = vector<Mint<T, MOD> >();
template<typename T,T MOD>
vector<Mint<T, MOD> > Mint<T, MOD>::op = vector<Mint<T, MOD> >();

template<typename T,typename M1,typename M2,typename M3>
struct PolynomialHash{
  using V = tuple<M1, M2, M3>;
  static const T BASE = 1777771;
  
  PolynomialHash(size_t n){
    M1::init(n, BASE);
    M2::init(n, BASE);
    M3::init(n, BASE);
  }

  V build(const vector<T> &v){
    int n=v.size();
    M1 r1;M2 r2;M3 r3;
    for(int i=0;i<n;i++){
      r1+=M1(v[i])*M1::po[i];
      r2+=M2(v[i])*M2::po[i];
      r3+=M3(v[i])*M3::po[i];
    }
    return V(r1,r2,r3);
  }

  V add(const V &a,const V &b){
    M1 a1=get<0>(a),b1=get<0>(b);
    M2 a2=get<1>(a),b2=get<1>(b);
    M3 a3=get<2>(a),b3=get<2>(b);
    return V(a1+b1,a2+b2,a3+b3);
  }
  
  V sub(const V &a,const V &b){
    M1 a1=get<0>(a),b1=get<0>(b);
    M2 a2=get<1>(a),b2=get<1>(b);
    M3 a3=get<2>(a),b3=get<2>(b);
    return V(a1-b1,a2-b2,a3-b3);
  }
  
  V mul(const V &a,const V &b){
    M1 a1=get<0>(a),b1=get<0>(b);
    M2 a2=get<1>(a),b2=get<1>(b);
    M3 a3=get<2>(a),b3=get<2>(b);
    return V(a1*b1,a2*b2,a3*b3);
  }
  
  V inv(const V &a){
    M1 a1;M2 a2;M3 a3;
    tie(a1,a2,a3)=a;
    return V(a1.inv(),a2.inv(),a3.inv());
  }

  V term(size_t x,T k){
    M1 a1=M1::po[x]*M1((long long)k);
    M2 a2=M2::po[x]*M2((long long)k);
    M3 a3=M3::po[x]*M3((long long)k);
    return V(a1,a2,a3);
  }
  
};

//INSERT ABOVE HERE
signed main(){
  int n,q;
  cin>>n>>q;
  using M1 = Mint<int, int(1e9+7)>;
  using M2 = Mint<int, int(1e9+9)>;
  using M3 = Mint<int, int(1e8+7)>;
  using V = PolynomialHash<int, M1, M2, M3>;  
  using T = V::V;
  V ph(n*2+100);
  
  vector<T> vs(n+1);
  vs[0]=ph.term(0,0);
  for(int i=0;i<n;i++)
    vs[i+1]=ph.add(vs[i],ph.term(i,1));

  T sum=vs[0];
  map<T, int> m;
  m[sum]=0;
  for(int i=0;i<q;i++){
    char c;
    cin>>c;
    if(c=='!'){
      int l,r,k;
      cin>>l>>r>>k;
      T di=ph.sub(vs[r],vs[l]);
      T sc=ph.term(0,k);
      sum=ph.add(sum,ph.mul(di,sc));
    }
    if(!m.count(sum)) m[sum]=i+1;
    if(c=='?') cout<<m[sum]<<endl;
  }
  return 0;
}
0