結果

問題 No.619 CardShuffle
ユーザー FF256grhyFF256grhy
提出日時 2019-09-21 04:12:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 569 ms / 3,000 ms
コード長 4,987 bytes
コンパイル時間 1,783 ms
コンパイル使用メモリ 180,796 KB
実行使用メモリ 7,872 KB
最終ジャッジ日時 2023-10-13 16:51:49
合計ジャッジ時間 14,606 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,356 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 516 ms
7,620 KB
testcase_17 AC 519 ms
7,700 KB
testcase_18 AC 503 ms
7,788 KB
testcase_19 AC 508 ms
7,872 KB
testcase_20 AC 414 ms
7,620 KB
testcase_21 AC 569 ms
7,664 KB
testcase_22 AC 557 ms
7,768 KB
testcase_23 AC 566 ms
7,644 KB
testcase_24 AC 567 ms
7,804 KB
testcase_25 AC 451 ms
7,804 KB
testcase_26 AC 444 ms
7,620 KB
testcase_27 AC 448 ms
7,760 KB
testcase_28 AC 432 ms
7,788 KB
testcase_29 AC 437 ms
7,760 KB
testcase_30 AC 383 ms
7,804 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 430 ms
7,676 KB
testcase_33 AC 517 ms
7,708 KB
testcase_34 AC 467 ms
7,744 KB
testcase_35 AC 196 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class SegTree {
private:
	T * a = NULL;
	int N = -1, S;
	function<T(T, T)> F;
	T I;
	bool is_available = false;

public:
	SegTree() { }
	SegTree(int n, function<T(T, T)> func, T id) { init(n, func, id); }
	
	// [0, n) が使用可能になる、func が演算、id は単位元
	void init(int size, function<T(T, T)> func, T id) {
		assert(size > 0);
		N = size;
		F = func;
		I = id;
		S = 1;
		while(S < size) { S *= 2; }
		delete[] a;
		a = new T[S * 2];
		inc(i, S * 2) { a[i] = I; }
		is_available = true;
	}
	
	// [p] の値を返す
	T operator[](int p) {
		assert(inID(p, 0, N));
		p += S;
		return a[p];
	}
	
	// [p] への参照を返す(calc() を呼ぶまで fold は使用不能になる)
	T & ref(int p) {
		is_available = false;
		assert(inID(p, 0, N));
		p += S;
		return a[p];
	}
	
	// テーブル全体を再計算
	void calc() {
		decID(i, 1, S) { a[i] = F(a[i * 2], a[i * 2 + 1]); }
		is_available = true;
	}
	
	// [p] に op を適用しテーブルを更新
	void apply(int p, function<void(T &)> op) {
		assert(inID(p, 0, N));
		p += S;
		op(a[p]);
		while(p != 1) {
			p /= 2;
			a[p] = F(a[p * 2], a[p * 2 + 1]);
		}
	}
	
	// [l, r) に f を適用した結果を返す
	T fold_ID(int l, int r, bool loop = false) {
		assert(is_available);
		assert(inII(l, 0, N));
		assert(inII(r, 0, N));
		if(loop && l >= r) { return F(fold_ID(l, N), fold_ID(0, r)); }
		assert(l <= r);
		l += S;
		r += S;
		
		T v = I, w = I;
		while(l < r) {
			if(l + 1 == r) { v = F(v, a[l]); break; }
			
			if(l % 2 == 1) { v = F(v, a[l]); }
			if(r % 2 == 1) { w = F(a[r - 1], w); }
			
			l = (l + 1) / 2;
			r = r / 2;
		}
		
		return F(v, w);
	}
	
	T fold_II(int l, int r, bool loop = false) { return fold_ID(l, r + 1, loop); }
};

#define OP(op) [&](auto a, auto b) { return op; }
#define AP(op) [&](auto & a) { op; }

// ---- ---- ---- ----

typedef array<LL, 4> A;

LL MOD = 1e9 + 7;
A F(A x, A y) {
	if(x[3] == -1) { return y; }
	if(y[3] == -1) { return x; }
	
	deque<LL> qx, qy;
	inc(i, 3) { if(x[i] != -1) { qx.PB(x[i]); } }
	inc(i, 3) { if(y[i] != -1) { qy.PB(y[i]); } }
	
	if(x[3] == 1) { // '*'
		LL vx = qx.back();  qx.pop_back();
		LL vy = qy.front(); qy.pop_front();
		qx.PB(vx * vy % MOD);
	}
	for(auto && e: qy) { qx.PB(e); }
	
	if(qx.size() == 1) {
		return { -1, qx[0], -1, y[3] };
	} else {
		LL xf = qx.front(); qx.pop_front();
		LL xb = qx.back();  qx.pop_back();
		LL sum = 0;
		for(auto && e: qx) { (sum += e) %= MOD; }
		
		return { xf, sum, xb, y[3] };
	}
}

LL val(A a) {
	LL sum = 0;
	inc(i, 3) { if(a[i] != -1) { (sum += a[i]) %= MOD; } }
	return sum;
}

int n, q;
SegTree<A> st;

int main() {
	cin >> n;
	vector<int> di;
	vector<int> op;
	inc(i, n) {
		string s;
		cin >> s;
		if(i % 2 == 0) { di.PB(s[0] - '0'); } else { op.PB(s[0] == '+' ? 0 : 1); }
	}
	
	st.init(di.size(), F, { 0, 0, 0, -1 });
	inc(i, di.size()) { st.ref(i) = { -1, di[i], -1, op[i] }; }
	st.calc();
	
	cin >> q;
	inc(i, q) {
		string s; int x, y;
		cin >> s >> x >> y;
		
		if(s == "!") {
			if(x % 2 == 1) { // 数字
				x /= 2;
				y /= 2;
				LL xx = st[x][1];
				LL yy = st[y][1];
				st.apply(x, AP(a[1] = yy));
				st.apply(y, AP(a[1] = xx));
			} else { // 演算子
				x = x / 2 - 1;
				y = y / 2 - 1;
				LL xx = st[x][3];
				LL yy = st[y][3];
				st.apply(x, AP(a[3] = yy));
				st.apply(y, AP(a[3] = xx));
			}
		} else {
			x /= 2;
			y /= 2;
			y++;
			cout << val(st.fold_ID(x, y)) << "\n";
		}
	}
	
	return 0;
}
0