답안 #589691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589691 2022-07-05T06:34:31 Z 8e7 Fish 2 (JOI22_fish2) C++17
31 / 100
468 ms 289368 KB
//Challenge: Accepted
#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 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;

struct segtree_interval{
	stack<pii> stk[4*maxn];	
	bool in (pii &p, int x) {
		return p.ff <= x && x <= p.ss;
	}
	void remove(int cur, int l, int r, int ind, vector<pii> &v) {
		if (r <= l) return;
		while (stk[cur].size() && in(stk[cur].top(), ind)) {
			v.push_back(stk[cur].top());
			stk[cur].pop();
		}
		int m = (l + r) / 2;
		if (ind < m) remove(cur*2, l, m, ind, v);
		else if (ind > m) remove(cur*2+1, m+1, r, ind, v);
	}
	void ins(int cur, int l, int r, pii p) {
		if (r <= l) return;
		int m = (l + r) / 2;
		if (in(p, m)) {
			stk[cur].push(p);
			return;
		}
		if (m > p.ss) ins(cur*2, l, m, p);
		else if (m < p.ff) ins(cur*2+1, m, r, p);
	}
} inter;
struct segtree_mincnt{
	pii seg[4*maxn];
	int tag[4*maxn];
	void init(int cur, int l, int r) {
		if (r <= l) return;
		seg[cur].ss = (r - l);
		if (r - l == 1) return;
		int m = (l + r) / 2;
		init(cur*2, l, m), init(cur*2+1, m, r);
	}
	void push(int cur, int l, int r) {
		seg[cur].ff += tag[cur];
		if (r - l > 1) {
			tag[cur*2] += tag[cur];
			tag[cur*2+1] += tag[cur];
		}
		tag[cur] = 0;
	}
	void merge(pii &a, pii &b, pii &c) {
		a.ff = min(b.ff, c.ff);
		a.ss = (b.ff == a.ff ? b.ss : 0) + (c.ff == a.ff ? c.ss : 0);
	}
	void pull(int cur, int l, int r) {
		int m = (l + r) / 2;
		push(cur*2, l, m), push(cur*2+1, m, r);
		merge(seg[cur], seg[cur*2], seg[cur*2+1]);
	}
	void modify(int cur, int l, int r, int ql, int qr, int v) {
		if (r <= l || ql >= r || qr <= l) return;
		push(cur, l, r);
		if (ql <= l && qr >= r) {
			tag[cur] += v;
			return;
		}
		int m = (l + r) / 2;
		modify(cur*2, l, m, ql, qr, v); 
		modify(cur*2+1, m, r, ql, qr, v); 
		pull(cur, l, r);
	}
	pii query(int cur, int l, int r, int ql, int qr) {
		if (r <= l || ql >= r || qr <= l) return {inf, 0};
		push(cur, l, r);
		if (ql <= l && qr >= r) return seg[cur];
		int m = (l + r) / 2;
		pii ret, vl = query(cur*2, l, m, ql, qr), vr = query(cur*2+1, m, r, ql, qr);
		merge(ret, vl, vr);
		return ret;
	}
} mincnt;

