제출 #545692

#제출 시각아이디문제언어결과실행 시간메모리
545692BackNoobHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
100 / 100
1067 ms181224 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1) 
#define TASK "task"
#define sz(s) (int) (s).size()

using namespace std;
const int mxN = 2e6 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

struct item{
	int r , c , idx;
};

int n;
int q;
int a[mxN];
int lef[mxN];
int bit[mxN];
vector<item> query[mxN];
vector<int> val[mxN];
bool ans[mxN];

void update(int pos , int val)
{
	for(; pos <= n ; pos += pos & (-pos)) maximize(bit[pos] , val);
}
int getmax(int pos)
{
	int res = 0;
	for(; pos > 0 ; pos -= pos & -pos) maximize(res , bit[pos]);
	return res;
}


void solve()
{
	cin >> n >> q;
	for(int i = 1 ; i <= n ; i++) cin >> a[i];

	stack<int> s;
	for(int i = 1 ; i <= n ; i++) {
		while(sz(s) && a[s.top()] <= a[i]) s.pop();
		if(sz(s)) lef[i] = s.top();
		else lef[i] = 0;
		s.push(i);
	} 

	// for(int i = 1 ; i <= n ; i++) cout << lef[i] << ' ';
	// cout << endl;


	for(int i = 1 ; i <= q ; i++) {
		int l, r, c;
		cin >> l >> r >> c;
		query[l].pb({r , c , i});
	}

	for(int i = 1 ; i <= n ; i++) val[lef[i]].pb(i);
	for(int i = n ; i >= 1 ; i--) {
		for(auto it : val[i]) {
			update(it , a[i] + a[it]);
		}

		for(auto it : query[i]) {
			// cout << it.idx << endl;
			ans[it.idx] = (getmax(it.r) <= it.c);
		}
	}

	for(int i = 1 ; i <= q ; i++) cout << ans[i] << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(TASK".in" , "r" , stdin);
    //freopen(TASK".out" , "w" , stdout);
    
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
#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...