Submission #570471

# Submission time Handle Problem Language Result Execution time Memory
570471 2022-05-30T04:12:44 Z 8e7 Fire (JOI20_ho_t5) C++17
1 / 100
1000 ms 65440 KB
//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
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 mod 998244353
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct query{
	int l, r, id;	
	query(){l = r = id = 0;}
	query(int a, int b, int c){l = a, r = b, id = c;}
};
struct line{
	ll m, k;
	line(){m = k = 0;}
	line(ll x, ll y){m = x, k = y;}
	ll val(ll x){return m * x + k;}
	line operator +(line l){
		return line(m + l.m, k + l.k);
	}
};
struct segtree{
	line seg[4 * maxn];
	void init(int cur, int l, int r, ll a[]) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = line{a[l], a[l]};
			return;	
		}
		int m = (l + r) / 2;
		init(cur*2, l, m, a), init(cur*2+1, m, r, a);
		seg[cur] = seg[cur*2] + seg[cur*2+1];
	}
	void modify(int cur, int l, int r, int ind, line li) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = li;
			return;
		}
		int m = (l + r) / 2;
		if (ind < m) modify(cur*2, l, m, ind, li);
		else modify(cur*2+1, m, r, ind, li);
		seg[cur] = seg[cur*2] + seg[cur*2+1];
	}
	ll query(int cur, int l, int r, int ql, int qr, int x) {
		if (r <= l || ql >= r || qr <= l) return 0;
		if (ql <= l && qr >= r) return seg[cur].val(x);
		int m = (l + r) / 2;
		return query(cur*2, l, m, ql, qr, x) + query(cur*2+1, m, r, ql, qr, x);
	}
} tr;
struct sparse_table{
	pii sp[18][maxn];
	void build(int n, ll a[]) {
		for (int i = 1;i <= n;i++) sp[0][i] = {a[i], i};
		for (int i = 1;i < 18;i++) {
			for (int j = 1;j <= n - (1<<i) + 1;j++) {
				sp[i][j] = max(sp[i-1][j], sp[i-1][j + (1<<(i-1))]);
			}
		}
	}
	int query(int l, int r) { //[l, r]
		l = max(l, 1);
		int h = __lg(r - l + 1);
		return max(sp[h][l], sp[h][r - (1<<h) + 1]).ss;	
	}
} sp;
vector<query> que[maxn];
vector<int> ev[maxn];
ll a[maxn], ans[maxn];
int lef[maxn], rig[maxn], type[maxn];
int main() {
	io
	int n, q;
	cin >> n >> q;
	for (int i = 1;i <= n;i++) cin >> a[i];	
	for (int i = 0;i < q;i++) {
		int t, l, r;
		cin >> t >> l >> r;
		que[t].push_back(query(l, r, i));			
	}
	stack<int> stk;
	for (int i = 1;i <= n;i++) {
		while (stk.size() && a[i] >= a[stk.top()]) {
			rig[stk.top()] = i;	
			stk.pop();
		}
		if (stk.size()) lef[i] = stk.top();
		else lef[i] = -n;
		stk.push(i);
	}
	while (stk.size()) {
		rig[stk.top()] = n + 1;
		stk.pop();
	}
	for (int i = 1;i <= n;i++) {
		if (i - lef[i] - 1 < maxn) ev[i - lef[i] - 1].push_back(i);	
		if (rig[i] - i - 1 < maxn) ev[rig[i] - i - 1].push_back(i);	
		if (rig[i] - lef[i] < maxn) ev[rig[i] - lef[i]].push_back(i);
	}
	tr.init(1, 1, n+1, a);
	sp.build(n, a);
	for (int i = 0;i <= n;i++) {
		for (int j:ev[i]) {
			//debug("ch", j);
			if (type[j] < 2) {
				ll cur = tr.query(1, 1, n+1, j, j+1, i);
				tr.modify(1, 1, n+1, j, line(-type[j] * a[j], cur + type[j] * a[j] * i));
			} else {
				tr.modify(1, 1, n+1, j, line());		
			}
			type[j]++;
		}
		for (auto [l, r, id]:que[i]) {
			ans[id] = tr.query(1, 1, n+1, l - i, r+1, i);	
			for (int x = max(l - i, 1);x <= r;x++) {
				ll k = tr.query(1, 1, n+1, x, x+1, 0);
				debug(tr.query(1, 1, n+1, x, x+1, 1) - k, k);
			}
			debug(i, l, r);
			int mr = sp.query(r-i, r), ml = sp.query(l - i, l);
			debug(ans[id], ml, mr);
			ans[id] -= (min(rig[mr] - 1, mr + i) - r) * a[mr];
			ans[id] -= (l - max(lef[ml] + i + 1, ml)) * a[ml];
			ans[id] -= tr.query(1, 1, n+1, l - i, ml, i);
			ans[id] -= tr.query(1, 1, n+1, mr+1, r+1, i);
		}
	}
	for (int i = 0;i < q;i++) cout << ans[i] << "\n";
}
/*
10 1
3 1 4 1 5 9 2 6 5 3
3 2 8
*/

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
ho_t5.cpp:133:5: note: in expansion of macro 'debug'
  133 |     debug(tr.query(1, 1, n+1, x, x+1, 1) - k, k);
      |     ^~~~~
