답안 #561972

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
561972 2022-05-14T02:06:00 Z hhhhaura 가로등 (APIO19_street_lamps) C++14
100 / 100
1029 ms 134208 KB
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
struct BIT {
	int n;
	vector<int> v;
	vector<pii> op;
	void init_(int _n) {
		n = _n;
		v.assign(n + 2, 0);
	}
	int lowbit(int x) {
		return x & (-x);
	}
	void modify(int x, int val, int yes) {
		if(!x) return;
		if(yes) op.push_back({x, val});
		for(int i = x; i <= n; i += lowbit(i)) {
			v[i] += val;
		}
	}
	int query(int x) {
		int ans = 0;
		if(x < 0) return 0;
		for(int i = x; i > 0; i -= lowbit(i)) {
			ans += v[i];
		}
		return ans;
	}
	void reset() {
		while(op.size()) {
			pii p = op.back();
			op.pop_back();
			modify(p.x, -p.y, 0);
		}
	}
};
namespace solver {
	int n, m;
	string s;
	struct pt { int t, x, y, tp; };
	vector<pt> op;
	set<int> v;
	vector<int> ans, vis, type;
	BIT bit;
	void init_(int _n, int _m, string _s) {
		n = _n, m = _m, s = _s;
		ans.assign(m + 1, 0);
		vis.assign(n + 1, 0);
		type.assign(m + 1, 0);
		bit.init_(n);
	}
	void CDQ(int l, int r) {
		int mid = (l + r) / 2;
		if(l == r) return;
		CDQ(l, mid);
		CDQ(mid + 1, r);
		int ii = l - 1;
		bit.reset();
		rep(i, mid + 1, r) if(op[i].tp == 0) {
			while(ii < mid && op[ii + 1].x >= op[i].x) {
				ii++;
				bit.modify(op[ii].y, op[ii].tp, 1);
			}
			int val = bit.query(n) - bit.query(op[i].y - 1);
			ans[op[i].t] += val;
		}
		vector<pt> tp(r - l + 1, {0, 0, 0, 0});
		merge(op.begin() + l, op.begin() + mid + 1, op.begin() + mid + 1, 
			op.begin() + r + 1, tp.begin(), [](pt a, pt b) { 
				if(a.x != b.x) return a.x > b.x;
				else if(a.y != b.y) return a.y > b.y;
				else return a.tp != 0;
		});
		rep(i, l, r) op[i] = tp[i - l];
		return;
	}
	void operate(int t, int l, int u, int r, int d, int val) {
		op.push_back({t, r, d, val});
		op.push_back({t, l - 1, d, -val});
		op.push_back({t, r, u - 1, -val});
		op.push_back({t, l - 1, u - 1, val});
	}
	void solve() {
		v.insert(0);
		v.insert(n + 1);
		rep(i, 0, n - 1) {
			vis[i + 1] = s[i] == '1';
			if(!vis[i + 1]) v.insert(i + 1);
		}
		rep(i, 1, m) {
			cin >> s;
			if(s == "toggle") {
				int x; cin >> x;
				v.insert(x);
				auto it = v.lower_bound(x);
				int l = *prev(it) + 1;
				int r = *next(it) - 1;
				vis[x] ^= 1;
				if(vis[x]) v.erase(v.find(x));
				int val = vis[x] ? -i : i;
				operate(i, l, x, x, r, val);
			}
			else {
				int l, r; 
				cin >> l >> r;
				op.push_back({i, l, r - 1, 0});
				auto it = v.lower_bound(l);
				if(*it >= r) ans[i] += i; 
				type[i] = 1;
			}
		}
		sort(all(op), [](pt a, pt b) {
			if(a.t != b.t) return a.t < b.t;
			else if(a.x != b.x) return a.x > b.x;
			else if(a.y != b.y) return a.y > b.y;
			else return a.tp != 0;
		});
		CDQ(0, signed(op.size()) - 1);
		rep(i, 1, m) if(type[i]) cout << ans[i] << "\n";
	}


};
using namespace solver;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, m;
	cin >> n >> m;
	string s; cin >> s;
	init_(n, m, s);
	solve();
	return 0;
}

