Submission #518800

# Submission time Handle Problem Language Result Execution time Memory
518800 2022-01-24T17:00:27 Z HynDuf Fire (JOI20_ho_t5) C++11
100 / 100
557 ms 164444 KB
#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FORD(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; FOR(_, l, r) DB1(A, _); cerr << '\n';}
#define sz(x) ((int) (x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;

typedef pair<ll, int> ii;
typedef array<int, 3> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vl;
template<typename T>
struct FT {
	vector<T> fenw;
	FT() {}
	FT(int N) : fenw(N + 1, 0) {}
	void upd(int x, T v) {
		// case x < 1?
		assert(x > 0);
		for (; x < sz(fenw); x += x & -x) {
			fenw[x] += v;
		}
	}
	T query(int x) {
		// case x >= sz(fenw)?
		assert(x < sz(fenw));
		T ans = 0;
		for (; x > 0; x -= x & -x) ans += fenw[x];
		return ans;
	}
	T query(int l, int r) {
		return query(r) - query(l - 1);
	}
};
const int N = 2e5 + 2;
int n, q, s[N];
ll ans[N];
viii qu[N];
vii upd_list[N][6]; // {v, pos}
FT<ll> fw[6];
// fw[0] ~ sum of v of open segments (to do: cur_time*sum_v - (0 - 1)*v) ~ all segments start at time 0
// fw[1] ~ sum of (left-1)*v of open segments (to do: pos*sum_v -(left - 1)*v)
// fw[2] ~ sum of (left-1)*v of closed segments
// fw[3] ~ sum of v of left closed segments
// fw[4] ~ sum of right*v of closed segments
// fw[5] ~ sum of v of right closed segments
void push_upd_events(int l, int r, int v)
{
	if (l > r) return;
	assert(l >= 1 && r <= 2 * n);
	upd_list[0][0].pb({v, l});
	upd_list[0][1].pb({(l - 1) * 1LL * v, l});
	int end_time = r - l + 1;
	upd_list[end_time][0].pb({-v, l});
	upd_list[end_time][1].pb({-(l - 1) * 1LL * v, l});
	upd_list[end_time][2].pb({(l - 1) * 1LL * v, l});
	upd_list[end_time][3].pb({v, l});
	upd_list[end_time][4].pb({r * 1LL * v, r});
	upd_list[end_time][5].pb({v, r});

}
void push_raw_upd_events(int l, int r, int t, int v)
{
	l += n, r += n;
	int L = l - t;
	push_upd_events(L, l - 1, -v);
	push_upd_events(L, r, v);
}
int D[N], top, right[N];
void build_upd_list()
{
	FOR(i, 0, 5) fw[i] = FT<ll> (2 * n);
	D[0] = n + 1;
	FORD(i, n, 1)
	{
		while (top && s[D[top]] < s[i]) 
		{
			push_raw_upd_events(D[top], right[D[top]], D[top] - i, -s[D[top]]);
			top--;
		}
		right[i] = D[top] - 1;
		push_raw_upd_events(i, right[i], 0, s[i]);
		D[++top] = i;
	}
}
ll cal(int pos, int cur_time)
{
	if (pos == 0) return 0;
	pos += n;
	ll res = 0;
	// open segments
	ll sum_v = fw[0].query(pos - cur_time);
	res += sum_v * (cur_time + 1);
	ll sum_left = fw[1].query(pos - cur_time + 1, pos);
	sum_v = fw[0].query(pos - cur_time + 1, pos);
	res += pos * sum_v - sum_left;
	// close segments
	sum_v = fw[3].query(pos);
	sum_left = fw[2].query(pos);
	res += pos * sum_v - sum_left;
	sum_v = fw[5].query(pos);
	ll sum_right = fw[4].query(pos);
	res += sum_right - pos * sum_v;
	return res;
}
int main()
{
#ifdef HynDuf
	freopen("inp.txt", "r", stdin);
#else
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
	cin >> n >> q;
	FOR(i, 1, n) cin >> s[i];
	FOR(i, 1, q)
	{
		int l, r, t;
		cin >> t >> l >> r;
		qu[t].pb({l, r, i});
	}
	build_upd_list();
	FOR(cur_time, 0, n)
	{
		FOR(i, 0, 5) for (ii &v : upd_list[cur_time][i]) fw[i].upd(v.S, v.F);
		for (auto &Q : qu[cur_time]) ans[Q[2]] = cal(Q[1], cur_time) - cal(Q[0] - 1, cur_time);
	}
	FOR(i, 1, q) cout << ans[i] << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 33100 KB Output is correct
2 Correct 17 ms 33356 KB Output is correct
3 Correct 18 ms 33332 KB Output is correct
4 Correct 18 ms 33332 KB Output is correct
5 Correct 21 ms 33292 KB Output is correct
6 Correct 18 ms 33360 KB Output is correct
7 Correct 17 ms 33360 KB Output is correct
8 Correct 18 ms 33360 KB Output is correct
9 Correct 17 ms 33360 KB Output is correct
10 Correct 16 ms 33360 KB Output is correct
11 Correct 16 ms 33360 KB Output is correct
12 Correct 17 ms 33308 KB Output is correct
13 Correct 17 ms 33264 KB Output is correct
14 Correct 18 ms 33336 KB Output is correct
15 Correct 18 ms 33296 KB Output is correct
16 Correct 16 ms 33360 KB Output is correct
17 Correct 17 ms 33232 KB Output is correct
18 Correct 16 ms 33372 KB Output is correct
19 Correct 17 ms 33368 KB Output is correct
20 Correct 16 ms 33328 KB Output is correct
21 Correct 18 ms 33316 KB Output is correct
22 Correct 17 ms 33360 KB Output is correct
23 Correct 16 ms 33324 KB Output is correct
24 Correct 16 ms 33360 KB Output is correct
25 Correct 18 ms 33356 KB Output is correct
26 Correct 16 ms 33268 KB Output is correct
27 Correct 17 ms 33336 KB Output is correct
28 Correct 16 ms 33224 KB Output is correct
29 Correct 17 ms 33360 KB Output is correct
30 Correct 16 ms 33360 KB Output is correct
31 Correct 17 ms 33352 KB Output is correct
32 Correct 17 ms 33352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 33100 KB Output is correct
2 Correct 432 ms 142228 KB Output is correct
3 Correct 425 ms 147148 KB Output is correct
4 Correct 425 ms 148136 KB Output is correct
5 Correct 424 ms 161064 KB Output is correct
6 Correct 430 ms 147700 KB Output is correct
7 Correct 433 ms 148384 KB Output is correct
8 Correct 465 ms 159752 KB Output is correct
9 Correct 557 ms 153320 KB Output is correct
10 Correct 419 ms 146596 KB Output is correct
11 Correct 437 ms 153188 KB Output is correct
12 Correct 434 ms 146444 KB Output is correct
13 Correct 441 ms 159952 KB Output is correct
14 Correct 428 ms 160564 KB Output is correct
15 Correct 432 ms 158724 KB Output is correct
16 Correct 400 ms 147856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 33100 KB Output is correct
2 Correct 440 ms 143092 KB Output is correct
3 Correct 427 ms 141900 KB Output is correct
4 Correct 430 ms 157660 KB Output is correct
5 Correct 429 ms 142800 KB Output is correct
6 Correct 413 ms 143568 KB Output is correct
7 Correct 436 ms 152032 KB Output is correct
8 Correct 435 ms 142868 KB Output is correct
9 Correct 441 ms 142532 KB Output is correct
10 Correct 419 ms 141872 KB Output is correct
11 Correct 477 ms 155272 KB Output is correct
12 Correct 427 ms 154644 KB Output is correct
13 Correct 459 ms 154924 KB Output is correct
14 Correct 450 ms 143076 KB Output is correct
15 Correct 448 ms 157812 KB Output is correct
16 Correct 456 ms 156620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 112888 KB Output is correct
2 Correct 381 ms 114216 KB Output is correct
3 Correct 430 ms 119556 KB Output is correct
4 Correct 376 ms 121716 KB Output is correct
5 Correct 404 ms 119464 KB Output is correct
6 Correct 385 ms 114248 KB Output is correct
7 Correct 376 ms 122720 KB Output is correct
8 Correct 391 ms 120212 KB Output is correct
9 Correct 368 ms 120576 KB Output is correct
10 Correct 418 ms 114308 KB Output is correct
11 Correct 402 ms 114932 KB Output is correct
12 Correct 384 ms 118316 KB Output is correct
13 Correct 376 ms 114052 KB Output is correct
14 Correct 380 ms 118828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 33100 KB Output is correct
2 Correct 17 ms 33356 KB Output is correct
3 Correct 18 ms 33332 KB Output is correct
4 Correct 18 ms 33332 KB Output is correct
5 Correct 21 ms 33292 KB Output is correct
6 Correct 18 ms 33360 KB Output is correct
7 Correct 17 ms 33360 KB Output is correct
8 Correct 18 ms 33360 KB Output is correct
9 Correct 17 ms 33360 KB Output is correct
10 Correct 16 ms 33360 KB Output is correct
11 Correct 16 ms 33360 KB Output is correct
12 Correct 17 ms 33308 KB Output is correct
13 Correct 17 ms 33264 KB Output is correct
14 Correct 18 ms 33336 KB Output is correct
15 Correct 18 ms 33296 KB Output is correct
16 Correct 16 ms 33360 KB Output is correct
17 Correct 17 ms 33232 KB Output is correct
18 Correct 16 ms 33372 KB Output is correct
19 Correct 17 ms 33368 KB Output is correct
20 Correct 16 ms 33328 KB Output is correct
21 Correct 18 ms 33316 KB Output is correct
22 Correct 17 ms 33360 KB Output is correct
23 Correct 16 ms 33324 KB Output is correct
24 Correct 16 ms 33360 KB Output is correct
25 Correct 18 ms 33356 KB Output is correct
26 Correct 16 ms 33268 KB Output is correct
27 Correct 17 ms 33336 KB Output is correct
28 Correct 16 ms 33224 KB Output is correct
29 Correct 17 ms 33360 KB Output is correct
30 Correct 16 ms 33360 KB Output is correct
31 Correct 17 ms 33352 KB Output is correct
32 Correct 17 ms 33352 KB Output is correct
33 Correct 432 ms 142228 KB Output is correct
34 Correct 425 ms 147148 KB Output is correct
35 Correct 425 ms 148136 KB Output is correct
36 Correct 424 ms 161064 KB Output is correct
37 Correct 430 ms 147700 KB Output is correct
38 Correct 433 ms 148384 KB Output is correct
39 Correct 465 ms 159752 KB Output is correct
40 Correct 557 ms 153320 KB Output is correct
41 Correct 419 ms 146596 KB Output is correct
42 Correct 437 ms 153188 KB Output is correct
43 Correct 434 ms 146444 KB Output is correct
44 Correct 441 ms 159952 KB Output is correct
45 Correct 428 ms 160564 KB Output is correct
46 Correct 432 ms 158724 KB Output is correct
47 Correct 400 ms 147856 KB Output is correct
48 Correct 440 ms 143092 KB Output is correct
49 Correct 427 ms 141900 KB Output is correct
50 Correct 430 ms 157660 KB Output is correct
51 Correct 429 ms 142800 KB Output is correct
52 Correct 413 ms 143568 KB Output is correct
53 Correct 436 ms 152032 KB Output is correct
54 Correct 435 ms 142868 KB Output is correct
55 Correct 441 ms 142532 KB Output is correct
56 Correct 419 ms 141872 KB Output is correct
57 Correct 477 ms 155272 KB Output is correct
58 Correct 427 ms 154644 KB Output is correct
59 Correct 459 ms 154924 KB Output is correct
60 Correct 450 ms 143076 KB Output is correct
61 Correct 448 ms 157812 KB Output is correct
62 Correct 456 ms 156620 KB Output is correct
63 Correct 412 ms 112888 KB Output is correct
64 Correct 381 ms 114216 KB Output is correct
65 Correct 430 ms 119556 KB Output is correct
66 Correct 376 ms 121716 KB Output is correct
67 Correct 404 ms 119464 KB Output is correct
68 Correct 385 ms 114248 KB Output is correct
69 Correct 376 ms 122720 KB Output is correct
70 Correct 391 ms 120212 KB Output is correct
71 Correct 368 ms 120576 KB Output is correct
72 Correct 418 ms 114308 KB Output is correct
73 Correct 402 ms 114932 KB Output is correct
74 Correct 384 ms 118316 KB Output is correct
75 Correct 376 ms 114052 KB Output is correct
76 Correct 380 ms 118828 KB Output is correct
77 Correct 469 ms 149584 KB Output is correct
78 Correct 486 ms 150688 KB Output is correct
79 Correct 476 ms 164444 KB Output is correct
80 Correct 507 ms 149312 KB Output is correct
81 Correct 475 ms 149080 KB Output is correct
82 Correct 464 ms 154400 KB Output is correct
83 Correct 499 ms 150484 KB Output is correct
84 Correct 479 ms 148920 KB Output is correct
85 Correct 535 ms 162116 KB Output is correct
86 Correct 463 ms 148820 KB Output is correct
87 Correct 454 ms 162704 KB Output is correct
88 Correct 466 ms 161124 KB Output is correct
89 Correct 499 ms 147700 KB Output is correct
90 Correct 478 ms 160288 KB Output is correct
91 Correct 442 ms 148604 KB Output is correct
92 Correct 440 ms 147392 KB Output is correct
93 Correct 450 ms 148856 KB Output is correct
94 Correct 464 ms 160984 KB Output is correct
95 Correct 453 ms 161732 KB Output is correct
96 Correct 443 ms 149124 KB Output is correct
97 Correct 488 ms 149108 KB Output is correct
98 Correct 418 ms 147912 KB Output is correct
99 Correct 435 ms 153040 KB Output is correct
100 Correct 441 ms 153992 KB Output is correct
101 Correct 420 ms 148448 KB Output is correct
102 Correct 470 ms 161080 KB Output is correct