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 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 5e5 + 5; const int MAXLOG = 55;
inline void read(int& x) { x = 0; int f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } x *= f; }
inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); }
int n, m, tp, pre[MAXN]; struct Position { int a, b, ind; } pos[MAXN], st[MAXN];
struct PresidentTree { int tot, root[MAXN]; struct Node { int lch, rch, dat; } s[MAXN * MAXLOG]; void push_up(int p) { s[p].dat = s[s[p].lch].dat + s[s[p].rch].dat; } void update(int& p, int l, int r, int pos, int val) { s[++tot] = s[p], p = tot; if (l == r) { s[p].dat += val; return; } int mid = (l + r) >> 1; if (pos <= mid) update(s[p].lch, l, mid, pos, val); else update(s[p].rch, mid + 1, r, pos, val); push_up(p); } int query(int p, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) return s[p].dat; int mid = (l + r) >> 1, res = 0; if (ql <= mid) res += query(s[p].lch, l, mid, ql, qr); if (qr > mid) res += query(s[p].rch, mid + 1, r, ql, qr); return res; } } pret;
int main() { freopen("stack.in", "r", stdin); freopen("stack.out", "w", stdout);
read(n), read(m); for (int i = 1; i <= n; i++) { read(pos[i].a); } for (int i = 1; i <= n; i++) { read(pos[i].b); }
for (int i = 1; i <= n; i++) { pos[i].ind = i; while (tp && (st[tp].a == pos[i].a || st[tp].b <= pos[i].b)) tp--; pre[i] = st[tp].ind; st[++tp] = pos[i]; }
for (int i = 1; i <= n; i++) pret.update(pret.root[i] = pret.root[i - 1], 0, n, pre[i], 1); for (int i = 1, l, r; i <= m; i++) { read(l), read(r); int tmp = pret.query(pret.root[r], 0, n, 0, l - 1) - pret.query(pret.root[l - 1], 0, n, 0, l - 1); write(tmp), putchar('\n'); }
return 0; }
|