Submission #533775

# Submission time Handle Problem Language Result Execution time Memory
533775 2022-03-07T08:03:37 Z rk42745417 Fire (JOI20_ho_t5) C++17
100 / 100
461 ms 63140 KB
/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
	cerr << "\e[1;33m" << s << " = [";
	while(l != r) {
		cerr << *l;
		cerr << (++l == r ? ']' : ',');
	}
	cerr << "\e[0m\n";
}

#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS  = 1e-8;
const int INF     = 0x3F3F3F3F;
const ll LINF     = 4611686018427387903;
const int MOD     = 1e9+7;
static int Lamy_is_cute = []() {
	EmiliaMyWife
	return 48763;
}();
/*--------------------------------------------------------------------------------------*/

const int N = 2e5 + 25;
struct segtree {
	ll arr[N << 1], tag[N];
	int n;
	void init(int _n) { n = _n; }
	void upd(int p, ll v, int h) {
		arr[p] += v * (1LL << h);
		if(p < n)
			tag[p] += v;
	}
	void push(int p) {
		for(int h = __lg(p); ~h; h--) {
			int i = p >> h;
			upd(i, tag[i >> 1], h);
			upd(i ^ 1, tag[i >> 1], h);
			tag[i >> 1] = 0;
		}
	}
	void pull(int p) {
		for(int h = 1; p > 1; p >>= 1, h++)
			arr[p >> 1] = arr[p] + arr[p ^ 1] + tag[p >> 1] * (1LL << h);
	}
	void edt(int l, int r, int v) {
		if(l >= r)
			return;
		int tl = l + n, tr = r + n - 1, h = 0;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1, h++) {
			if(l & 1)
				upd(l++, v, h);
			if(r & 1)
				upd(--r, v, h);
		}
		pull(tl);
		pull(tr);
	}
	ll que(int l, int r) {
		if(l >= r)
			return 0;
		push(l + n);
		push(r + n - 1);
		ll res = 0;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if(l & 1)
				res += arr[l++];
			if(r & 1)
				res += arr[--r];
		}
		return res;
	}
} tree, tree2;

