답안 #617172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
617172 2022-08-01T09:20:04 Z cheissmart Fish 2 (JOI22_fish2) C++14
60 / 100
4000 ms 253256 KB
// 花啊啊啊啊啊啊啊啊啊啊啊啊
#include <iostream>
#pragma GCC optimize("Ofast")
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int INF = 1e9 + 7, N = 1e5 + 2;
const ll oo = 1e16;

ll a[N];
int pw2[20];
int n;

namespace cover {
	int t[N * 2];
	char tag[N * 2];
	int sum(int v, int h) { return tag[v] ? (1 << h) : t[v]; }
	void pull(int p) {
		int h = 0;
	    while(p > 1) {
	        t[p >> 1] = sum(p, h) + sum(p ^ 1, h);
	        p >>= 1;
	        h++;
	    }
	}
	void add(int l, int r, int x) {
		if(l == 1 && r == n + 1) return;
		l--, r--;
		int tl = l, tr = r;
	    for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
	    	if(l & 1) tag[l++] += x;
	    	if(r & 1) tag[--r] += x;
	    }
	    pull(tl + n), pull(tr - 1 + n);
	}
	int qry(int l, int r) {
		l--, r--; 
	    int res = 0, h = 0;
	    for(l += n, r += n; l < r; l >>= 1, r >>= 1, h++) {
	        if(l & 1) res += sum(l++, h);
	        if(r & 1) res += sum(--r, h);
	    }
	    return res;
	}
}

namespace seg {
	template<class A, class B, int SZ> struct DS {
		pair<A, B> a[SZ];
		int sz;
		DS() { sz = 0; }
		void clear() { sz = 0; }
		void EB(A x, B y) { a[sz++] = {x, y}; }
		void PB(pair<A, B> p) { a[sz++] = p; }
		pair<A, B>& operator[](int i) { return a[i]; }
		void resize(int s) { sz = s; }
	};

	struct node {
		DS<int, ll, 31> bad_pref, bad_suff;
		DS<int, int, 31> bad_seg;
		ll sum;
	} t[N * 2];

	int id(int tl, int tr) {
		tl--, tr -= 2;
	    return (tl + tr) | (tl != tr);
	}

	void pull(int v, int tl, int tr, int lc, int rc, int posl = INF, int posr = 0) {
		for(int i = 0; i < t[lc].bad_pref.sz; i++)
			t[v].bad_pref.PB(t[lc].bad_pref[i]);
		for(int i = 0; i < t[rc].bad_suff.sz; i++)
			t[v].bad_suff.PB(t[rc].bad_suff[i]);

		for(int i = 0; i < t[rc].bad_pref.sz; i++) {
			ll sum1 = t[rc].bad_pref[i].S + t[lc].sum;
			int pos1 = t[rc].bad_pref[i].F;
			if(sum1 + a[tl - 1] < a[pos1 + 1])
				t[v].bad_pref.EB(pos1, sum1);
		}
		for(int i = 0; i < t[lc].bad_suff.sz; i++) {
			ll sum1 = t[lc].bad_suff[i].S + t[rc].sum;
			int pos1 = t[lc].bad_suff[i].F;
			if(sum1 + a[tr] < a[pos1 - 1])
				t[v].bad_suff.EB(pos1, sum1);
		}

		int szi = t[lc].bad_suff.sz;
		int szj = t[rc].bad_pref.sz;
		int i = 0, j = 0;
		while(i < szi && t[lc].bad_suff[i].F > posl)
			i++;
		while(j < szj && t[rc].bad_pref[j].F < posr)
			j++;
		while(i < szi && j < szj) {
			auto& he = t[lc].bad_suff[i], be = t[rc].bad_pref[j];
			if(he.S + be.S < min(a[he.F - 1], a[be.F + 1])) {
				t[v].bad_seg.EB(he.F, be.F);
				cover::add(he.F, be.F + 1, 1);
			}
			if(a[he.F - 1] < a[be.F + 1])
				i++;
			else
				j++;
		}
		t[v].sum = t[lc].sum + t[rc].sum;
	}
	void build( int tl = 1, int tr = n + 1) {
		int v = id(tl, tr);
		if(tr - tl == 1) {
			t[v].sum = a[tl];
			if(a[tl] + a[tl - 1] < a[tl + 1])
				t[v].bad_pref.EB(tl, a[tl]);
			if(a[tl] + a[tl + 1] < a[tl - 1])
				t[v].bad_suff.EB(tl, a[tl]);
			if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1]) {
				t[v].bad_seg.EB(tl, tl);
				cover::add(tl, tl + 1, 1);
			}
		} else {
			int tm = (tl + tr + 1) / 2;
			build(tl, tm);
			build(tm, tr);
			pull(v, tl, tr, id(tl, tm), id(tm, tr));
		}
	}
	void upd(int pos, int tl = 1, int tr = n + 1) {
		int v = id(tl, tr);
		int j = 0;
		for(int i = 0; i < t[v].bad_seg.sz; i++) {
			auto [l, r] = t[v].bad_seg[i];
			if(r < pos - 1 || l > pos + 1) {
				t[v].bad_seg[j++] = t[v].bad_seg[i];
			} else {
				cover::add(l, r + 1, -1);
			}
		}
		t[v].bad_seg.resize(j);

		t[v].bad_pref.clear(), t[v].bad_suff.clear();
		if(tr - tl == 1) {
			t[v].sum = a[tl];
			if(a[tl] + a[tl - 1] < a[tl + 1])
				t[v].bad_pref.EB(tl, a[tl]);
			if(a[tl] + a[tl + 1] < a[tl - 1])
				t[v].bad_suff.EB(tl, a[tl]);
			if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1]) {
				t[v].bad_seg.EB(tl, tl);
				cover::add(tl, tl + 1, 1);
			}
		} else {
			int tm = (tl + tr + 1) / 2;
			if(pos - 1 < tm)
				upd(pos, tl, tm);
			if(pos + 1 >= tm)
				upd(pos, tm, tr);
			pull(v, tl, tr, id(tl, tm), id(tm, tr), pos + 1, pos - 1);
		}
	}
}

