답안 #203367

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203367 2020-02-20T11:29:45 Z Bruteforceman Fire (JOI20_ho_t5) C++11
1 / 100
1000 ms 37500 KB
#include <bits/stdc++.h>
using namespace std;
#define prev previous
const int maxn = 200005;
using pii = pair <int, int>;

int n;
int s[maxn];
struct query {
	int l, r, t;
} Q[maxn];
vector <int> v[maxn];
int prev[maxn];
int l[maxn], r[maxn];
vector <int> g[maxn];
int sub[maxn];
long long t[maxn];
long long pred[maxn];

void eulerOrder(int x) {
	static int current = 0;
	l[x] = ++current;
	for(int i : g[x]) {
		eulerOrder(i);
		pred[i] = sub[x];
		sub[x] += sub[i]; 
	}
	sub[x] += 1;
	r[x] = current;
}
void update(int x, long long val) {
	for(int i = x; i <= n + 1; i += i & (-i)) {
		t[i] += val;
	}
}
long long sum(int x) {
	long long ans = 0;
	for(int i = x; i > 0; i -= i & (-i)) {
		ans += t[i];
	}
	return ans;
}

int main() {
	int q;
	scanf("%d %d", &n, &q);
	for_each(s + 1, s + n + 1, [] (int &i) { scanf("%d", &i); });
	for_each(Q + 1, Q + q + 1, [&] (query &i) { 
					scanf("%d %d %d", &i.t, &i.l, &i.r); 
					v[i.r].emplace_back(i.t);
					v[i.l - 1].emplace_back(i.t); });
	stack <int> st;
	for(int i = 1; i <= n; i++) {
		while(!st.empty() && s[st.top()] < s[i]) {
			st.pop();
		} 
		prev[i] = st.empty() ? 0 : st.top();
		st.push(i);
	}
	for(int i = 1; i <= n; i++) {
		g[prev[i]].push_back(i);
	}
	eulerOrder(0);
	map <pii, long long> res;
	auto cost = [&] (int x) {
		if(prev[x] == 0) return 0;
		else return s[prev[x]] - s[x];
	};
	vector <int> rmq;
	for(int i = 1; i <= n; i++) {
		while(!rmq.empty() && s[rmq.back()] < s[i]) {
			rmq.pop_back();
		}
		rmq.push_back(i);
		for(int t : v[i]) {
			int last = *lower_bound(rmq.begin(), rmq.end(), i - t);
			long long ans = 1LL * s[last] * (i - last);
			// cout << s[last] << endl;
			for(int k = 1; k <= last; k++) {
				int var = t - pred[k];
				var = min(var, sub[k]);
				var = max(var, 0);
				ans += 1LL * cost(k) * var;
				ans += s[k];
			}
			res[pii(i, t)] = ans;
		}
	}
	for_each(Q + 1, Q + q + 1, [&] (query i) { 
				printf("%lld\n", res[pii(i.r, i.t)] - res[pii(i.l - 1, i.t)]); });
	return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp: In lambda function:
ho_t5.cpp:47:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for_each(s + 1, s + n + 1, [] (int &i) { scanf("%d", &i); });
                                           ~~~~~^~~~~~~~~~
ho_t5.cpp: In lambda function:
ho_t5.cpp:49:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d %d %d", &i.t, &i.l, &i.r); 
      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9720 KB Output is correct
2 Correct 11 ms 9976 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 11 ms 9852 KB Output is correct
5 Correct 11 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 11 ms 9720 KB Output is correct
8 Correct 13 ms 9720 KB Output is correct
9 Correct 11 ms 9848 KB Output is correct
10 Correct 11 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 12 ms 9720 KB Output is correct
13 Correct 11 ms 9848 KB Output is correct
14 Correct 11 ms 9720 KB Output is correct
15 Correct 10 ms 9848 KB Output is correct
16 Correct 11 ms 9720 KB Output is correct
17 Correct 11 ms 9720 KB Output is correct
18 Correct 11 ms 9720 KB Output is correct
19 Correct 11 ms 9848 KB Output is correct
20 Correct 11 ms 9848 KB Output is correct
21 Correct 12 ms 9848 KB Output is correct
22 Correct 11 ms 9720 KB Output is correct
23 Correct 11 ms 9720 KB Output is correct
24 Correct 11 ms 9720 KB Output is correct
25 Correct 11 ms 9720 KB Output is correct
26 Correct 10 ms 9720 KB Output is correct
27 Correct 13 ms 9724 KB Output is correct
28 Correct 11 ms 9720 KB Output is correct
29 Correct 11 ms 9720 KB Output is correct
30 Correct 11 ms 9720 KB Output is correct
31 Correct 11 ms 9720 KB Output is correct
32 Correct 11 ms 9720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9720 KB Output is correct
2 Execution timed out 1087 ms 27284 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9720 KB Output is correct
2 Execution timed out 1092 ms 28896 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1094 ms 37500 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9720 KB Output is correct
2 Correct 11 ms 9976 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 11 ms 9852 KB Output is correct
5 Correct 11 ms 9720 KB Output is correct
6 Correct 11 ms 9720 KB Output is correct
7 Correct 11 ms 9720 KB Output is correct
8 Correct 13 ms 9720 KB Output is correct
9 Correct 11 ms 9848 KB Output is correct
10 Correct 11 ms 9720 KB Output is correct
11 Correct 10 ms 9720 KB Output is correct
12 Correct 12 ms 9720 KB Output is correct
13 Correct 11 ms 9848 KB Output is correct
14 Correct 11 ms 9720 KB Output is correct
15 Correct 10 ms 9848 KB Output is correct
16 Correct 11 ms 9720 KB Output is correct
17 Correct 11 ms 9720 KB Output is correct
18 Correct 11 ms 9720 KB Output is correct
19 Correct 11 ms 9848 KB Output is correct
20 Correct 11 ms 9848 KB Output is correct
21 Correct 12 ms 9848 KB Output is correct
22 Correct 11 ms 9720 KB Output is correct
23 Correct 11 ms 9720 KB Output is correct
24 Correct 11 ms 9720 KB Output is correct
25 Correct 11 ms 9720 KB Output is correct
26 Correct 10 ms 9720 KB Output is correct
27 Correct 13 ms 9724 KB Output is correct
28 Correct 11 ms 9720 KB Output is correct
29 Correct 11 ms 9720 KB Output is correct
30 Correct 11 ms 9720 KB Output is correct
31 Correct 11 ms 9720 KB Output is correct
32 Correct 11 ms 9720 KB Output is correct
33 Execution timed out 1095 ms 28288 KB Time limit exceeded
34 Halted 0 ms 0 KB -