小y的序列
// Problem: 小y的序列// Contest: NowCoder// URL: Memory Limit: 524288 MB// Time Limit: 6000 ms// 2022-02-25 11:33:22// // Powered by CP Editor (namespace std;#define rep(i,l,r) for(int i=(l);i<=(r);i++)#define per(i,l,r) for(int i=(l);i>=(r);i--)#define ll long long#define pii pair#define mset(s,t) memset(s,t,sizeof(t))#define mcpy(s,t) memcpy(s,t,sizeof(t))#define fi first#define se second#define pb push_back#define all(x) (x).begin(),(x).end()#define SZ(x) ((int)(x).size())#define mp make_pairconst ll mod = 1e9 + 7;inline ll qmi (ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a%mod; a = a * a %mod; b >>= 1; } return ans;}inline int read () { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= (ch=='-'),ch= getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f?-x:x;}template void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x/10); putchar(x % 10 + '0');}inline ll sub (ll a, ll b) { return ((a - b ) %mod + mod) %mod;}inline ll add (ll a, ll b) { return (a + b) %mod;}inline ll inv (ll a) { return qmi(a, mod - 2);}const int N = 4e6 + 10;struct Node { int l, r; int minv, maxv; int dif;}t[N];int n, k;int a[N];inline void push_up (int p) { t[p].maxv = max (t[p << 1].maxv, t[(p << 1)+ 1].maxv); t[p].minv = min (t[p << 1].minv, t[(p << 1) + 1].minv);}inline void build (int p, int l, int r) { t[p].l = l, t[p].r = r; if (l == r) { t[p].l = l, t[p].r = r; t[p].maxv = t[p].minv = a[l]; return; } int mid = l + r>> 1; build(p * 2, l, mid); build(p * 2 + 1, mid + 1, r); push_up(p);}inline void modify (int p, int x, int v) { if (t[p].l <= x && t[p].r >= x) { t[p].maxv = t[p].minv = v; return; } int mid = (t[p].l + t[p].r) >> 1; if (x <= mid) modify (p << 1, x, v); else modify ((p << 1) + 1, x, v ); push_up (p);}inline pii query (int p, int l, int r) { if (t[p].l >= l && t[p].r <= r) { return mp(t[p].minv, t[p].maxv); } int val1= 0x3f3f3f3f, val2 = -0x3f3f3f3f; int mid = (t[p].l + t[p].r) >> 1; if (l <= mid) { pii temp = query(p << 1, l, r); val1 = min (val1, temp.fi); val2 = max(val2, temp.se); } if (r > mid) { pii temp = query((p <<1) + 1, l, r); val1 = min (val1, temp.fi); val2 = max(val2, temp.se); } return mp(val1, val2);}// bool check1 (int i, int mid) { // int maxv = query(1, i, mid, 0); // int minv = query (1, i, mid, 1); // return maxv - minv >= k;// }// bool check2 (int i, int mid) { // int maxv = query(1, i, mid, 0); // int minv = query (1, i, mid, 1); // return maxv - minv <= k;// }// bool check3(int i, int mid) { // int maxv = query(1, i, mid, 0); // int minv = query (1, i, mid, 1); // return maxv - minv == k;// }void solve() { scanf("%d%d", & n, & k); rep(i, 1, n) scanf("%d", & a[i]); build(1, 1, n); ll res = 0; int l = 1, r1 = 1, r2 = 1; for ( ; l <= n; l ++ ) { pair qry = query(1, l, r1); while ( r1 <= n && qry.second - qry.first < k ) r1 ++, qry = query(1, l, r1); qry = query(1, l, r2); while ( r2 <= n && qry.second - qry.first <= k ) r2 ++, qry = query(1, l, r2); r2 --; if ( r1 <= n ) res += (ll)r2 - (ll)r1 + 1; else break; } printf("%lld\n", res);}int main () { int t; t =1; //cin >> t; while (t --) solve(); return 0;}
这一题要看到一个关键性质。固定一个左端,最大值和最小值的插值是递增的 用线段树O(Nlogn)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~