Submission #562620

# Submission time Handle Problem Language Result Execution time Memory
562620 2022-05-14T20:19:06 Z Bungmint Street Lamps (APIO19_street_lamps) C++17
100 / 100
1301 ms 81184 KB
// Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound

constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
const ld PI = acos((ld)-1.0);
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
	return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
	os << '{';
	string sep;
	for (const T &x : v)
		os << sep << x, sep = ", ";
	return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &v) {
	os << vector<T>(all(v));
	return os;
}
template <typename T, typename S, typename C>
ostream &operator<<(ostream &os, priority_queue<T, S, C> pq) {
	vector<T> v;
	while (sz(pq)) {
		v.pb(pq.top());
		pq.pop();
	}
	os << v;
	return os;
}
void dbg_out()
{
	cerr << "\033[0m" << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
	cerr << ' ' << H;
	dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "\033[1;35m(" << #__VA_ARGS__ << "):\033[33m", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda
{
	template <typename Fun>
	struct y_combinator_result
	{
		Fun fun_;
		template <typename T>
		explicit y_combinator_result(T &&fun) : fun_(forward<T>(fun)) {}
		template <typename... Args>
		decltype(auto) operator()(Args &&...args)
		{
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun)
	{
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

inline namespace Range {
	class ForwardRange {
		int src, dst;

	  public:
	  	explicit constexpr ForwardRange(const int l, const int r) : src(l), dst(r) {}
		explicit constexpr ForwardRange(const int n) : src(0), dst(n) {}
		constexpr ForwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src < dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return src++; }
	};
	class BackwardRange {
		int src, dst;

	  public:
	  	explicit constexpr BackwardRange(const int l, const int r) : src(r), dst(l) {}
		explicit constexpr BackwardRange(const int n) : src(n), dst(0) {}
		constexpr BackwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src > dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return --src; }
	};
	using rep = ForwardRange;
	using per = BackwardRange;
};

const int MX = 3e5;

struct BIT {
	vi ind;
	vl t;
	bool mode = 0;
	BIT() {}
	int atMost(int i) {
		return ub(all(ind), i) - ind.begin();
	}
	void upd(int i, int v) {
		if (!mode) {
			ind.pb(i);
			return;
		}
		int it = atMost(i);
		if (it == 0 || ind[it - 1] != i) it++;
		for (; it <= sz(ind); it += it & -it) {
			t[it - 1] += v;
		}
	}
	ll query(int i) {
		ll ret{};
		for (int it = atMost(i); it; it -= it & -it) {
			ret += t[it - 1];
		}
		return ret;
	}
	ll query(int l, int r) {
		return query(r) - query(l - 1);
	}
	void init() {
		mode = 1;
		sort(all(ind)), ind.resize(unique(all(ind)) - ind.begin());
		t.resize(sz(ind));
	}
}bit[MX];

int n, q;

void fakeUpd(int a, int b) {
	for (; a; a -= a & -a) {
		bit[a - 1].upd(b, 0);
		dbg(a, b);
	}
}
void init() {
	for (int i : rep(n)) bit[i].init();
}
void upd(int x, int y, int v) {
	for (; x <= n; x += x & -x) {
		dbg(x, y, v);
		dbg(bit[x - 1].ind);
		bit[x - 1].upd(y, v);
		dbg(x, bit[x - 1].t);
	}
}
void upd(int l, int i, int r, int v) {
	upd(l, i, v);
	upd(i + 1, i, -v);
	upd(l, r + 1, -v);
	upd(i + 1, r + 1, v);

}
int qq(int x, int y) {
	int ret = 0;
	for (; x; x -= x & -x) {
		ret += bit[x - 1].query(y);
		if (y == 3) dbg(x, y, bit[x - 1].ind);
	}
	return ret;
}



void solve()
{
	cin >> n >> q;
	string s;
	cin >> s;
	set<int> st; st.insert(0), st.insert(n + 1);
	for (int i : rep(n)) if (s[i] == '0') st.insert(i + 1);
	vector<pair<char, pii>> query(q);
	for (int i : rep(q)) {
		string t;
		cin >> t;
		if (t[0] == 'q') {
			int a, b;
			cin >> a >> b;
			b--;
			fakeUpd(a, b);
			query[i] = {t[0], {a, b}};
		}else{
			int j;
			cin >> j;
			query[i] = {t[0], {j, j}};
		}
	}
	init();
	for (int i : rep(q)) {
		auto &[t, p] = query[i];
		if (t == 'q') {
			int z = qq(p.fi, p.se) + (*st.lb(p.fi) > p.se ? i + 1 : 0);
			cout << z << '\n';
		}else{
			int L, R, v;
			auto j = p.fi;
			if (st.count(j)) {
				v = -(i + 1);
				auto it = st.lb(j);
				L = *prev(it) + 1;
				R = *next(it) - 1;
				st.erase(it);
			}else{
				v = (i + 1);
				auto it = st.insert(j).fi;
				L = *prev(it) + 1;
				R = *next(it) - 1;
			}
			upd(L, j, R, v);
		}
	}
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
	{
		solve();
	}
#ifdef LOCAL
	cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}

Compilation message

street_lamps.cpp: In function 'void fakeUpd(int, int)':
street_lamps.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
street_lamps.cpp:176:3: note: in expansion of macro 'dbg'
  176 |   dbg(a, b);
      |   ^~~
street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
street_lamps.cpp:184:3: note: in expansion of macro 'dbg'
  184 |   dbg(x, y, v);
      |   ^~~
street_lamps.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
street_lamps.cpp:185:3: note: in expansion of macro 'dbg'
  185 |   dbg(bit[x - 1].ind);
      |   ^~~
street_lamps.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
street_lamps.cpp:187:3: note: in expansion of macro 'dbg'
  187 |   dbg(x, bit[x - 1].t);
      |   ^~~
street_lamps.cpp: In function 'int qq(int, int)':
street_lamps.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
street_lamps.cpp:201:15: note: in expansion of macro 'dbg'
  201 |   if (y == 3) dbg(x, y, bit[x - 1].ind);
      |               ^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16752 KB Output is correct
3 Correct 9 ms 16756 KB Output is correct
4 Correct 9 ms 16756 KB Output is correct
5 Correct 9 ms 16724 KB Output is correct
6 Correct 11 ms 16704 KB Output is correct
7 Correct 9 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 26764 KB Output is correct
2 Correct 199 ms 27492 KB Output is correct
3 Correct 353 ms 29728 KB Output is correct
4 Correct 1154 ms 55828 KB Output is correct
5 Correct 1071 ms 58648 KB Output is correct
6 Correct 995 ms 51944 KB Output is correct
7 Correct 1301 ms 77568 KB Output is correct
8 Correct 879 ms 64812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16724 KB Output is correct
2 Correct 10 ms 16836 KB Output is correct
3 Correct 10 ms 16772 KB Output is correct
4 Correct 9 ms 16868 KB Output is correct
5 Correct 650 ms 39712 KB Output is correct
6 Correct 944 ms 50516 KB Output is correct
7 Correct 1000 ms 58392 KB Output is correct
8 Correct 894 ms 68592 KB Output is correct
9 Correct 132 ms 26928 KB Output is correct
10 Correct 134 ms 27532 KB Output is correct
11 Correct 131 ms 27956 KB Output is correct
12 Correct 1246 ms 81184 KB Output is correct
13 Correct 930 ms 68692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 16852 KB Output is correct
2 Correct 10 ms 16776 KB Output is correct
3 Correct 10 ms 16852 KB Output is correct
4 Correct 9 ms 16772 KB Output is correct
5 Correct 1148 ms 73808 KB Output is correct
6 Correct 1069 ms 63628 KB Output is correct
7 Correct 939 ms 51948 KB Output is correct
8 Correct 575 ms 32736 KB Output is correct
9 Correct 265 ms 26032 KB Output is correct
10 Correct 185 ms 23328 KB Output is correct
11 Correct 250 ms 26004 KB Output is correct
12 Correct 184 ms 23420 KB Output is correct
13 Correct 243 ms 26060 KB Output is correct
14 Correct 190 ms 23340 KB Output is correct
15 Correct 1197 ms 81160 KB Output is correct
16 Correct 869 ms 68496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16752 KB Output is correct
3 Correct 9 ms 16756 KB Output is correct
4 Correct 9 ms 16756 KB Output is correct
5 Correct 9 ms 16724 KB Output is correct
6 Correct 11 ms 16704 KB Output is correct
7 Correct 9 ms 16724 KB Output is correct
8 Correct 163 ms 26764 KB Output is correct
9 Correct 199 ms 27492 KB Output is correct
10 Correct 353 ms 29728 KB Output is correct
11 Correct 1154 ms 55828 KB Output is correct
12 Correct 1071 ms 58648 KB Output is correct
13 Correct 995 ms 51944 KB Output is correct
14 Correct 1301 ms 77568 KB Output is correct
15 Correct 879 ms 64812 KB Output is correct
16 Correct 10 ms 16724 KB Output is correct
17 Correct 10 ms 16836 KB Output is correct
18 Correct 10 ms 16772 KB Output is correct
19 Correct 9 ms 16868 KB Output is correct
20 Correct 650 ms 39712 KB Output is correct
21 Correct 944 ms 50516 KB Output is correct
22 Correct 1000 ms 58392 KB Output is correct
23 Correct 894 ms 68592 KB Output is correct
24 Correct 132 ms 26928 KB Output is correct
25 Correct 134 ms 27532 KB Output is correct
26 Correct 131 ms 27956 KB Output is correct
27 Correct 1246 ms 81184 KB Output is correct
28 Correct 930 ms 68692 KB Output is correct
29 Correct 12 ms 16852 KB Output is correct
30 Correct 10 ms 16776 KB Output is correct
31 Correct 10 ms 16852 KB Output is correct
32 Correct 9 ms 16772 KB Output is correct
33 Correct 1148 ms 73808 KB Output is correct
34 Correct 1069 ms 63628 KB Output is correct
35 Correct 939 ms 51948 KB Output is correct
36 Correct 575 ms 32736 KB Output is correct
37 Correct 265 ms 26032 KB Output is correct
38 Correct 185 ms 23328 KB Output is correct
39 Correct 250 ms 26004 KB Output is correct
40 Correct 184 ms 23420 KB Output is correct
41 Correct 243 ms 26060 KB Output is correct
42 Correct 190 ms 23340 KB Output is correct
43 Correct 1197 ms 81160 KB Output is correct
44 Correct 869 ms 68496 KB Output is correct
45 Correct 84 ms 22308 KB Output is correct
46 Correct 127 ms 22944 KB Output is correct
47 Correct 509 ms 37244 KB Output is correct
48 Correct 1079 ms 55828 KB Output is correct
49 Correct 148 ms 28660 KB Output is correct
50 Correct 142 ms 28492 KB Output is correct
51 Correct 165 ms 29112 KB Output is correct
52 Correct 176 ms 31532 KB Output is correct
53 Correct 205 ms 31384 KB Output is correct
54 Correct 185 ms 31480 KB Output is correct