#include #include #include #include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) typedef long long ll; using namespace std; // rolling/zobrist hash const int L = 16; const int m[L] = { 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093, 1000000097, 1000000103, 1000000123, 1000000181, 1000000207, 1000000223, 1000000241, 1000000271, 1000000289, 1000000297 }; vector > generate_e(int n) { vector > e(n); int b[L]; random_device device; default_random_engine engine(device()); repeat (i,L) { uniform_int_distribution dist(0, m[i]-1); b[i] = dist(engine); } whole(fill, e[0], 1); repeat (i,n-1) repeat (j,L) e[i+1][j] = e[i][j] *(ll) b[j] % m[j]; return e; } array add(array const & a, array const & b) { array c; repeat (i,L) c[i] = (a[i] +(ll) b[i]) % m[i]; return c; }; array mul(array const & a, int b) { array c; repeat (i,L) c[i] = ((a[i] *(ll) b) % m[i] + m[i]) % m[i]; return c; }; int main() { int n, q; cin >> n >> q; assert (1 <= n and n <= 1000000); assert (1 <= q and q <= 100000); vector > e = generate_e(n); vector > acc(n+1); repeat (i,n) acc[i+1] = add(acc[i], e[i]); array x = {}; map, int> f; f[x] = 0; repeat (t,q) { char c; cin >> c; if (c == '!') { int l, r, k; cin >> l >> r >> k; assert (0 <= l and l < n); assert (l < r and r <= n); assert (- 100 <= k and k <= + 100); x = add(x, mul(add(acc[r], mul(acc[l], -1)), k)); if (not f.count(x)) f[x] = t+1; } else if (c == '?') { cout << f[x] << endl; } else { assert (false); } } return 0; }