1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <cstdio> #include <cmath> #include <algorithm> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 5e5 + 10, M = 710; int n, m, st[M], id[N]; LL w[N], v[N], add[M]; int main () { read(n), read(m); int blk = sqrt(n); for (int i = 1; i <= n; i++) { read(w[i]), v[i] = w[i]; id[i] = i / blk; } for (int i = 0; i <= id[n]; i++) st[i] = i * blk; st[0] = 1, st[id[n] + 1] = n + 1; for (int i = 0; i <= id[n]; i++) sort(v + st[i], v + st[i + 1]); for (int op; m; m--) { read(op); if (op == 1) { int l, r, x; read(l), read(r), read(x); if (id[l] == id[r]) { for (int i = l; i <= r; i++) w[i] += x; for (int i = st[id[l]]; i < st[id[l] + 1]; i++) v[i] = w[i]; sort(v + st[id[l]], v + st[id[l] + 1]); continue; } for (int i = l; i < st[id[l] + 1]; i++) w[i] += x; for (int i = st[id[l]]; i < st[id[l] + 1]; i++) v[i] = w[i]; sort(v + st[id[l]], v + st[id[l] + 1]); for (int i = st[id[r]]; i <= r; i++) w[i] += x; for (int i = st[id[r]]; i < st[id[r] + 1]; i++) v[i] = w[i]; sort(v + st[id[r]], v + st[id[r] + 1]); for (int i = id[l] + 1; i < id[r]; i++) add[i] += x; } else if (op == 2) { int y; read(y); int l = 0, r = 0; for (int i = 0, t; !l && i <= id[n]; i++) if ((t = lower_bound(v + st[i], v + st[i + 1], y - add[i]) - v) ^ st[i + 1] && (v[t] + add[i] == y)) for (int j = st[i]; !l && j < st[i + 1]; j++) w[j] + add[i] == y && (l = j); for (int i = id[n], t; !r && ~i; i--) if ((t = lower_bound(v + st[i], v + st[i + 1], y - add[i]) - v) ^ st[i + 1] && (v[t] + add[i] == y)) for (int j = st[i + 1] - 1; !r && j >= st[i]; j--) w[j] + add[i] == y && (r = j); l ? (write(r - l), puts("")) : puts("-1"); } } return 0; }
|