답안 #541988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541988 2022-03-25T02:26:33 Z 8e7 Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
930 ms 210360 KB
//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
struct sparse_segtree{
	struct node{
		ll m, tag, k;
		node *lef, *rig;
		node(){m = tag = k = 0, lef = rig = NULL;}
		node(ll a, ll b, ll c){m = a, tag = b, k = c;}
	};
	node *root = new node();	
	void add(node * &x) {
		if (x == NULL) {
			x = new node();
		}
	}
	ll calc(int l, int r, ll m) {
		return m * (l + r - 1) * (r - l) / 2;
	}
	void modify(node *cur, ll l, ll r, ll ql, ll qr, ll m, ll k) {
		//if (cur == root) debug(ql, qr, m, k);
		if (r <= l || qr <= l || ql >= r) return;
		if (ql <= l && qr >= r) {
			cur->m += m;
			cur->tag += k;	
			return;
		}
		ll mid = (l + r) / 2;	
		add(cur->lef), add(cur->rig); 
		modify(cur->lef, l, mid, ql, qr, m, k);	
		modify(cur->rig, mid, r, ql, qr, m, k);
		cur->k = cur->lef->k + cur->rig->k + calc(l, m, cur->lef->m) + calc(m, r, cur->rig->m) + 
			cur->lef->tag + cur->rig->tag;
	}	
	ll query(node *cur, ll l, ll r, int x) {
		if (r <= l || x < l || x >= r) return 0;
		if (r - l == 1) return calc(l, r, cur->m) + cur->k + cur->tag;
		ll m = (l + r) / 2;
		ll lef = (cur->lef ? query(cur->lef, l, m, x) : 0);
		ll rig = (cur->rig ? query(cur->rig, m, r, x) : 0);
		return (x < m ? lef : rig) + cur->tag + cur->m * x;
	}
} tr;