struct segtree{
	ll seg[4*maxn], tag[4*maxn];
	void init(int cur, int l, int r, ll a[]) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = a[l];
			return;
		}
		int m = (l + r) / 2;
		init(cur*2, l, m, a), init(cur*2+1, m, r, a);
		seg[cur] = max(seg[cur*2], seg[cur*2+1]);
	}
	void push(int cur, int l, int r) {
		seg[cur] += tag[cur];
		if (r - l > 1) {
			tag[cur*2] += tag[cur];
			tag[cur*2+1] += tag[cur];
		}
		tag[cur] = 0;
	}
	void pull(int cur, int l, int r) {
		int m = (l + r) / 2;
		push(cur*2, l, m), push(cur*2+1, m, r);
		seg[cur] = max(seg[cur*2], seg[cur*2+1]);
	}
	void modify(int cur, int l, int r, int ql, int qr, ll x) {
		if (r <= l || ql >= r || qr <= l) return;
		push(cur, l, r);
		if (ql <= l && qr >= r) {
			tag[cur] += x;
			return;
		}
		int m = (l + r) / 2;
		modify(cur*2, l, m, ql, qr, x);
		modify(cur*2+1, m, r, ql, qr, x);
		pull(cur, l, r);
	}
	int search(int cur, int l, int r, int ql, int qr, bool type, ll t) {
		//type 0: left, 1:right
		push(cur, l, r);
		if (r <= l || ql >= r || qr <= l || seg[cur] < t) return -1;
		if (r - l == 1) return l;
		int m = (l + r) / 2;
		if (type) {
			int vr = search(cur*2+1, m, r, ql, qr, type, t);
			if (vr != -1) return vr;
			return search(cur*2, l, m, ql, qr, type, t);
		} else {
			int vl = search(cur*2, l, m, ql, qr, type, t);
			if (vl != -1) return vl;
			return search(cur*2+1, m, r, ql, qr, type, t);
		}
	}
	ll query(int cur, int l, int r, int x) { //getpnt
		if (r <= l) return 0;
		push(cur, l, r);
		if (r - l == 1) return seg[cur];
		int m = (l + r) / 2;
		if (x < m) return query(cur*2, l, m, x);
		else query(cur*2+1, m, r, x);
	}
} tr, lb, rb; 
struct BIT{
	ll bit[maxn];
	void modify(int ind, ll val) {
		ind++;
		for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
	}
	ll query(int ind) {
		ll ret = 0;
		ind++;
		for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
		return ret;
	}
	ll sum(int l, int r) {
		return query(r) - query(l-1);	
	}
} sum;
ll a[maxn];
int main() {
	io
	int n, q;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> a[i];
		sum.modify(i, a[i]);
	}
	auto check = [&] (int l, int r) { //stuck in [l, r]
		if (r < l) return;
		ll s = sum.sum(l, r);
	
		if(!((l && s >= a[l-1]) || (r < n - 1 && s >= a[r+1]))) {
			debug(l, r);
			inter.ins(1, 0, n, {l, r});				
			mincnt.modify(1, 0, n, l, r+1, 1);	
		}
	};
	tr.init(1, 0, n, a);
	mincnt.init(1, 0, n);
	{
		ll pref[maxn];
		for (int i = 0;i < n;i++) {
			pref[i] = a[i] + (i ? pref[i-1] : 0);
			lb.modify(1, 0, n, i, i+1, a[i] - (i ? pref[i-1] : 0));	
			rb.modify(1, 0, n, i, i+1, a[i] + pref[i]);
		}
		
		stack<int> stk;
		for (int i = 0;i < n;i++) {
			while (stk.size() && a[i] >= a[stk.top()]) {
				check(stk.top()+1, i-1);
				stk.pop();	
			}
			if (stk.size()) check(stk.top()+1, i-1);	
			else check(0, i-1);
			stk.push(i);
		}
		while (stk.size()) {
			check(stk.top()+1, n-1);
			stk.pop();
		}
	}
	cin >> q;
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			cout << "zisk" << "\n";
		} else {
			int l, r;
			cin >> l >> r;
			l--; //[l, r)
			ll suml = -sum.query(l-1)+1, sumr = sum.query(r-1)+1;
			int il = lb.search(1, 0, n, l, r, 1, suml);
			int ir = rb.search(1, 0, n, l, r, 0, sumr)+1;
			if (il != -1) l = il;
			if (ir != -1) r = ir;
			debug("query", l, r);
			cout << mincnt.query(1, 0, n, l, r).ss << "\n";
		}
	}
}
/*
*/

Compilation message

fish2.cpp: In lambda function:
fish2.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
fish2.cpp:191:4: note: in expansion of macro 'debug'
  191 |    debug(l, r);
      |    ^~~~~
fish2.cpp: In function 'int main()':
fish2.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
fish2.cpp:236:4: note: in expansion of macro 'debug'
  236 |    debug("query", l, r);
      |    ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 199 ms 273476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 273592 KB Output is correct