signed main() {
	int n, q;
	cin >> n >> q;
	tree.init(n);
	tree2.init(n);
	vector<int> arr(n), prv(n), nxt(n);
	for(int i = 0; i < n; i++)
		cin >> arr[i];

	{
		stack<int, vector<int>> st;
		for(int i = 0; i < n; i++) {
			while(!st.empty() && arr[st.top()] < arr[i]) {
				nxt[st.top()] = i;
				st.pop();
			}
			if(!st.empty())
				prv[i] = st.top();
			else
				prv[i] = -1;
			st.push(i);
		}
		while(!st.empty()) {
			nxt[st.top()] = n;
			st.pop();
		}
	}

	vector<vector<tuple<int, int, int>>> mods(n + 1), mods2(n + 1);
	auto add_triangle = [&](int l, int r, ll v) { // [l, r]
		mods[0].push_back({r + 1, n, -v});
		int tot = r - l + 1;
		if(tot <= n)
			mods[tot].push_back({r + 1, n, v});

		mods2[0].push_back({l, n, v});
		if(tot <= n)
			mods2[tot].push_back({l, n, -v});
	};

	for(int i = 0; i < n; i++) {
		if(~prv[i]) {
			add_triangle(prv[i] + 1, nxt[i] - 1, arr[i]);
			if(prv[i] + 1 < i)
				add_triangle(prv[i] + 1, i - 1, -arr[i]);
			if(i + 1 < nxt[i])
				add_triangle(i + 1, nxt[i] - 1, -arr[i]);
		}
		else {
			mods[0].push_back({i, nxt[i], arr[i]});
			if(i + 1 < nxt[i])
				add_triangle(i + 1, nxt[i] - 1, -arr[i]);
		}
	}

	vector<ll> ans(q);
	vector<vector<tuple<int, int, int>>> ques(n + 1);
	for(int i = 0; i < q; i++) {
		int t, l, r;
		cin >> t >> l >> r;
		ques[t].push_back({l - 1, r, i});
	}

	for(int i = 0; i <= n; i++) {
		for(const auto &[l, r, v] : mods[i])
			tree.edt(l, r, v);
		for(const auto &[l, r, v] : mods2[i])
			tree2.edt(l, r, v);
		for(const auto &[l, r, id] : ques[i]) {
			ans[id] = tree.que(l, r);
			ans[id] += tree2.que(max(0, l - i), max(0, r - i));
		}
	}
	
	for(ll a : ans)
		cout << a << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 436 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 372 ms 60772 KB Output is correct
3 Correct 326 ms 60300 KB Output is correct
4 Correct 375 ms 60904 KB Output is correct
5 Correct 369 ms 62920 KB Output is correct
6 Correct 393 ms 60428 KB Output is correct
7 Correct 368 ms 61052 KB Output is correct
8 Correct 386 ms 63140 KB Output is correct
9 Correct 381 ms 61396 KB Output is correct
10 Correct 368 ms 60116 KB Output is correct
11 Correct 366 ms 61496 KB Output is correct
12 Correct 346 ms 60304 KB Output is correct
13 Correct 432 ms 62608 KB Output is correct
14 Correct 368 ms 62956 KB Output is correct
15 Correct 361 ms 62644 KB Output is correct
16 Correct 382 ms 60660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 388 ms 58600 KB Output is correct
3 Correct 378 ms 57808 KB Output is correct
4 Correct 366 ms 61272 KB Output is correct
5 Correct 372 ms 58156 KB Output is correct
6 Correct 368 ms 58636 KB Output is correct
7 Correct 367 ms 58896 KB Output is correct
8 Correct 407 ms 58380 KB Output is correct
9 Correct 388 ms 57856 KB Output is correct
10 Correct 386 ms 57428 KB Output is correct
11 Correct 376 ms 61068 KB Output is correct
12 Correct 385 ms 60600 KB Output is correct
13 Correct 392 ms 60836 KB Output is correct
14 Correct 369 ms 57932 KB Output is correct
15 Correct 386 ms 61028 KB Output is correct
16 Correct 386 ms 60568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 395 ms 56292 KB Output is correct
2 Correct 383 ms 56404 KB Output is correct
3 Correct 405 ms 57616 KB Output is correct
4 Correct 389 ms 56132 KB Output is correct
5 Correct 391 ms 56256 KB Output is correct
6 Correct 401 ms 56996 KB Output is correct
7 Correct 394 ms 57924 KB Output is correct
8 Correct 401 ms 57236 KB Output is correct
9 Correct 417 ms 56276 KB Output is correct
10 Correct 392 ms 57428 KB Output is correct
11 Correct 397 ms 56672 KB Output is correct
12 Correct 391 ms 56984 KB Output is correct
13 Correct 397 ms 56724 KB Output is correct
14 Correct 407 ms 56604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 436 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 372 ms 60772 KB Output is correct
34 Correct 326 ms 60300 KB Output is correct
35 Correct 375 ms 60904 KB Output is correct
36 Correct 369 ms 62920 KB Output is correct
37 Correct 393 ms 60428 KB Output is correct
38 Correct 368 ms 61052 KB Output is correct
39 Correct 386 ms 63140 KB Output is correct
40 Correct 381 ms 61396 KB Output is correct
41 Correct 368 ms 60116 KB Output is correct
42 Correct 366 ms 61496 KB Output is correct
43 Correct 346 ms 60304 KB Output is correct
44 Correct 432 ms 62608 KB Output is correct
45 Correct 368 ms 62956 KB Output is correct
46 Correct 361 ms 62644 KB Output is correct
47 Correct 382 ms 60660 KB Output is correct
48 Correct 388 ms 58600 KB Output is correct
49 Correct 378 ms 57808 KB Output is correct
50 Correct 366 ms 61272 KB Output is correct
51 Correct 372 ms 58156 KB Output is correct
52 Correct 368 ms 58636 KB Output is correct
53 Correct 367 ms 58896 KB Output is correct
54 Correct 407 ms 58380 KB Output is correct
55 Correct 388 ms 57856 KB Output is correct
56 Correct 386 ms 57428 KB Output is correct
57 Correct 376 ms 61068 KB Output is correct
58 Correct 385 ms 60600 KB Output is correct
59 Correct 392 ms 60836 KB Output is correct
60 Correct 369 ms 57932 KB Output is correct
61 Correct 386 ms 61028 KB Output is correct
62 Correct 386 ms 60568 KB Output is correct
63 Correct 395 ms 56292 KB Output is correct
64 Correct 383 ms 56404 KB Output is correct
65 Correct 405 ms 57616 KB Output is correct
66 Correct 389 ms 56132 KB Output is correct
67 Correct 391 ms 56256 KB Output is correct
68 Correct 401 ms 56996 KB Output is correct
69 Correct 394 ms 57924 KB Output is correct
70 Correct 401 ms 57236 KB Output is correct
71 Correct 417 ms 56276 KB Output is correct
72 Correct 392 ms 57428 KB Output is correct
73 Correct 397 ms 56672 KB Output is correct
74 Correct 391 ms 56984 KB Output is correct
75 Correct 397 ms 56724 KB Output is correct
76 Correct 407 ms 56604 KB Output is correct
77 Correct 428 ms 59344 KB Output is correct
78 Correct 436 ms 60300 KB Output is correct
79 Correct 436 ms 61836 KB Output is correct
80 Correct 413 ms 59556 KB Output is correct
81 Correct 425 ms 59368 KB Output is correct
82 Correct 426 ms 59808 KB Output is correct
83 Correct 447 ms 59532 KB Output is correct
84 Correct 438 ms 58868 KB Output is correct
85 Correct 449 ms 61752 KB Output is correct
86 Correct 435 ms 59148 KB Output is correct
87 Correct 368 ms 61672 KB Output is correct
88 Correct 348 ms 61692 KB Output is correct
89 Correct 347 ms 58508 KB Output is correct
90 Correct 376 ms 61200 KB Output is correct
91 Correct 346 ms 58732 KB Output is correct
92 Correct 342 ms 58124 KB Output is correct
93 Correct 349 ms 59224 KB Output is correct
94 Correct 382 ms 61988 KB Output is correct
95 Correct 357 ms 61704 KB Output is correct
96 Correct 382 ms 59064 KB Output is correct
97 Correct 352 ms 59632 KB Output is correct
98 Correct 424 ms 58244 KB Output is correct
99 Correct 431 ms 58800 KB Output is correct
100 Correct 461 ms 59232 KB Output is correct
101 Correct 435 ms 58732 KB Output is correct
102 Correct 429 ms 61196 KB Output is correct