답안 #203436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203436 2020-02-20T16:15:41 Z Bruteforceman Fire (JOI20_ho_t5) C++17
100 / 100
487 ms 47176 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];
vector <int> g[maxn];
int sub[maxn];
long long pred[maxn];
long long pref[maxn];
long long res[maxn];
vector <int> h[maxn];

void eulerOrder(int x) {
	for(int i : g[x]) {
		eulerOrder(i);
		pred[i] = sub[x];
		sub[x] += sub[i]; 
	}
	sub[x] += 1;
}
struct BinaryIndexedTree {
	long long t[maxn]; 
	int size;
	void update(int x, long long val) {
		x += 1;
		for(int i = x; i <= size + 1; i += i & (-i)) {
			t[i] += val;
		}
	}
	long long sum(int x) {
		x = min(x, size);
		x += 1;
		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);  });
	for(int i = 1; i <= q; i++) {
		v[Q[i].l - 1].push_back(-i);
		v[Q[i].r].push_back(i);
	}
	for(int i = 1; i <= n; i++) {
		pref[i] = pref[i - 1] + s[i];
	}
	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);
	auto cost = [&] (int &x) {
		if(prev[x] == 0) return 0;
		else return s[prev[x]] - s[x];
	};
	BinaryIndexedTree A, B, C, D;
	A.size = B.size = C.size = D.size = n;
	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(auto j : v[i]) {
			int id = j;
			int t = Q[abs(id)].t;
			int last = *lower_bound(rmq.begin(), rmq.end(), i - t);
			long long add = 1LL * s[last] * (i - last) + pref[last];
			h[last].emplace_back(id);
			if(id < 0) res[-id] -= add;
			else res[id] += add;
		}
	}
	for(int i = 1; i <= n; i++) {
		int k = i;
		A.update(pred[k], 1LL * cost(k) * pred[k]);
		B.update(pred[k], 1LL * cost(k));
		C.update(pred[k] + sub[k], 1LL * cost(k) * (sub[k] + pred[k]));
		D.update(pred[k] + sub[k], 1LL * cost(k));	
		for(auto j : h[i]) {
			int id = j;
			int t = Q[abs(id)].t;
			long long add = 0;
			add += B.sum(t) * t - A.sum(t);
			add += C.sum(t) - D.sum(t) * t;
			if(id < 0) res[-id] -= add;
			else res[id] += add;
		}
	}
	for_each(res + 1, res + q + 1, [] (auto &i) { printf("%lld\n", i); } );
	return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:49: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:50: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:52: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 14 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 14 ms 14456 KB Output is correct