2 Correct 303 ms 286928 KB Output is correct
3 Correct 266 ms 286692 KB Output is correct
4 Correct 299 ms 286952 KB Output is correct
5 Correct 265 ms 286672 KB Output is correct
6 Correct 255 ms 287340 KB Output is correct
7 Correct 269 ms 286608 KB Output is correct
8 Correct 244 ms 287360 KB Output is correct
9 Correct 249 ms 286520 KB Output is correct
10 Correct 255 ms 286980 KB Output is correct
11 Correct 243 ms 286636 KB Output is correct
12 Correct 249 ms 286540 KB Output is correct
13 Correct 241 ms 286572 KB Output is correct
14 Correct 252 ms 287028 KB Output is correct
15 Correct 294 ms 287020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 199 ms 273476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 273592 KB Output is correct
2 Correct 303 ms 286928 KB Output is correct
3 Correct 266 ms 286692 KB Output is correct
4 Correct 299 ms 286952 KB Output is correct
5 Correct 265 ms 286672 KB Output is correct
6 Correct 255 ms 287340 KB Output is correct
7 Correct 269 ms 286608 KB Output is correct
8 Correct 244 ms 287360 KB Output is correct
9 Correct 249 ms 286520 KB Output is correct
10 Correct 255 ms 286980 KB Output is correct
11 Correct 243 ms 286636 KB Output is correct
12 Correct 249 ms 286540 KB Output is correct
13 Correct 241 ms 286572 KB Output is correct
14 Correct 252 ms 287028 KB Output is correct
15 Correct 294 ms 287020 KB Output is correct
16 Correct 157 ms 273640 KB Output is correct
17 Correct 452 ms 288420 KB Output is correct
18 Correct 416 ms 288756 KB Output is correct
19 Correct 406 ms 288480 KB Output is correct
20 Correct 444 ms 288476 KB Output is correct
21 Correct 387 ms 288408 KB Output is correct
22 Correct 417 ms 288712 KB Output is correct
23 Correct 398 ms 288312 KB Output is correct
24 Correct 416 ms 288284 KB Output is correct
25 Correct 395 ms 288512 KB Output is correct
26 Correct 398 ms 288404 KB Output is correct
27 Correct 419 ms 289212 KB Output is correct
28 Correct 456 ms 289160 KB Output is correct
29 Correct 450 ms 289368 KB Output is correct
30 Correct 325 ms 288152 KB Output is correct
31 Correct 338 ms 288108 KB Output is correct
32 Correct 387 ms 288328 KB Output is correct
33 Correct 424 ms 288796 KB Output is correct
34 Correct 382 ms 288216 KB Output is correct
35 Correct 367 ms 288292 KB Output is correct
36 Correct 468 ms 288708 KB Output is correct
37 Correct 348 ms 288204 KB Output is correct
38 Correct 360 ms 288192 KB Output is correct
39 Correct 458 ms 288884 KB Output is correct
40 Correct 378 ms 288760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 273592 KB Output is correct
2 Correct 303 ms 286928 KB Output is correct
3 Correct 266 ms 286692 KB Output is correct
4 Correct 299 ms 286952 KB Output is correct
5 Correct 265 ms 286672 KB Output is correct
6 Correct 255 ms 287340 KB Output is correct
7 Correct 269 ms 286608 KB Output is correct
8 Correct 244 ms 287360 KB Output is correct
9 Correct 249 ms 286520 KB Output is correct
10 Correct 255 ms 286980 KB Output is correct
11 Correct 243 ms 286636 KB Output is correct
12 Correct 249 ms 286540 KB Output is correct
13 Correct 241 ms 286572 KB Output is correct
14 Correct 252 ms 287028 KB Output is correct
15 Correct 294 ms 287020 KB Output is correct
16 Incorrect 166 ms 273592 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 199 ms 273476 KB Output isn't correct
2 Halted 0 ms 0 KB -