ho_t5.cpp:132:8: warning: unused variable 'k' [-Wunused-variable]
  132 |     ll k = tr.query(1, 1, n+1, x, x+1, 0);
      |        ^
ho_t5.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
ho_t5.cpp:135:4: note: in expansion of macro 'debug'
  135 |    debug(i, l, r);
      |    ^~~~~
ho_t5.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
ho_t5.cpp:137:4: note: in expansion of macro 'debug'
  137 |    debug(ans[id], ml, mr);
      |    ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 22264 KB Output is correct
2 Correct 14 ms 22260 KB Output is correct
3 Correct 14 ms 22280 KB Output is correct
4 Correct 14 ms 22264 KB Output is correct
5 Correct 13 ms 22260 KB Output is correct
6 Correct 15 ms 22356 KB Output is correct
7 Correct 16 ms 22260 KB Output is correct
8 Correct 15 ms 22356 KB Output is correct
9 Correct 13 ms 22264 KB Output is correct
10 Correct 15 ms 22484 KB Output is correct
11 Correct 12 ms 22356 KB Output is correct
12 Correct 12 ms 22356 KB Output is correct
13 Correct 13 ms 22292 KB Output is correct
14 Correct 12 ms 22356 KB Output is correct
15 Correct 13 ms 22312 KB Output is correct
16 Correct 13 ms 22356 KB Output is correct
17 Correct 13 ms 22248 KB Output is correct
18 Correct 14 ms 22244 KB Output is correct
19 Correct 15 ms 22356 KB Output is correct
20 Correct 14 ms 22288 KB Output is correct
21 Correct 12 ms 22356 KB Output is correct
22 Correct 16 ms 22256 KB Output is correct
23 Correct 15 ms 22364 KB Output is correct
24 Correct 14 ms 22260 KB Output is correct
25 Correct 13 ms 22260 KB Output is correct
26 Correct 12 ms 22356 KB Output is correct
27 Correct 15 ms 22264 KB Output is correct
28 Correct 12 ms 22264 KB Output is correct
29 Correct 13 ms 22256 KB Output is correct
30 Correct 13 ms 22352 KB Output is correct
31 Correct 12 ms 22356 KB Output is correct
32 Correct 13 ms 22356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 22264 KB Output is correct
2 Execution timed out 1059 ms 62916 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 22264 KB Output is correct
2 Execution timed out 1082 ms 65440 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1054 ms 62260 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 22264 KB Output is correct
2 Correct 14 ms 22260 KB Output is correct
3 Correct 14 ms 22280 KB Output is correct
4 Correct 14 ms 22264 KB Output is correct
5 Correct 13 ms 22260 KB Output is correct
6 Correct 15 ms 22356 KB Output is correct
7 Correct 16 ms 22260 KB Output is correct
8 Correct 15 ms 22356 KB Output is correct
9 Correct 13 ms 22264 KB Output is correct
10 Correct 15 ms 22484 KB Output is correct
11 Correct 12 ms 22356 KB Output is correct
12 Correct 12 ms 22356 KB Output is correct
13 Correct 13 ms 22292 KB Output is correct
14 Correct 12 ms 22356 KB Output is correct
15 Correct 13 ms 22312 KB Output is correct
16 Correct 13 ms 22356 KB Output is correct
17 Correct 13 ms 22248 KB Output is correct
18 Correct 14 ms 22244 KB Output is correct
19 Correct 15 ms 22356 KB Output is correct
20 Correct 14 ms 22288 KB Output is correct
21 Correct 12 ms 22356 KB Output is correct
22 Correct 16 ms 22256 KB Output is correct
23 Correct 15 ms 22364 KB Output is correct
24 Correct 14 ms 22260 KB Output is correct
25 Correct 13 ms 22260 KB Output is correct
26 Correct 12 ms 22356 KB Output is correct
27 Correct 15 ms 22264 KB Output is correct
28 Correct 12 ms 22264 KB Output is correct
29 Correct 13 ms 22256 KB Output is correct
30 Correct 13 ms 22352 KB Output is correct
31 Correct 12 ms 22356 KB Output is correct
32 Correct 13 ms 22356 KB Output is correct
33 Execution timed out 1059 ms 62916 KB Time limit exceeded
34 Halted 0 ms 0 KB -