結果

問題 No.619 CardShuffle
ユーザー hashiryohashiryo
提出日時 2019-06-02 19:17:24
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,333 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 179,904 KB
実行使用メモリ 7,564 KB
最終ジャッジ日時 2023-10-17 23:05:51
合計ジャッジ時間 9,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 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 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 201 ms
7,564 KB
testcase_17 AC 189 ms
7,564 KB
testcase_18 WA -
testcase_19 AC 186 ms
7,564 KB
testcase_20 AC 177 ms
7,564 KB
testcase_21 WA -
testcase_22 AC 118 ms
7,564 KB
testcase_23 WA -
testcase_24 AC 121 ms
7,564 KB
testcase_25 AC 104 ms
7,564 KB
testcase_26 AC 253 ms
7,564 KB
testcase_27 AC 245 ms
7,564 KB
testcase_28 WA -
testcase_29 AC 246 ms
7,564 KB
testcase_30 AC 238 ms
7,564 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 177 ms
7,564 KB
testcase_33 AC 196 ms
7,564 KB
testcase_34 AC 226 ms
7,564 KB
testcase_35 AC 137 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll> Pll;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/10;
const ll MOD = 1e9+7;


template <typename T>
struct SegmentTree{
    using F = function<T(T,T)>;
    int n;
    F f;
    T ti;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(F f,T ti):f(f),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 set_val(int k,T x){
        dat[k+=n]=x;
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
    }
    //[a,b)
    T query(int a,int b,int k=1,int l=0,int r=-1){
        if(r<0)r=n;
        if(r<=a||b<=l) return ti;
        if(a<=l&&r<=b) return dat[k];
        T vl=query(a,b,k*2,l,(l+r)/2);
        T vr=query(a,b,k*2+1,(l+r)/2,r);
        return f(vl,vr);
    }
    T operator[](const int &k) const {return dat[k + n];}
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;cin>>N;
  using hoge = tuple<bool,ll,ll,ll>;
  auto f=[](hoge a,hoge b){
    bool aop,bop;
    ll aa,ab,ac,ba,bb,bc;
    tie(aop,aa,ab,ac)=a;
    tie(bop,ba,bb,bc)=b;
    ll ca,cb,cc;
    ll d=ac*ba%MOD;
    if(ac<0){
      return b;
    }
    if(ba<0){
      return a;
    }
    if(bop){
      if(ab>=0){
        ca=aa;
        if(bb>=0){
          cb=(ab+d+bb);
        }else{
          cb=ab;
        }
      }else{
        ca=d;
        if(bb>=0){
          cb=bb;
        }else{
          cb=-d;
        }
      }
      if(bb>=0){
        cc=bc;
      }else{
        cc=d;
      }
    }else{
      ca=aa;
      cb=(ab+ac+ba+bb)%MOD;
      cc=bc;
    }
    if(cb>=0)ca%=MOD;
    if(cb>=0)cc%=MOD;
    if(cb>=0)cb%=MOD;
    return make_tuple(aop,ca,cb,cc);
  };
  SegmentTree<hoge> seg(f,make_tuple(0,-1,0,-1));
  seg.init((N+1)/2);
  vector<char> C(N);
  for(ll i=0;i<N;i++){
    cin>>C[i];
  }
  for(ll i=0;i<(N+1)/2;i++){
    ll c=C[2*i]-'0';
    bool flg = i&&C[2*i-1]=='*';
    seg.set_val(i,make_tuple(flg,c,-c,c));
  }
  ll Q;cin>>Q;
  for(ll q=0;q<Q;q++){
    char T;
    ll X,Y;cin>>T>>X>>Y;
    if(T=='!'){
      if(X&1){
        X/=2;Y/=2;
        ll x=C[2*X]-'0';
        bool flgx = X&&C[2*X-1]=='*';
        ll y=C[2*Y]-'0';
        bool flgy = Y&&C[2*Y-1]=='*';
        seg.set_val(X,make_tuple(flgx,y,-y,y));
        seg.set_val(Y,make_tuple(flgy,x,-x,x));
        swap(C[2*X],C[2*Y]);
      }else{
        X/=2;Y/=2;
        ll x=C[2*X]-'0';
        bool flgx = X&&C[2*X-1]=='*';
        ll y=C[2*Y]-'0';
        bool flgy = Y&&C[2*Y-1]=='*';
        seg.set_val(X,make_tuple(flgy,x,-x,x));
        seg.set_val(Y,make_tuple(flgx,y,-y,y));
        swap(C[2*X-1],C[2*Y-1]);
      }
    }else{
      X/=2;Y/=2;
      hoge a=seg.query(X,Y+1);
      /*
      debug(get<1>(a));
      debug(get<2>(a));
      debug(get<3>(a));
      */
      ll ans= get<1>(a)+get<2>(a)+get<3>(a);
      cout<<ans<<endl;
    }
  }
  return 0;
}
0