#include #define ll long long #define F first #define Se second #define min(a,b) ((ab)?a:b) using namespace std; struct node { ll a,b,c,d;//or and max min node(){}; node(ll aa,ll bb,ll cc,ll dd){a=aa,b=bb,c=cc,d=dd;} }; node tree[400000]; void build(int idx, int start, int end,vector A) { if(start==end)tree[idx]=node(A[start],A[start],start,start); else { int mid = (start + end) / 2; build(2*idx, start, mid,A); build(2*idx+1, mid+1, end,A); tree[idx].a=tree[2*idx].a|tree[2*idx+1].a; tree[idx].b=tree[2*idx].b&tree[2*idx+1].b; tree[idx].c=(A[tree[2*idx].c]>A[tree[2*idx+1].c])?tree[2*idx].c:tree[2*idx+1].c; tree[idx].d=(A[tree[2*idx].d] A) { if(r < start or end < l) return node(-1,-1,-1,-1); if(l <= start and end <= r) return tree[idx]; int mid = (start + end) / 2; node p1 = query(2*idx, start, mid, l, r,A); node p2 = query(2*idx+1, mid+1, end, l, r,A); if(p1.a==-1 and p2.a==-1)return node(-1,-1,-1,-1); if(p1.a==-1)return p2; if(p2.a==-1)return p1; node p3; p3.a=p1.a|p2.a; p3.b=p1.b&p2.b; p3.c=(A[p1.c]>A[p2.c])?p1.c:p2.c; p3.d=(A[p1.c] costlyIntervals(int n, int k, vector A) { // Return a list of length n consisting of the answers int i; node c; ll cc; set> m; build(1,0,n-1,A); vector v(n); for(i=0;i<(int)v.size();i++)v[i]=-1; stack> s; pair p,q; s.push({0,n-1}); m.insert({0,n-1}); while(1) { if(s.empty())break; p=s.top(),s.pop(); c=query(1,0,n-1,p.F,p.Se,A); cc=c.a-c.b-A[c.c]+A[c.d]; if(cc>=k) for(i=p.F;i<=p.Se;i++)v[i]=p.Se-p.F+1; else { q={p.F,min(c.c,c.d)-1}; if(q.F<=q.Se and q.Se> n >> k; vector A(n); for(int A_i = 0; A_i < n; A_i++){ cin >> A[A_i]; } vector result = costlyIntervals(n, k, A); for (ssize_t i = 0; i < result.size(); i++) { cout << result[i] << (i != result.size() - 1 ? "\n" : ""); } cout << endl; return 0; }