Compilation message

street_lamps.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
    5 | #pragma loop-opt(on)
      | 
street_lamps.cpp:20:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
      |             ^~~~
street_lamps.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
      |                    ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 490 ms 75080 KB Output is correct
2 Correct 506 ms 75428 KB Output is correct
3 Correct 602 ms 76112 KB Output is correct
4 Correct 836 ms 89252 KB Output is correct
5 Correct 800 ms 79976 KB Output is correct
6 Correct 651 ms 96244 KB Output is correct
7 Correct 622 ms 58044 KB Output is correct
8 Correct 393 ms 43856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1029 ms 134208 KB Output is correct
6 Correct 945 ms 103936 KB Output is correct
7 Correct 746 ms 80020 KB Output is correct
8 Correct 410 ms 43900 KB Output is correct
9 Correct 173 ms 28008 KB Output is correct
10 Correct 188 ms 32452 KB Output is correct
11 Correct 179 ms 32428 KB Output is correct
12 Correct 618 ms 57872 KB Output is correct
13 Correct 426 ms 43824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 464 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 560 ms 51544 KB Output is correct
6 Correct 597 ms 75780 KB Output is correct
7 Correct 643 ms 96192 KB Output is correct
8 Correct 720 ms 131308 KB Output is correct
9 Correct 449 ms 91712 KB Output is correct
10 Correct 480 ms 117032 KB Output is correct
11 Correct 434 ms 91832 KB Output is correct
12 Correct 484 ms 116996 KB Output is correct
13 Correct 429 ms 91612 KB Output is correct
14 Correct 491 ms 117024 KB Output is correct
15 Correct 631 ms 57892 KB Output is correct
16 Correct 415 ms 43916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 490 ms 75080 KB Output is correct
9 Correct 506 ms 75428 KB Output is correct
10 Correct 602 ms 76112 KB Output is correct
11 Correct 836 ms 89252 KB Output is correct
12 Correct 800 ms 79976 KB Output is correct
13 Correct 651 ms 96244 KB Output is correct
14 Correct 622 ms 58044 KB Output is correct
15 Correct 393 ms 43856 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1029 ms 134208 KB Output is correct
21 Correct 945 ms 103936 KB Output is correct
22 Correct 746 ms 80020 KB Output is correct
23 Correct 410 ms 43900 KB Output is correct
24 Correct 173 ms 28008 KB Output is correct
25 Correct 188 ms 32452 KB Output is correct
26 Correct 179 ms 32428 KB Output is correct
27 Correct 618 ms 57872 KB Output is correct
28 Correct 426 ms 43824 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 2 ms 464 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 560 ms 51544 KB Output is correct
34 Correct 597 ms 75780 KB Output is correct
35 Correct 643 ms 96192 KB Output is correct
36 Correct 720 ms 131308 KB Output is correct
37 Correct 449 ms 91712 KB Output is correct
38 Correct 480 ms 117032 KB Output is correct
39 Correct 434 ms 91832 KB Output is correct
40 Correct 484 ms 116996 KB Output is correct
41 Correct 429 ms 91612 KB Output is correct
42 Correct 491 ms 117024 KB Output is correct
43 Correct 631 ms 57892 KB Output is correct
44 Correct 415 ms 43916 KB Output is correct
45 Correct 273 ms 48572 KB Output is correct
46 Correct 315 ms 48840 KB Output is correct
47 Correct 468 ms 53816 KB Output is correct
48 Correct 842 ms 89368 KB Output is correct
49 Correct 212 ms 36052 KB Output is correct
50 Correct 215 ms 35992 KB Output is correct
51 Correct 217 ms 36188 KB Output is correct
52 Correct 220 ms 36540 KB Output is correct
53 Correct 233 ms 36628 KB Output is correct
54 Correct 227 ms 36672 KB Output is correct