void upd(int pos, ll val) {
	a[pos] = val;
	seg::upd(pos);
}

#define gc getchar_unlocked
inline int readInt () {
	int c = gc();
	while(!isdigit(c)) c = gc();
	int res = c - '0';
	while(isdigit(c = gc())) res = res * 10 + c - '0';
	return res;
}

signed main()
{
	IO_OP;

	n = readInt();
	a[0] = a[n + 1] = oo;
	for(int i = 1; i <= n; i++)
		a[i] = readInt();

	seg::build();

	int q = readInt();
	while(q--) {
		int op = readInt();
		if(op == 1) {
			int x = readInt(), y = readInt();
			upd(x, y);
		} else {
			int l = readInt(), r = readInt();
			ll tl = a[l - 1], tr = a[r + 1];

			if(l - 1 >= 1) upd(l - 1, 1e15);
			if(r + 1 <= n) upd(r + 1, 1e15);
			if(l != 1 || r != n)
				cover::add(l, r + 1, -1);

			cout << r - l + 1 - cover::qry(l, r + 1) << '\n';

			if(l != 1 || r != n)
				cover::add(l, r + 1, 1);

			if(l - 1 >= 1) upd(l - 1, tl);
			if(r + 1 <= n) upd(r + 1, tr);
		}
	}

}

Compilation message

