結果

問題 No.469 区間加算と一致検索の問題
ユーザー しらっ亭しらっ亭
提出日時 2016-12-19 00:22:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 5,000 ms
コード長 3,241 bytes
コンパイル時間 1,830 ms
コンパイル使用メモリ 175,844 KB
実行使用メモリ 28,360 KB
最終ジャッジ日時 2023-08-23 13:18:13
合計ジャッジ時間 6,201 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,896 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 43 ms
6,384 KB
testcase_24 AC 44 ms
6,232 KB
testcase_25 AC 43 ms
6,276 KB
testcase_26 AC 43 ms
6,604 KB
testcase_27 AC 43 ms
5,904 KB
testcase_28 AC 43 ms
6,112 KB
testcase_29 AC 45 ms
6,428 KB
testcase_30 AC 44 ms
5,876 KB
testcase_31 AC 44 ms
6,368 KB
testcase_32 AC 44 ms
6,644 KB
testcase_33 AC 118 ms
28,360 KB
testcase_34 AC 116 ms
28,244 KB
testcase_35 AC 117 ms
28,296 KB
testcase_36 AC 117 ms
28,236 KB
testcase_37 AC 117 ms
28,248 KB
testcase_38 AC 103 ms
26,920 KB
testcase_39 AC 101 ms
26,868 KB
testcase_40 AC 101 ms
26,952 KB
testcase_41 AC 109 ms
26,856 KB
testcase_42 AC 105 ms
26,684 KB
testcase_43 AC 106 ms
26,596 KB
testcase_44 AC 103 ms
27,024 KB
testcase_45 AC 103 ms
26,808 KB
testcase_46 AC 102 ms
26,864 KB
testcase_47 AC 103 ms
26,668 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 107 ms
26,884 KB
testcase_51 AC 105 ms
27,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define fst first
#define snd second
using ll=long long;using pii=pair<int,int>;
using vb=vector<bool>;using vs=vector<string>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template<typename T>T read(){T t;cin>>t;return t;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){o<<'('<<p.fst<<", "<<p.snd<<')';return o;}

struct RollingHash64 {
  const int base = 1e9+7;
  const int N;
  vector<long long> P, H;

  /**
   * O(N)
   */
  RollingHash64(const vi & S) : N(S.size()), P(N+1, 1), H(N+1, 0) {
    for (int i = 0; i < N; i++) {
      int c = S[i];
      H[i+1] = H[i] * base + c;
      P[i+1] = P[i] * base;
    }
  }

  /**
   * S全体のハッシュ値を返す
   * O(1)
   */
  long long hash() const {
    return hash(0, N);
  }

  /**
   * S の [l, r) のハッシュ値を返す
   * O(1)
   */
  long long hash(int l, int r) const {
    return H[r] - H[l] * P[r - l];
  }

  /**
   * S の [l1, r1) と [l2, r2) のハッシュ値が等しいかを返す
   * O(1)
   */
  bool equals(int l1, int r1, int l2, int r2) const {
    if (l1 < 0 || l2 < 0) return false;
    if (r1 > N || r2 > N) return false;
    if (r1 - l1 != r2 - l2) return false;
    return hash(l1, r1) == hash(l2, r2);
  }

  /**
   * S の p1 からの len 文字と p2 からの len 文字が等しいかを返す
   * O(1)
   */
  bool equals(int p1, int p2, int len) const {
    return equals(p1, p1 + len, p2, p2 + len);
  }

  /**
   * S の pos 以降と other が等しいかを返す
   * O(1)
   */
  bool equals(int pos, const RollingHash64 &other) const {
    int r = pos + other.N;
    if (r >= N) return false;

    return hash(pos, r) == hash(0, other.N);
  }
};

void Main() {
  int n = read<int>();
  int Q = read<int>();

  vi A(n+1);
  RollingHash64 rh(A);

  map<ll, int> first;

  ll current = rh.hash();
  first[current] = 0;

  rep(q, Q) {
    char t = read<char>();
    if (t == '!') {
      int l = read<int>();
      int r = read<int>();
      int k = read<int>();

      A[l] += k;
      A[r] -= k;

      ll sl = rh.P[l] * k;
      ll sr = rh.P[r] * -k;
      current += sl;
      current += sr;
      if (first.count(current) == 0) {
        first[current] = q + 1;
      }
    }
    else {
      cout << first[current] << endl;
    }
    //cout << q << " : "; rep(ii,sz(A)) cout << ' ' << A[ii]; cout << endl;
  }
}
int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
0