4 Correct 14 ms 14456 KB Output is correct
5 Correct 14 ms 14456 KB Output is correct
6 Correct 14 ms 14456 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
8 Correct 14 ms 14456 KB Output is correct
9 Correct 14 ms 14460 KB Output is correct
10 Correct 14 ms 14456 KB Output is correct
11 Correct 14 ms 14584 KB Output is correct
12 Correct 14 ms 14456 KB Output is correct
13 Correct 14 ms 14584 KB Output is correct
14 Correct 14 ms 14456 KB Output is correct
15 Correct 14 ms 14584 KB Output is correct
16 Correct 17 ms 14456 KB Output is correct
17 Correct 14 ms 14584 KB Output is correct
18 Correct 14 ms 14456 KB Output is correct
19 Correct 14 ms 14456 KB Output is correct
20 Correct 14 ms 14456 KB Output is correct
21 Correct 14 ms 14456 KB Output is correct
22 Correct 14 ms 14460 KB Output is correct
23 Correct 14 ms 14456 KB Output is correct
24 Correct 15 ms 14456 KB Output is correct
25 Correct 14 ms 14456 KB Output is correct
26 Correct 14 ms 14456 KB Output is correct
27 Correct 14 ms 14456 KB Output is correct
28 Correct 14 ms 14584 KB Output is correct
29 Correct 14 ms 14584 KB Output is correct
30 Correct 14 ms 14456 KB Output is correct
31 Correct 14 ms 14456 KB Output is correct
32 Correct 14 ms 14456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 327 ms 38124 KB Output is correct
3 Correct 323 ms 37608 KB Output is correct
4 Correct 341 ms 38448 KB Output is correct
5 Correct 325 ms 38060 KB Output is correct
6 Correct 338 ms 37916 KB Output is correct
7 Correct 329 ms 38636 KB Output is correct
8 Correct 329 ms 38380 KB Output is correct
9 Correct 331 ms 38416 KB Output is correct
10 Correct 326 ms 37736 KB Output is correct
11 Correct 339 ms 38504 KB Output is correct
12 Correct 334 ms 37512 KB Output is correct
13 Correct 343 ms 37876 KB Output is correct
14 Correct 324 ms 37896 KB Output is correct
15 Correct 328 ms 38128 KB Output is correct
16 Correct 311 ms 38128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 342 ms 37336 KB Output is correct
3 Correct 325 ms 36748 KB Output is correct
4 Correct 340 ms 37740 KB Output is correct
5 Correct 327 ms 36776 KB Output is correct
6 Correct 332 ms 37368 KB Output is correct
7 Correct 332 ms 37304 KB Output is correct
8 Correct 335 ms 37460 KB Output is correct
9 Correct 341 ms 37244 KB Output is correct
10 Correct 325 ms 36756 KB Output is correct
11 Correct 349 ms 37444 KB Output is correct
12 Correct 327 ms 37004 KB Output is correct
13 Correct 356 ms 37688 KB Output is correct
14 Correct 335 ms 37360 KB Output is correct
15 Correct 342 ms 37392 KB Output is correct
16 Correct 331 ms 37292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 466 ms 46360 KB Output is correct
2 Correct 446 ms 46284 KB Output is correct
3 Correct 469 ms 47176 KB Output is correct
4 Correct 471 ms 46184 KB Output is correct
5 Correct 451 ms 46316 KB Output is correct
6 Correct 449 ms 46392 KB Output is correct
7 Correct 469 ms 46788 KB Output is correct
8 Correct 459 ms 46692 KB Output is correct
9 Correct 470 ms 46576 KB Output is correct
10 Correct 464 ms 46532 KB Output is correct
11 Correct 467 ms 46444 KB Output is correct
12 Correct 487 ms 46820 KB Output is correct
13 Correct 467 ms 46436 KB Output is correct
14 Correct 480 ms 46620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 14 ms 14456 KB Output is correct
4 Correct 14 ms 14456 KB Output is correct
5 Correct 14 ms 14456 KB Output is correct
6 Correct 14 ms 14456 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
8 Correct 14 ms 14456 KB Output is correct
9 Correct 14 ms 14460 KB Output is correct
10 Correct 14 ms 14456 KB Output is correct
11 Correct 14 ms 14584 KB Output is correct
12 Correct 14 ms 14456 KB Output is correct
13 Correct 14 ms 14584 KB Output is correct
14 Correct 14 ms 14456 KB Output is correct
15 Correct 14 ms 14584 KB Output is correct
16 Correct 17 ms 14456 KB Output is correct
17 Correct 14 ms 14584 KB Output is correct
18 Correct 14 ms 14456 KB Output is correct
19 Correct 14 ms 14456 KB Output is correct
20 Correct 14 ms 14456 KB Output is correct
21 Correct 14 ms 14456 KB Output is correct
22 Correct 14 ms 14460 KB Output is correct
23 Correct 14 ms 14456 KB Output is correct
24 Correct 15 ms 14456 KB Output is correct
25 Correct 14 ms 14456 KB Output is correct
26 Correct 14 ms 14456 KB Output is correct
27 Correct 14 ms 14456 KB Output is correct
28 Correct 14 ms 14584 KB Output is correct
29 Correct 14 ms 14584 KB Output is correct
30 Correct 14 ms 14456 KB Output is correct
31 Correct 14 ms 14456 KB Output is correct
32 Correct 14 ms 14456 KB Output is correct
33 Correct 360 ms 38060 KB Output is correct
34 Correct 360 ms 38188 KB Output is correct
35 Correct 372 ms 37968 KB Output is correct
36 Correct 358 ms 37944 KB Output is correct
37 Correct 349 ms 37900 KB Output is correct
38 Correct 366 ms 38188 KB Output is correct
39 Correct 357 ms 37892 KB Output is correct
40 Correct 368 ms 37980 KB Output is correct
41 Correct 350 ms 37948 KB Output is correct
42 Correct 375 ms 38256 KB Output is correct
43 Correct 346 ms 38008 KB Output is correct
44 Correct 349 ms 38544 KB Output is correct
45 Correct 349 ms 37676 KB Output is correct
46 Correct 347 ms 37932 KB Output is correct
47 Correct 342 ms 37512 KB Output is correct
48 Correct 353 ms 37256 KB Output is correct
49 Correct 350 ms 37988 KB Output is correct
50 Correct 349 ms 39020 KB Output is correct
51 Correct 359 ms 38404 KB Output is correct
52 Correct 334 ms 37740 KB Output is correct
53 Correct 347 ms 38136 KB Output is correct
54 Correct 355 ms 38008 KB Output is correct
55 Correct 348 ms 37968 KB Output is correct
56 Correct 364 ms 38260 KB Output is correct
57 Correct 341 ms 37752 KB Output is correct
58 Correct 357 ms 38588 KB Output is correct
59 Correct 327 ms 38124 KB Output is correct
60 Correct 323 ms 37608 KB Output is correct
61 Correct 341 ms 38448 KB Output is correct
62 Correct 325 ms 38060 KB Output is correct
63 Correct 338 ms 37916 KB Output is correct
64 Correct 329 ms 38636 KB Output is correct
65 Correct 329 ms 38380 KB Output is correct
66 Correct 331 ms 38416 KB Output is correct
67 Correct 326 ms 37736 KB Output is correct
68 Correct 339 ms 38504 KB Output is correct
69 Correct 334 ms 37512 KB Output is correct
70 Correct 343 ms 37876 KB Output is correct
71 Correct 324 ms 37896 KB Output is correct
72 Correct 328 ms 38128 KB Output is correct
73 Correct 311 ms 38128 KB Output is correct
74 Correct 342 ms 37336 KB Output is correct
75 Correct 325 ms 36748 KB Output is correct
76 Correct 340 ms 37740 KB Output is correct
77 Correct 327 ms 36776 KB Output is correct
78 Correct 332 ms 37368 KB Output is correct
79 Correct 332 ms 37304 KB Output is correct
80 Correct 335 ms 37460 KB Output is correct
81 Correct 341 ms 37244 KB Output is correct
82 Correct 325 ms 36756 KB Output is correct
83 Correct 349 ms 37444 KB Output is correct
84 Correct 327 ms 37004 KB Output is correct
85 Correct 356 ms 37688 KB Output is correct
86 Correct 335 ms 37360 KB Output is correct
87 Correct 342 ms 37392 KB Output is correct
88 Correct 331 ms 37292 KB Output is correct
89 Correct 466 ms 46360 KB Output is correct
90 Correct 446 ms 46284 KB Output is correct
91 Correct 469 ms 47176 KB Output is correct
92 Correct 471 ms 46184 KB Output is correct
93 Correct 451 ms 46316 KB Output is correct
94 Correct 449 ms 46392 KB Output is correct
95 Correct 469 ms 46788 KB Output is correct
96 Correct 459 ms 46692 KB Output is correct
97 Correct 470 ms 46576 KB Output is correct
98 Correct 464 ms 46532 KB Output is correct
99 Correct 467 ms 46444 KB Output is correct
100 Correct 487 ms 46820 KB Output is correct
101 Correct 467 ms 46436 KB Output is correct
102 Correct 480 ms 46620 KB Output is correct