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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<set> #include<bitset> #include<map>
#include<cstdio> #include <iomanip> #pragma GCC optimize(2) #define up(i,a,b) for(int i=a;i<b;i++) #define dw(i,a,b) for(int i=a;i>b;i--) #define upd(i,a,b) for(int i=a;i<=b;i++) #define dwd(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll; typedef unsigned long long ull; const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; ll read() { char ch = getchar(); ll x = 0, f = 1; while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } typedef pair<int, int> pir; #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lrt root<<1 #define rrt root<<1|1 const int N = 1e5 + 10; int cnt[N]; int ans[N]; int n, m; int hi[N]; struct tr { int maxx, pos; tr operator+(const tr temp)const { tr now = *this; tr res; if (now.maxx >= temp.maxx) { res.maxx = now.maxx; res.pos = now.pos; } else { res.maxx = temp.maxx; res.pos = temp.pos; } return res; } }tree[N<<2]; void update(int l, int r, int root,int pos,int val) { if (l == r) { tree[root].maxx = val; tree[root].pos = pos; return; } int mid = (l + r) >> 1; if (pos <= mid)update(lson, pos, val); else update(rson, pos, val); tree[root] = tree[lrt] + tree[rrt]; } tr query(int l, int r, int root, int L, int R) { if (L > R)return tr{ 0,0 }; if (L <= l && r <= R) { return tree[root]; } int mid = (l + r) >> 1; tr ans; tr anss; bool flag = 1; if (L <= mid) { flag = 0; ans = query(lson, L, R); } if (R > mid) { if (flag)ans = query(rson, L, R); else { anss = query(rson, L, R); if (ans.maxx < anss.maxx)ans = anss; } } return ans; } int solve(int l,int r,int d) { if (l > r)return -1; tr now = query(1, n, 1, l, r); cnt[now.pos] = d; ans[now.pos] = solve(l, now.pos - 1, d + 1) + 1; int anss = ans[now.pos], dd= 0; int pp = now.pos; while (pp < r) { tr temp = query(1, n, 1, pp + 1, r); if (temp.maxx == hi[pp]) { dd = solve(pp + 1, temp.pos - 1, d + 1); anss = max(dd + 1, anss); ans[temp.pos] = dd + 1; ans[pp] = max(ans[pp], dd + 1); cnt[temp.pos] = d; pp = temp.pos; } else break; } ans[pp] = max(ans[pp], solve(pp + 1, r, d + 1) + 1); anss = max(ans[pp], anss); return anss; } int main() { n = read(), m = read(); upd(i, 1, n)hi[i] = read(),update(1,n,1,i,hi[i]); solve(1, n, 1); int x, y; while (m--) { x = read(), y = read(); if (y == 0) { printf("%d\n", ans[x]); } else { if (hi[x] > hi[y])swap(x, y); if (y > x) { tr temp = query(1, n, 1, x, y - 1); if (temp.maxx >= hi[y])printf("%d\n", 0); else printf("%d\n", -cnt[y] + cnt[x]); } else { tr temp = query(1, n, 1, y + 1, x); if (temp.maxx >= hi[y])printf("%d\n", 0); else printf("%d\n", -cnt[y] + cnt[x]); } } } return 0; }
|