답안 #203387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203387 2020-02-20T12:46:06 Z Bruteforceman Fire (JOI20_ho_t5) C++11
100 / 100
475 ms 47048 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(int i = 1; i <= q; i++) {
		printf("%lld\n", res[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 13 ms 14584 KB Output is correct
3 Correct 13 ms 14456 KB Output is correct
4 Correct 14 ms 14584 KB Output is correct
5 Correct 14 ms 14584 KB Output is correct
6 Correct 13 ms 14584 KB Output is correct
7 Correct 14 ms 14712 KB Output is correct
8 Correct 13 ms 14456 KB Output is correct
9 Correct 14 ms 14456 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 14584 KB Output is correct
13 Correct 14 ms 14584 KB Output is correct
14 Correct 13 ms 14584 KB Output is correct
15 Correct 14 ms 14456 KB Output is correct
16 Correct 14 ms 14456 KB Output is correct
17 Correct 13 ms 14584 KB Output is correct
18 Correct 14 ms 14584 KB Output is correct
19 Correct 13 ms 14456 KB Output is correct
20 Correct 13 ms 14584 KB Output is correct
21 Correct 13 ms 14456 KB Output is correct
22 Correct 13 ms 14456 KB Output is correct
23 Correct 14 ms 14456 KB Output is correct
24 Correct 13 ms 14456 KB Output is correct
25 Correct 13 ms 14456 KB Output is correct
26 Correct 14 ms 14584 KB Output is correct
27 Correct 14 ms 14456 KB Output is correct
28 Correct 15 ms 14584 KB Output is correct
29 Correct 13 ms 14456 KB Output is correct
30 Correct 13 ms 14460 KB Output is correct
31 Correct 14 ms 14456 KB Output is correct
32 Correct 14 ms 14584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 342 ms 38100 KB Output is correct
3 Correct 316 ms 37608 KB Output is correct
4 Correct 337 ms 38400 KB Output is correct
5 Correct 329 ms 38060 KB Output is correct
6 Correct 321 ms 38128 KB Output is correct
7 Correct 319 ms 38636 KB Output is correct
8 Correct 320 ms 38256 KB Output is correct
9 Correct 336 ms 38416 KB Output is correct
10 Correct 318 ms 37636 KB Output is correct
11 Correct 334 ms 38684 KB Output is correct
12 Correct 308 ms 37512 KB Output is correct
13 Correct 334 ms 37956 KB Output is correct
14 Correct 315 ms 37900 KB Output is correct
15 Correct 322 ms 38124 KB Output is correct
16 Correct 311 ms 38000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 340 ms 37232 KB Output is correct
3 Correct 324 ms 36748 KB Output is correct
4 Correct 332 ms 37612 KB Output is correct
5 Correct 330 ms 36780 KB Output is correct
6 Correct 321 ms 37228 KB Output is correct
7 Correct 333 ms 37304 KB Output is correct
8 Correct 340 ms 37460 KB Output is correct
9 Correct 335 ms 37116 KB Output is correct
10 Correct 322 ms 36884 KB Output is correct
11 Correct 345 ms 37680 KB Output is correct
12 Correct 329 ms 37132 KB Output is correct
13 Correct 343 ms 37692 KB Output is correct
14 Correct 326 ms 37360 KB Output is correct
15 Correct 327 ms 37520 KB Output is correct
16 Correct 354 ms 37292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 451 ms 46164 KB Output is correct
2 Correct 452 ms 46256 KB Output is correct
3 Correct 452 ms 47048 KB Output is correct
4 Correct 455 ms 46188 KB Output is correct
5 Correct 437 ms 46316 KB Output is correct
6 Correct 444 ms 46284 KB Output is correct
7 Correct 441 ms 46904 KB Output is correct
8 Correct 450 ms 46692 KB Output is correct
9 Correct 444 ms 46316 KB Output is correct
10 Correct 460 ms 46660 KB Output is correct
11 Correct 454 ms 46568 KB Output is correct
12 Correct 475 ms 46696 KB Output is correct
13 Correct 445 ms 46436 KB Output is correct
14 Correct 440 ms 46600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 13 ms 14584 KB Output is correct
3 Correct 13 ms 14456 KB Output is correct
4 Correct 14 ms 14584 KB Output is correct
5 Correct 14 ms 14584 KB Output is correct
6 Correct 13 ms 14584 KB Output is correct
7 Correct 14 ms 14712 KB Output is correct
8 Correct 13 ms 14456 KB Output is correct
9 Correct 14 ms 14456 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 14584 KB Output is correct
13 Correct 14 ms 14584 KB Output is correct
14 Correct 13 ms 14584 KB Output is correct
15 Correct 14 ms 14456 KB Output is correct
16 Correct 14 ms 14456 KB Output is correct
17 Correct 13 ms 14584 KB Output is correct
18 Correct 14 ms 14584 KB Output is correct
19 Correct 13 ms 14456 KB Output is correct
20 Correct 13 ms 14584 KB Output is correct
21 Correct 13 ms 14456 KB Output is correct
22 Correct 13 ms 14456 KB Output is correct
23 Correct 14 ms 14456 KB Output is correct
24 Correct 13 ms 14456 KB Output is correct
25 Correct 13 ms 14456 KB Output is correct
26 Correct 14 ms 14584 KB Output is correct
27 Correct 14 ms 14456 KB Output is correct
28 Correct 15 ms 14584 KB Output is correct
29 Correct 13 ms 14456 KB Output is correct
30 Correct 13 ms 14460 KB Output is correct
31 Correct 14 ms 14456 KB Output is correct
32 Correct 14 ms 14584 KB Output is correct
33 Correct 357 ms 38180 KB Output is correct
34 Correct 355 ms 38060 KB Output is correct
35 Correct 352 ms 37904 KB Output is correct
36 Correct 359 ms 37936 KB Output is correct
37 Correct 362 ms 38116 KB Output is correct
38 Correct 370 ms 38188 KB Output is correct
39 Correct 339 ms 37944 KB Output is correct
40 Correct 387 ms 37928 KB Output is correct
41 Correct 340 ms 37952 KB Output is correct
42 Correct 351 ms 38124 KB Output is correct
43 Correct 349 ms 38008 KB Output is correct
44 Correct 338 ms 38544 KB Output is correct
45 Correct 350 ms 37676 KB Output is correct
46 Correct 328 ms 37928 KB Output is correct
47 Correct 336 ms 37512 KB Output is correct
48 Correct 330 ms 37256 KB Output is correct
49 Correct 351 ms 37732 KB Output is correct
50 Correct 334 ms 39016 KB Output is correct
51 Correct 340 ms 38092 KB Output is correct
52 Correct 332 ms 37736 KB Output is correct
53 Correct 347 ms 38008 KB Output is correct
54 Correct 349 ms 37880 KB Output is correct
55 Correct 337 ms 37844 KB Output is correct
56 Correct 348 ms 38132 KB Output is correct
57 Correct 326 ms 37624 KB Output is correct
58 Correct 359 ms 38592 KB Output is correct
59 Correct 342 ms 38100 KB Output is correct
60 Correct 316 ms 37608 KB Output is correct
61 Correct 337 ms 38400 KB Output is correct
62 Correct 329 ms 38060 KB Output is correct
63 Correct 321 ms 38128 KB Output is correct
64 Correct 319 ms 38636 KB Output is correct
65 Correct 320 ms 38256 KB Output is correct
66 Correct 336 ms 38416 KB Output is correct
67 Correct 318 ms 37636 KB Output is correct
68 Correct 334 ms 38684 KB Output is correct
69 Correct 308 ms 37512 KB Output is correct
70 Correct 334 ms 37956 KB Output is correct
71 Correct 315 ms 37900 KB Output is correct
72 Correct 322 ms 38124 KB Output is correct
73 Correct 311 ms 38000 KB Output is correct
74 Correct 340 ms 37232 KB Output is correct
75 Correct 324 ms 36748 KB Output is correct
76 Correct 332 ms 37612 KB Output is correct
77 Correct 330 ms 36780 KB Output is correct
78 Correct 321 ms 37228 KB Output is correct
79 Correct 333 ms 37304 KB Output is correct
80 Correct 340 ms 37460 KB Output is correct
81 Correct 335 ms 37116 KB Output is correct
82 Correct 322 ms 36884 KB Output is correct
83 Correct 345 ms 37680 KB Output is correct
84 Correct 329 ms 37132 KB Output is correct
85 Correct 343 ms 37692 KB Output is correct
86 Correct 326 ms 37360 KB Output is correct
87 Correct 327 ms 37520 KB Output is correct
88 Correct 354 ms 37292 KB Output is correct
89 Correct 451 ms 46164 KB Output is correct
90 Correct 452 ms 46256 KB Output is correct
91 Correct 452 ms 47048 KB Output is correct
92 Correct 455 ms 46188 KB Output is correct
93 Correct 437 ms 46316 KB Output is correct
94 Correct 444 ms 46284 KB Output is correct
95 Correct 441 ms 46904 KB Output is correct
96 Correct 450 ms 46692 KB Output is correct
97 Correct 444 ms 46316 KB Output is correct
98 Correct 460 ms 46660 KB Output is correct
99 Correct 454 ms 46568 KB Output is correct
100 Correct 475 ms 46696 KB Output is correct
101 Correct 445 ms 46436 KB Output is correct
102 Correct 440 ms 46600 KB Output is correct