struct segtree{
	pii seg[4*maxn];
	int type = 0;
	void init(int cur, int l, int r, ll a[]) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = {a[l], l};
			return;
		}
		int m = (l + r) / 2;
		init(cur*2, l, m, a), init(cur*2+1, m, r, a);
		seg[cur] = min(seg[cur*2], seg[cur*2+1]);
		if (type == 1) seg[cur] = max(seg[cur*2], seg[cur*2+1]);
	}
	pii query(int cur, int l, int r, int ql, int qr) {
		if (r <= l || ql >= r || qr <= l) return (type ? make_pair(0LL, (ll)l) : make_pair(inf, (ll)l));
		if (ql <= l && qr >= r) return seg[cur];
		int m = (l + r) / 2;
		pii vl = query(cur*2, l, m, ql, qr), vr = query(cur*2+1, m, r, ql, qr);
		if (type) return max(vl, vr);
		else return min(vl,vr);
	}
} ma, mb;
struct query{
	int c, u, id;	
	query(){c = u = id = 0;}
	query(int b, int p, int q){c = b, u = p, id = q;}
};
ll a[maxn], b[maxn], pref[maxn];
int prv[maxn];
vector<query> que[maxn];
ll ans[maxn];
int main() {
	io
	int n, m;
	cin >> n >> m;	
	for (int i = 1;i<= n;i++) cin >> a[i], pref[i] = a[i] + pref[i-1];
	for (int i = 1;i <= n;i++) cin >> b[i];
	ma.type = 1;
	ma.init(1, 1, n+1, a);
	mb.init(1, 1, n+1, b);
	stack<int> stk;		
	for (int i = 0;i < m;i++) {
		int s, t, u;
		cin >> s >> t >> u;
		if (ma.query(1, 1, n+1, s, t).ff > u) {
			ans[i] = -1;
		} else {
			que[s].push_back(query(1, u, i));
			ll X = max(pref[s-1], pref[t-1] - u);
			int ind = lower_bound(pref, pref + n + 1, X) - pref;
			pii best = mb.query(1, 1, n+1, ind + 1, t);
			//debug(i, X, ind, best.ff, best.ss);
			ans[i] += (pref[t-1] - pref[best.ss-1]) * best.ff;
			que[best.ss].push_back(query(-1, u, i));
		}
	}	
	const int mu = 100000005;
	
	for (int i = n;i >= 1;i--) {
		while (stk.size() && b[i] < b[stk.top()]) {
			int cur = stk.top();
			stk.pop();
			ll pnt = pref[cur-1] - pref[i-1] + 1;
			if (pnt < mu) {
				tr.modify(tr.root, 0, mu, pnt, mu, -b[cur], -b[cur] * (1 - pnt));
				ll len = pref[prv[cur]-1] - pref[i-1] + 1;
				tr.modify(tr.root, 0, mu, len, mu, b[cur], b[cur] * (1 - len));
			}
		}	
		prv[i] = (stk.size() ? stk.top() : n+1);
		stk.push(i);
		tr.modify(tr.root, 0, mu, 0, mu, b[i], 0); //ok
		ll len = pref[prv[i]-1] - pref[i-1] + 1;
		tr.modify(tr.root, 0, mu, len, mu, -b[i], -b[i] * (1 - len)); //ok

		for (auto q:que[i]) {
			ans[q.id] += q.c * tr.query(tr.root, 0, mu, q.u);	
		}
		/*
		for (int j = 1;j <= 10;j++) {
			cout << tr.query(tr.root, 0, mu, j) << " ";
		}
		cout << "\n";
		*/
	}	
	for (int i = 0;i < m;i++) cout << ans[i] << "\n";
}
/*
5 4
3 4 1 2 2
2 5 1 2 1
3 6 1
3 6 2
3 6 3

3 4
1 2 2
1 2 1
1 4 1
1 4 2
1 4 3
1 4 4

5 4
3 4 1 1 4
2 5 1 2 1
1 6 3
1 6 4
3 5 1
2 5 9
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 9428 KB Output is correct
2 Correct 13 ms 9044 KB Output is correct
3 Correct 8 ms 5916 KB Output is correct
4 Correct 16 ms 9264 KB Output is correct
5 Correct 14 ms 8896 KB Output is correct
6 Correct 9 ms 7152 KB Output is correct
7 Correct 11 ms 6812 KB Output is correct
8 Correct 13 ms 8916 KB Output is correct
9 Correct 9 ms 5996 KB Output is correct
10 Correct 14 ms 9264 KB Output is correct
11 Correct 12 ms 8660 KB Output is correct
12 Correct 10 ms 7124 KB Output is correct
13 Correct 13 ms 9220 KB Output is correct
14 Correct 18 ms 8908 KB Output is correct
15 Correct 19 ms 8880 KB Output is correct
16 Correct 15 ms 8920 KB Output is correct
17 Correct 10 ms 6064 KB Output is correct
18 Correct 12 ms 6700 KB Output is correct
19 Correct 7 ms 5588 KB Output is correct
20 Correct 11 ms 8164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 32956 KB Output is correct
2 Correct 168 ms 42632 KB Output is correct
3 Correct 160 ms 25868 KB Output is correct
4 Correct 126 ms 24436 KB Output is correct
5 Correct 199 ms 43624 KB Output is correct
6 Correct 540 ms 62960 KB Output is correct
7 Correct 747 ms 107328 KB Output is correct
8 Correct 678 ms 63308 KB Output is correct
9 Correct 656 ms 62776 KB Output is correct
10 Correct 701 ms 86872 KB Output is correct
11 Correct 697 ms 86496 KB Output is correct
12 Correct 601 ms 46884 KB Output is correct
13 Correct 783 ms 55316 KB Output is correct
14 Correct 743 ms 54948 KB Output is correct
15 Correct 581 ms 209144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 759 ms 76896 KB Output is correct
2 Correct 525 ms 54932 KB Output is correct
3 Correct 434 ms 60716 KB Output is correct
4 Correct 635 ms 106720 KB Output is correct
5 Correct 416 ms 75988 KB Output is correct
6 Correct 459 ms 54196 KB Output is correct
7 Correct 464 ms 48052 KB Output is correct
8 Correct 382 ms 43932 KB Output is correct
9 Correct 357 ms 43036 KB Output is correct
10 Correct 534 ms 210360 KB Output is correct
11 Correct 433 ms 60304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 9428 KB Output is correct
2 Correct 13 ms 9044 KB Output is correct
3 Correct 8 ms 5916 KB Output is correct
4 Correct 16 ms 9264 KB Output is correct
5 Correct 14 ms 8896 KB Output is correct
6 Correct 9 ms 7152 KB Output is correct
7 Correct 11 ms 6812 KB Output is correct
8 Correct 13 ms 8916 KB Output is correct
9 Correct 9 ms 5996 KB Output is correct
10 Correct 14 ms 9264 KB Output is correct
11 Correct 12 ms 8660 KB Output is correct
12 Correct 10 ms 7124 KB Output is correct
13 Correct 13 ms 9220 KB Output is correct
14 Correct 18 ms 8908 KB Output is correct
15 Correct 19 ms 8880 KB Output is correct
16 Correct 15 ms 8920 KB Output is correct
17 Correct 10 ms 6064 KB Output is correct
18 Correct 12 ms 6700 KB Output is correct
19 Correct 7 ms 5588 KB Output is correct
20 Correct 11 ms 8164 KB Output is correct
21 Correct 122 ms 32956 KB Output is correct
22 Correct 168 ms 42632 KB Output is correct
23 Correct 160 ms 25868 KB Output is correct
24 Correct 126 ms 24436 KB Output is correct
25 Correct 199 ms 43624 KB Output is correct
26 Correct 540 ms 62960 KB Output is correct
27 Correct 747 ms 107328 KB Output is correct
28 Correct 678 ms 63308 KB Output is correct
29 Correct 656 ms 62776 KB Output is correct
30 Correct 701 ms 86872 KB Output is correct
31 Correct 697 ms 86496 KB Output is correct
32 Correct 601 ms 46884 KB Output is correct
33 Correct 783 ms 55316 KB Output is correct
34 Correct 743 ms 54948 KB Output is correct
35 Correct 581 ms 209144 KB Output is correct
36 Correct 759 ms 76896 KB Output is correct
37 Correct 525 ms 54932 KB Output is correct
38 Correct 434 ms 60716 KB Output is correct
39 Correct 635 ms 106720 KB Output is correct
40 Correct 416 ms 75988 KB Output is correct
41 Correct 459 ms 54196 KB Output is correct
42 Correct 464 ms 48052 KB Output is correct
43 Correct 382 ms 43932 KB Output is correct
44 Correct 357 ms 43036 KB Output is correct
45 Correct 534 ms 210360 KB Output is correct
46 Correct 433 ms 60304 KB Output is correct
47 Correct 829 ms 55248 KB Output is correct
48 Correct 718 ms 62292 KB Output is correct
49 Correct 606 ms 45928 KB Output is correct
50 Correct 930 ms 108096 KB Output is correct
51 Correct 609 ms 70172 KB Output is correct
52 Correct 818 ms 125640 KB Output is correct
53 Correct 678 ms 48792 KB Output is correct
54 Correct 543 ms 44124 KB Output is correct
55 Correct 534 ms 44044 KB Output is correct
56 Correct 618 ms 210140 KB Output is correct
57 Correct 626 ms 61136 KB Output is correct