fish2.cpp: In function 'void seg::upd(int, int, int)':
fish2.cpp:143:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |    auto [l, r] = t[v].bad_seg[i];
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 249152 KB Output is correct
2 Correct 123 ms 249168 KB Output is correct
3 Correct 120 ms 249164 KB Output is correct
4 Correct 110 ms 249204 KB Output is correct
5 Correct 118 ms 249160 KB Output is correct
6 Correct 129 ms 249300 KB Output is correct
7 Correct 135 ms 249248 KB Output is correct
8 Correct 115 ms 249188 KB Output is correct
9 Correct 127 ms 249148 KB Output is correct
10 Correct 114 ms 249248 KB Output is correct
11 Correct 114 ms 249192 KB Output is correct
12 Correct 131 ms 249244 KB Output is correct
13 Correct 124 ms 249164 KB Output is correct
14 Correct 112 ms 249164 KB Output is correct
15 Correct 122 ms 249232 KB Output is correct
16 Correct 150 ms 249228 KB Output is correct
17 Correct 111 ms 249148 KB Output is correct
18 Correct 116 ms 249344 KB Output is correct
19 Correct 110 ms 249192 KB Output is correct
20 Correct 126 ms 249276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 249136 KB Output is correct
2 Correct 139 ms 251144 KB Output is correct
3 Correct 158 ms 250816 KB Output is correct
4 Correct 163 ms 251156 KB Output is correct
5 Correct 163 ms 250848 KB Output is correct
6 Correct 153 ms 251460 KB Output is correct
7 Correct 150 ms 250792 KB Output is correct
8 Correct 159 ms 251524 KB Output is correct
9 Correct 142 ms 250800 KB Output is correct
10 Correct 141 ms 251072 KB Output is correct
11 Correct 146 ms 250808 KB Output is correct
12 Correct 170 ms 250824 KB Output is correct
13 Correct 127 ms 250824 KB Output is correct
14 Correct 144 ms 251076 KB Output is correct
15 Correct 143 ms 251220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 249152 KB Output is correct
2 Correct 123 ms 249168 KB Output is correct
3 Correct 120 ms 249164 KB Output is correct
4 Correct 110 ms 249204 KB Output is correct
5 Correct 118 ms 249160 KB Output is correct
6 Correct 129 ms 249300 KB Output is correct
7 Correct 135 ms 249248 KB Output is correct
8 Correct 115 ms 249188 KB Output is correct
9 Correct 127 ms 249148 KB Output is correct
10 Correct 114 ms 249248 KB Output is correct
11 Correct 114 ms 249192 KB Output is correct
12 Correct 131 ms 249244 KB Output is correct
13 Correct 124 ms 249164 KB Output is correct
14 Correct 112 ms 249164 KB Output is correct
15 Correct 122 ms 249232 KB Output is correct
16 Correct 150 ms 249228 KB Output is correct
17 Correct 111 ms 249148 KB Output is correct
18 Correct 116 ms 249344 KB Output is correct
19 Correct 110 ms 249192 KB Output is correct
20 Correct 126 ms 249276 KB Output is correct
21 Correct 112 ms 249136 KB Output is correct
22 Correct 139 ms 251144 KB Output is correct
23 Correct 158 ms 250816 KB Output is correct
24 Correct 163 ms 251156 KB Output is correct
25 Correct 163 ms 250848 KB Output is correct
26 Correct 153 ms 251460 KB Output is correct
27 Correct 150 ms 250792 KB Output is correct
28 Correct 159 ms 251524 KB Output is correct
29 Correct 142 ms 250800 KB Output is correct
30 Correct 141 ms 251072 KB Output is correct
31 Correct 146 ms 250808 KB Output is correct
32 Correct 170 ms 250824 KB Output is correct
33 Correct 127 ms 250824 KB Output is correct
34 Correct 144 ms 251076 KB Output is correct
35 Correct 143 ms 251220 KB Output is correct
36 Correct 173 ms 251116 KB Output is correct
37 Correct 190 ms 250924 KB Output is correct
38 Correct 188 ms 250888 KB Output is correct
39 Correct 185 ms 251248 KB Output is correct
40 Correct 178 ms 250868 KB Output is correct
41 Correct 158 ms 251612 KB Output is correct
42 Correct 164 ms 251444 KB Output is correct
43 Correct 170 ms 250828 KB Output is correct
44 Correct 179 ms 250804 KB Output is correct
45 Correct 182 ms 251184 KB Output is correct
46 Correct 170 ms 251136 KB Output is correct
47 Correct 198 ms 250892 KB Output is correct
48 Correct 143 ms 250844 KB Output is correct
49 Correct 154 ms 250812 KB Output is correct
50 Correct 172 ms 251184 KB Output is correct
51 Correct 160 ms 251212 KB Output is correct
52 Correct 172 ms 251220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 249136 KB Output is correct
2 Correct 139 ms 251144 KB Output is correct
3 Correct 158 ms 250816 KB Output is correct
4 Correct 163 ms 251156 KB Output is correct
5 Correct 163 ms 250848 KB Output is correct
6 Correct 153 ms 251460 KB Output is correct
7 Correct 150 ms 250792 KB Output is correct
8 Correct 159 ms 251524 KB Output is correct
9 Correct 142 ms 250800 KB Output is correct
10 Correct 141 ms 251072 KB Output is correct
11 Correct 146 ms 250808 KB Output is correct
12 Correct 170 ms 250824 KB Output is correct
13 Correct 127 ms 250824 KB Output is correct
14 Correct 144 ms 251076 KB Output is correct
15 Correct 143 ms 251220 KB Output is correct
16 Correct 116 ms 249168 KB Output is correct
17 Execution timed out 4022 ms 252508 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 249136 KB Output is correct
2 Correct 139 ms 251144 KB Output is correct
3 Correct 158 ms 250816 KB Output is correct
4 Correct 163 ms 251156 KB Output is correct
5 Correct 163 ms 250848 KB Output is correct
6 Correct 153 ms 251460 KB Output is correct
7 Correct 150 ms 250792 KB Output is correct
8 Correct 159 ms 251524 KB Output is correct
9 Correct 142 ms 250800 KB Output is correct
10 Correct 141 ms 251072 KB Output is correct
11 Correct 146 ms 250808 KB Output is correct
12 Correct 170 ms 250824 KB Output is correct
13 Correct 127 ms 250824 KB Output is correct
14 Correct 144 ms 251076 KB Output is correct
15 Correct 143 ms 251220 KB Output is correct
16 Correct 126 ms 249232 KB Output is correct
17 Correct 706 ms 252136 KB Output is correct
18 Correct 563 ms 252652 KB Output is correct
19 Correct 530 ms 252256 KB Output is correct
20 Correct 535 ms 252560 KB Output is correct
21 Correct 637 ms 252160 KB Output is correct
22 Correct 562 ms 252676 KB Output is correct
23 Correct 582 ms 252148 KB Output is correct
24 Correct 519 ms 252624 KB Output is correct
25 Correct 643 ms 252152 KB Output is correct
26 Correct 267 ms 253164 KB Output is correct
27 Correct 340 ms 253200 KB Output is correct
28 Correct 436 ms 252420 KB Output is correct
29 Correct 319 ms 253256 KB Output is correct
30 Correct 404 ms 253204 KB Output is correct
31 Correct 461 ms 252520 KB Output is correct
32 Correct 533 ms 252536 KB Output is correct
33 Correct 339 ms 252140 KB Output is correct
34 Correct 496 ms 252892 KB Output is correct
35 Correct 304 ms 252540 KB Output is correct
36 Correct 485 ms 252564 KB Output is correct
37 Correct 486 ms 252260 KB Output is correct
38 Correct 335 ms 252328 KB Output is correct
39 Correct 336 ms 252552 KB Output is correct
40 Correct 234 ms 252380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 249152 KB Output is correct
2 Correct 123 ms 249168 KB Output is correct
3 Correct 120 ms 249164 KB Output is correct
4 Correct 110 ms 249204 KB Output is correct
5 Correct 118 ms 249160 KB Output is correct
6 Correct 129 ms 249300 KB Output is correct
7 Correct 135 ms 249248 KB Output is correct
8 Correct 115 ms 249188 KB Output is correct
9 Correct 127 ms 249148 KB Output is correct
10 Correct 114 ms 249248 KB Output is correct
11 Correct 114 ms 249192 KB Output is correct
12 Correct 131 ms 249244 KB Output is correct
13 Correct 124 ms 249164 KB Output is correct
14 Correct 112 ms 249164 KB Output is correct
15 Correct 122 ms 249232 KB Output is correct
16 Correct 150 ms 249228 KB Output is correct
17 Correct 111 ms 249148 KB Output is correct
18 Correct 116 ms 249344 KB Output is correct
19 Correct 110 ms 249192 KB Output is correct
20 Correct 126 ms 249276 KB Output is correct
21 Correct 112 ms 249136 KB Output is correct
22 Correct 139 ms 251144 KB Output is correct
23 Correct 158 ms 250816 KB Output is correct
24 Correct 163 ms 251156 KB Output is correct
25 Correct 163 ms 250848 KB Output is correct
26 Correct 153 ms 251460 KB Output is correct
27 Correct 150 ms 250792 KB Output is correct
28 Correct 159 ms 251524 KB Output is correct
29 Correct 142 ms 250800 KB Output is correct
30 Correct 141 ms 251072 KB Output is correct
31 Correct 146 ms 250808 KB Output is correct
32 Correct 170 ms 250824 KB Output is correct
33 Correct 127 ms 250824 KB Output is correct
34 Correct 144 ms 251076 KB Output is correct
35 Correct 143 ms 251220 KB Output is correct
36 Correct 173 ms 251116 KB Output is correct
37 Correct 190 ms 250924 KB Output is correct
38 Correct 188 ms 250888 KB Output is correct
39 Correct 185 ms 251248 KB Output is correct
40 Correct 178 ms 250868 KB Output is correct
41 Correct 158 ms 251612 KB Output is correct
42 Correct 164 ms 251444 KB Output is correct
43 Correct 170 ms 250828 KB Output is correct
44 Correct 179 ms 250804 KB Output is correct
45 Correct 182 ms 251184 KB Output is correct
46 Correct 170 ms 251136 KB Output is correct
47 Correct 198 ms 250892 KB Output is correct
48 Correct 143 ms 250844 KB Output is correct
49 Correct 154 ms 250812 KB Output is correct
50 Correct 172 ms 251184 KB Output is correct
51 Correct 160 ms 251212 KB Output is correct
52 Correct 172 ms 251220 KB Output is correct
53 Correct 116 ms 249168 KB Output is correct
54 Execution timed out 4022 ms 252508 KB Time limit exceeded
55 Halted 0 ms 0 KB -