Submission #154504

#TimeUsernameProblemLanguageResultExecution timeMemory
154504muhammad_hokimiyonHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
0 / 100
1800 ms56956 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define fi first #define se second #define LL long long using namespace std; const int N = 1e6 + 7; const LL mod = 1e9 + 7; const int B = sqrt(1e5); int n,q; int a[N]; int l[N]; int r[N]; int k[N]; int ord[N]; int nxt[N]; int ans[N]; int t[4 * N]; stack < int > s; void add( int x , int l , int r , int pos , int xx ) { if( !( l <= pos && r >= pos ) )return; t[x] = max( t[x] , xx ); int m = (l + r) / 2; if( l == r )return; add(x * 2 , l , m , pos , x); add(x * 2 + 1 , m + 1 , r , pos , x); } int get( int x , int l , int r , int tl , int tr ) { if( l >= tl && r <= tr )return t[x]; if( l > tr || r < tl )return -1; int m = (l + r) / 2; return max( get(x * 2 , l , m , tl , tr ) , get( x * 2 + 1 , m + 1 , r , tl , tr) ); } bool cmp( int x , int y ){ return r[x] < r[y]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); cin >> n >> q; for( int i = 0; i < N; i++ ){ nxt[i] = -1; } for( int i = 0; i < 4 * N; i++ ){ t[i] = -1; } for( int i = 0; i < n; i++ ){ cin >> a[i]; } for( int i = 0; i < q; i++ ){ cin >> l[i] >> r[i] >> k[i]; l[i]--,r[i]--; ord[i] = i; } int cnt = -1; sort( ord , ord + q , cmp ); for( int i = 0; i < n; i++ ){ while( !s.empty() && a[s.top()] <= a[i] ){ s.pop(); } if( !s.empty() ){ nxt[i] = s.top(); } s.push(i); } for( int i = 0; i < q; i++ ){ int y = ord[i]; while( cnt < r[y] ){ cnt++; if( nxt[cnt] != -1 ){ add( 1 , 0 , n - 1 , nxt[cnt] , a[nxt[cnt]] + a[cnt] ); } } ans[i] = ( get( 1 , 0 , n - 1 , l[i] , r[i] ) <= k[i] ); } for( int i = 0; i < q; i++ ) cout << ans[i] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...