結果

問題 No.619 CardShuffle
ユーザー koprickykopricky
提出日時 2017-12-30 20:29:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 5,214 bytes
コンパイル時間 1,797 ms
コンパイル使用メモリ 165,020 KB
実行使用メモリ 16,580 KB
最終ジャッジ日時 2023-08-24 15:23:31
合計ジャッジ時間 6,585 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 122 ms
16,580 KB
testcase_17 AC 115 ms
15,956 KB
testcase_18 AC 119 ms
16,044 KB
testcase_19 AC 117 ms
15,400 KB
testcase_20 AC 107 ms
15,312 KB
testcase_21 AC 99 ms
15,904 KB
testcase_22 AC 97 ms
15,732 KB
testcase_23 AC 98 ms
15,576 KB
testcase_24 AC 98 ms
16,144 KB
testcase_25 AC 82 ms
15,636 KB
testcase_26 AC 133 ms
15,044 KB
testcase_27 AC 134 ms
15,612 KB
testcase_28 AC 135 ms
15,468 KB
testcase_29 AC 134 ms
16,128 KB
testcase_30 AC 122 ms
15,128 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 103 ms
15,668 KB
testcase_33 AC 120 ms
15,692 KB
testcase_34 AC 122 ms
15,632 KB
testcase_35 AC 50 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

ll add(ll x,ll y)
{
    return (x + y)%MOD;
}

ll sub(ll x,ll y)
{
    return (x+MOD-y)%MOD;
}

ll mul(ll x,ll y)
{
    return x*y%MOD;
}

struct monoid
{
    ll a,b,c;
    int type;
    monoid(){};
    monoid(ll x,ll y,ll z,int flag) : a(x), b(y), c(z), type(flag){};
    monoid operator*(const monoid& another){
        if(this->type == 2 || another.type == 2){
            if(this->type == 2){
                if(another.type == 2){
                    return monoid(0,0,0,2);
                }else{
                    return monoid(another.a,another.b,another.c,another.type);
                }
            }else{
                return monoid(this->a,this->b,this->c,this->type);
            }
        }
        if(this->type==0){
            if(another.type==0){
                return monoid(this->a,add(add(this->b,mul(this->c,another.a)),another.b),another.c,0);
            }else{
                return monoid(this->a,this->b,mul(this->c,another.a),0);
            }
        }else{
            if(another.type==0){
                return monoid(mul(this->a,another.a),another.b,another.c,0);
            }else{
                return monoid(mul(this->a,another.a),0,0,1);
            }
        }
    }
};

template<typename V> class segtree {
private:
    int n,sz;
    vector<V> node;
public:
    segtree(vector<V>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        rep(i,sz){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = node[i*2+1]*node[i*2+2];
        }
    }
    void update(int k,V a)
    {
    	k += n-1;
    	node[k] = a;
    	while(k>0){
    		k = (k-1)/2;
    		node[k] = node[2*k+1]*node[2*k+2];
    	}
    }
    V query(int a,int b,int k=0,int l=0,int r=-1)
    {
        if(r < 0) r = n;
    	if(r <= a || b <= l){
    		return monoid(0,0,0,2);
    	}
    	if(a <= l && r <= b){
    		return node[k];
    	}else{
    		V vl = query(a,b,2*k+1,l,(l+r)/2);
    		V vr = query(a,b,2*k+2,(l+r)/2,r);
    		return vl*vr;
    	}
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vs s(n);
    rep(i,n){
        cin >> s[i];
    }
    vector<monoid> vec(n);
    for(int i=1;i<n;i+=2){
        if(s[i] == "*"){
            vec[(i-1)/2] = monoid(stoi(s[i+1]),0,0,1);
        }else{
            vec[(i-1)/2] = monoid(1,0,stoi(s[i+1]),0);
        }
    }
    segtree<monoid> sg(vec);
    int q;
    cin >> q;
    rep(i,q){
        string t;
        int x,y;
        cin >> t >> x >> y;
        --x,--y;
        if(t == "!"){
            if(x % 2 == 1){
                monoid xx,yy;
                if(s[y] == "*"){
                    xx = monoid(stoi(s[x+1]),0,0,1);
                }else{
                    xx = monoid(1,0,stoi(s[x+1]),0);
                }
                if(s[x] == "*"){
                    yy = monoid(stoi(s[y+1]),0,0,1);
                }else{
                    yy = monoid(1,0,stoi(s[y+1]),0);
                }
                sg.update((x-1)/2,xx);
                sg.update((y-1)/2,yy);
            }else{
                monoid xx,yy;
                if(x != 0 && s[x-1] == "*"){
                    xx = monoid(stoi(s[y]),0,0,1);
                }else{
                    xx = monoid(1,0,stoi(s[y]),0);
                }
                if(y != 0 && s[y-1] == "*"){
                    yy = monoid(stoi(s[x]),0,0,1);
                }else{
                    yy = monoid(1,0,stoi(s[x]),0);
                }
                if(x != 0){
                    sg.update((x-1)/2,xx);
                }
                if(y != 0){
                    sg.update((y-1)/2,yy);
                }
            }
            swap(s[x],s[y]);
        }else{
            monoid res = sg.query(x/2,y/2);
            cout << add(mul(stoi(s[x]),res.a),add(res.b,res.c)) << "\n";
        }
    }
    return 0;
}
0