結果

問題 No.619 CardShuffle
ユーザー beetbeet
提出日時 2018-12-07 16:50:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 3,000 ms
コード長 3,671 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 209,728 KB
実行使用メモリ 6,372 KB
最終ジャッジ日時 2023-10-12 03:45:12
合計ジャッジ時間 5,952 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,356 KB
testcase_16 AC 87 ms
6,296 KB
testcase_17 AC 83 ms
6,276 KB
testcase_18 AC 84 ms
6,160 KB
testcase_19 AC 85 ms
6,220 KB
testcase_20 AC 81 ms
6,152 KB
testcase_21 AC 96 ms
6,144 KB
testcase_22 AC 92 ms
6,272 KB
testcase_23 AC 96 ms
6,212 KB
testcase_24 AC 95 ms
6,372 KB
testcase_25 AC 89 ms
6,156 KB
testcase_26 AC 70 ms
6,280 KB
testcase_27 AC 67 ms
6,152 KB
testcase_28 AC 70 ms
6,164 KB
testcase_29 AC 68 ms
6,164 KB
testcase_30 AC 65 ms
6,156 KB
testcase_31 AC 2 ms
4,356 KB
testcase_32 AC 77 ms
6,196 KB
testcase_33 AC 84 ms
6,260 KB
testcase_34 AC 64 ms
6,160 KB
testcase_35 AC 28 ms
4,356 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,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  int n;
  F f;
  G g;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,G g,T ti):f(f),g(g),ti(ti){}

  void init(int n_){    
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void update(int k,E a){
    k+=n;
    dat[k]=g(dat[k],a);
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  void set_val(int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
  }
  T query(int a,int b){
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};


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


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(long long 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) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{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;}

};

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  n=(n+1)/2;
  vector<int> v(n);
  vector<char> c(n);
  c[0]='+';
  for(int i=0;i<n;i++){
    if(i) cin>>c[i];
    cin>>v[i];
  }
  using M = Mint<int>;
  struct T{
    int t;
    M a,b,c;
    T(){}
    T(int t,M a,M b,M c):t(t),a(a),b(b),c(c){}
    bool operator==(const T &o)const{
      return t==o.t&&a==o.a&&b==o.b&&c==o.c;
    }
  };
  
  T ti(-1,0,0,0);
  auto f=[&](T a,T b){
           if(a==ti) return b;
           if(ti==b) return a;
           if(a.t==0) return T(b.t,a.a*b.a,b.b,b.c);
           if(b.t==0) return T(a.t,a.a,a.b,a.c*b.a);
           return T(a.t,a.a,a.b+a.c*b.a+b.b,b.c);
         };
  
  auto g=[&](T a,T b){a.t++;return b;};
  SegmentTree<T, T> seg(f,g,ti);

  vector<T> vt(n);
  for(int i=0;i<n;i++){
    if(c[i]=='*') vt[i]=T(0,v[i],0,0);
    if(c[i]=='+') vt[i]=T(1,1,0,v[i]);
  }
  seg.build(vt);
  
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    char t;
    int x,y;
    cin>>t>>x>>y;
    if(t=='!'){
      int k=x&1;
      x>>=1;y>>=1;
      if(k) swap(v[x],v[y]);
      else  swap(c[x],c[y]);
      for(int i:{x,y}){
        if(c[i]=='*') vt[i]=T(0,v[i],0,0);
        if(c[i]=='+') vt[i]=T(1,1,0,v[i]);
        seg.update(i,vt[i]);
      }
    }
    if(t=='?'){
      x>>=1;y>>=1;
      auto z=seg.query(x,y+1);
      if(c[x]=='*') cout<<(z.a+z.b+z.c).v<<"\n";
      if(c[x]=='+') cout<<(z.b+z.c).v<<"\n";
    }
  }
  cout<<flush;
  return 0;
}
0