제출 #650338

#제출 시각아이디문제언어결과실행 시간메모리
650338600MihneaFire (JOI20_ho_t5)C++17
1 / 100
1098 ms11704 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Op {
	int limit;
	int k;
	int id;
};

int main() {
#ifndef ONPC
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
	
	int n, q;
	cin >> n >> q;
	vector<ll> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	// l, r, t
	vector<ll> prn(2 * q, 0);
	vector<Op> ops;
	for (int i = 0; i < q; i++) {
		int l, r, t;
		cin >> t >> l >> r;
		l--, r--;
		ops.push_back({l - 1, t, i});
		ops.push_back({r, t, i + q});
	}
	// next bigger
	vector<int> s(n, (int) 1e9 + 7), f(n, -((int) 1e9 + 7));
	{
		vector<int> stk;
		for (int i = n - 1; i >= 0; i--) {
			while (!stk.empty() && a[stk.back()] <= a[i]) {
				stk.pop_back();
			}
			if (!stk.empty()) {
				s[i] = stk.back() - 1;
			}
			stk.push_back(i);
		}	
	}
	{
		vector<int> stk;
		for (int i = 0; i < n; i++) {
			while (!stk.empty() && a[stk.back()] < a[i]) {
				stk.pop_back();
			}
			if (!stk.empty()) {
				f[i] = stk.back() + 1;
			}
			stk.push_back(i);
		}	
	}
	ll h = 0;
	for (auto &it : ops) {
		int limit = it.limit;
		if (limit < 0) {
			//cout << "skip\n";
			continue;
		}
		int k = it.k;
		int id = it.id;
		ll sol = 0;
		int cnt = 0;
		for (int i = 0; i <= limit; i++) {
			// i <= rgh <= s[i]
			// f[i] <= rgh - k <= i
			// f[i] + k <= rgh <= i + k
			int q = max({i, f[i] + k});
			int w = min({s[i], i + k});
			if (q <= limit && q <= w) {
				if (w >= limit) {
					cnt++;
					w = limit;
				}
				sol += 1LL * a[i] * (w - q + 1);
				
			}
			// whenever this comes I make an assumption and later a correction
		}
		assert(cnt == 1);
		prn[id] = sol;
	}
	for (int i = 0; i < q; i++) {
		h = h * 777777 + prn[i];
		h = h * 777777 + prn[i + q];
		cout << prn[i + q] - prn[i] << "\n";
	}
#ifdef ONPC
	assert(h == -3878595637253783235);
	cout << "h = " << h << "\n";
#endif
	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...