// yukicoder: No.469 区間加算と一致検索の問題 // bal4u 2019.8.11 #include typedef unsigned long long ll; //// 入出力関係 #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { // 整数の入力 int n = 0, c = gc(); if (c == '-') { c = gc(); do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return -n; } do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return n; } void out(int n) { // 非負整数の表示(出力) int i; char b[20]; if (!n) pc('0'); else { i = 0; while (n) b[i++] = n % 10 + '0', n /= 10; while (i--) pc(b[i]); } pc('\n'); } //// ハッシュ関数 #define HASHSIZ 9999991 typedef struct { ll s; int id; } HASH; HASH hash[HASHSIZ+5], *hashend = hash + HASHSIZ; int lookup(ll s, int id) { HASH *p = hash + (int)(s % HASHSIZ); while (p->s) { if (p->s == s) return p->id; if (++p == hashend) p = hash; } return id; } void insert(ll s, int id) { HASH *p = hash + (int)(s % HASHSIZ); while (p->s) { if (p->s == s) return; if (++p == hashend) p = hash; } p->s = s, p->id = id; } //// 疑似乱数発生 #if 0 unsigned xorshift() { static unsigned y = 2463534242; y = y ^ (y << 13), y = y ^ (y >> 17), y = y ^ (y << 5); return y; } #endif ll phash[1000005]; const unsigned r = 723471715; int main() { int i, N, Q, x; ll s; N = in(), Q = in(); // r = xorshift(); phash[0] = 1; for (i = 1; i <= N; i++) phash[i] = phash[i-1] * r; s = 0; insert(s+1, 0); for (i = 1; i <= Q; i++) { x = gc(), gc(); if (x == '?') out(lookup(s+1, i)); else { int l = in(), r = in(), k = in(); s += (ll)(phash[r] - phash[l]) * k; insert(s+1, i); } } return 0; }