結果

問題 No.619 CardShuffle
ユーザー pekempeypekempey
提出日時 2017-12-19 11:44:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 3,000 ms
コード長 3,718 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 89,408 KB
実行使用メモリ 10,640 KB
最終ジャッジ日時 2023-08-24 14:17:40
合計ジャッジ時間 7,249 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,116 KB
testcase_01 AC 3 ms
5,116 KB
testcase_02 AC 3 ms
5,168 KB
testcase_03 AC 3 ms
5,224 KB
testcase_04 AC 4 ms
5,224 KB
testcase_05 AC 3 ms
5,352 KB
testcase_06 AC 3 ms
5,156 KB
testcase_07 AC 3 ms
5,212 KB
testcase_08 AC 3 ms
5,116 KB
testcase_09 AC 3 ms
5,224 KB
testcase_10 AC 3 ms
5,096 KB
testcase_11 AC 4 ms
5,104 KB
testcase_12 AC 3 ms
5,168 KB
testcase_13 AC 3 ms
5,124 KB
testcase_14 AC 3 ms
5,096 KB
testcase_15 AC 3 ms
5,216 KB
testcase_16 AC 258 ms
9,244 KB
testcase_17 AC 255 ms
10,392 KB
testcase_18 AC 259 ms
9,456 KB
testcase_19 AC 251 ms
10,380 KB
testcase_20 AC 183 ms
8,268 KB
testcase_21 AC 191 ms
9,268 KB
testcase_22 AC 174 ms
10,640 KB
testcase_23 AC 188 ms
9,316 KB
testcase_24 AC 173 ms
10,392 KB
testcase_25 AC 123 ms
8,332 KB
testcase_26 AC 312 ms
9,344 KB
testcase_27 AC 317 ms
10,580 KB
testcase_28 AC 306 ms
9,580 KB
testcase_29 AC 312 ms
10,508 KB
testcase_30 AC 244 ms
8,548 KB
testcase_31 AC 3 ms
5,120 KB
testcase_32 AC 182 ms
8,216 KB
testcase_33 AC 258 ms
9,568 KB
testcase_34 AC 288 ms
9,364 KB
testcase_35 AC 172 ms
5,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

const int mod = 1e9 + 7;

struct Modint {
  int n;
  Modint(int n = 0) : n(n) {}
};

Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % mod); }
Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
Modint &operator*=(Modint &a, Modint b) { return a = a * b; }

const int N = 1 << 17;

struct ST {
  vector<Modint> dat;

  ST() : dat(N * 2, 1) {}

  void update(int k, Modint v) {
    dat[k + N] = v;
    for (int i = k + N >> 1; i > 0; i >>= 1) {
      dat[i] = dat[i * 2] * dat[i * 2 + 1];
    }
  }

  Modint query(int l, int r) {
    l += N;
    r += N;
    Modint ret = 1;
    while (l < r) {
      if (l & 1) ret *= dat[l++];
      if (r & 1) ret *= dat[--r];
      l >>= 1;
      r >>= 1;
    }
    return ret;
  }
};

struct STadd {
  vector<Modint> dat;

  STadd() : dat(N * 2, 0) {}

  void update(int k, Modint v) {
    dat[k + N] = v;
    for (int i = k + N >> 1; i > 0; i >>= 1) {
      dat[i] = dat[i * 2] + dat[i * 2 + 1];
    }
  }

  Modint query(int l, int r) {
    l += N;
    r += N;
    Modint ret = 0;
    while (l < r) {
      if (l & 1) ret += dat[l++];
      if (r & 1) ret += dat[--r];
      l >>= 1;
      r >>= 1;
    }
    return ret;
  }
};

int main() {
  int n;
  cin >> n;

  vector<string> c(n + 2);
  c[0] = "+";
  c[n + 1] = "+";
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  ST tree;
  for (int i = 1; i <= n; i++) {
    if (i % 2 == 1) {
      tree.update(i, c[i].front() - '0');
    }
  }
  
  int q;
  cin >> q;
  
  STadd tree2;

  set<int> st;
  for (int i = 0; i <= n + 1; i++) {
    if (c[i] == "+") {
      st.insert(i);
    }
  }
  for (auto it = st.begin(); next(it) != st.end(); it++) {
    int l = *it;
    int r = *next(it);
    tree2.update(l, tree.query(l, r));
  }

  auto change = [&](int k, string t) {
    if (c[k] == "+" && t == "*") {
      tree2.update(k, 0);
      st.erase(k);
      auto it = prev(st.lower_bound(k));
      int l = *it;
      int r = *next(it);
      tree2.update(l, tree.query(l, r));
    } else if (c[k] == "*" && t == "+") {
      st.insert(k);
      auto it = st.lower_bound(k);
      int l = *prev(it);
      int m = *it;
      int r = *next(it);
      tree2.update(l, tree.query(l, m));
      tree2.update(m, tree.query(m, r));
    }
    c[k] = t;
  };

  auto change_v = [&](int k, Modint v) {
    tree.update(k, v);
    auto it = prev(st.lower_bound(k));
    int l = *it;
    int r = *next(it);
    tree2.update(l, tree.query(l, r));
  };

  while (q--) {
    string type;
    int x, y;
    cin >> type >> x >> y;

    if (type == "!") {
      if (x % 2 == 1) {
        Modint a = tree.dat[x + N];
        Modint b = tree.dat[y + N];
        change_v(x, b);
        change_v(y, a);
      } else {
        string a = c[x]; 
        string b = c[y]; 
        change(x, b);
        change(y, a);
      }
    } else {
      Modint ret = 0;
      x--;
      y++;
      while (x != y) {
        auto it = st.lower_bound(x);
        if (x < *it) {
          int tmp = min(*it, y);
          ret += tree.query(x, tmp);
          x = tmp;
          continue;
        }
        
        auto it2 = prev(st.upper_bound(y));
        if (y > *it2) {
          ret += tree.query(*it2, y);
          y = *it2;
          continue;
        }

        ret += tree2.query(*it, *it2);
        break;
      }

      cout << ret.n << endl;
    }
  }
}

0