Submission #673606

# Submission time Handle Problem Language Result Execution time Memory
673606 2022-12-21T10:52:49 Z ghostwriter Street Lamps (APIO19_street_lamps) C++17
100 / 100
2708 ms 149636 KB
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
    #include <debug.h>
#else
    #define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
// #define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); }
void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); }
void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    GOAT
----------------------------------------------------------------
*/
struct Event {
	str type;
	int i, a, b;
	Event() {}
};
struct Toggle {
	int lm, rm, v;
	Toggle() {}
	Toggle(int lm, int rm, int v) : lm(lm), rm(rm), v(v) {}
};
const int oo = 1e9 + 5;
const int N = 3e5 + 5;
int n, q, additionalValue[N];
char s[N];
Event event[N];
Toggle toggleUpdate[N];
vi f[N], f1[N];
void input(int test_id) {
	cin >> n >> q;
	FOR(i, 1, n) cin >> s[i];
	FOR(i, 1, q) {
		cin >> event[i].type;
		if (event[i].type == "toggle") cin >> event[i].i;
		else {
			cin >> event[i].a >> event[i].b;
			--event[i].b;
		}
	}
}
void preUpdate(int x, int y) {
	for (int i = x; i <= n; i += (i & -i)) f1[i].pb(y);
}
void update(int x, int y, int v) {
	for (int i = x; i <= n; i += (i & -i)) {
		int pos = lb(all(f1[i]), y) - f1[i].bg();
		for (int j = pos; j < sz(f1[i]); j += ((j + 1) & -(j + 1))) f[i][j] += v;
	}
}
void preUpdate(int xl, int xr, int yl, int yr) {
	preUpdate(xl, yl);
	if (yr < n) preUpdate(xl, yr + 1);
	if (xr < n) preUpdate(xr + 1, yl);
	if (xr < n && yr < n) preUpdate(xr + 1, yr + 1);
}
void update(int xl, int xr, int yl, int yr, int v) {
	update(xl, yl, v);
	if (yr < n) update(xl, yr + 1, -v);
	if (xr < n) update(xr + 1, yl, -v);
	if (xr < n && yr < n) update(xr + 1, yr + 1, v);
}
void preGetSum(int x, int y) {
	for (int i = x; i > 0; i -= (i & -i)) f1[i].pb(y);
}
int getSum(int x, int y) {
	int rs = 0;
	for (int i = x; i > 0; i -= (i & -i)) {
		int pos = lb(all(f1[i]), y) - f1[i].bg();
		for (int j = pos; j >= 0; j -= ((j + 1) & -(j + 1))) rs += f[i][j];
	}
	return rs;
}
void initFenwickTree() {
	FOR(i, 1, n) {
		if (f1[i].empty()) continue;
		sort(all(f1[i]));
		f[i].resize(sz(f1[i]), 0);
	}
}
void preToggle(multiset<pi> &segment, int i, int j) {
	s[i] ^= 1;
	if (s[i] == '1') {
		int lm = i, rm = i;
		if (s[i - 1] == '1') lm = (*--segment.ub({i, oo})).st;
		if (s[i + 1] == '1') rm = (*segment.ub({i, oo})).nd;
		if (lm == -oo) lm = i;
		if (rm == oo) rm = i;
		preUpdate(lm, i, i, rm);
		toggleUpdate[j] = Toggle(lm, rm, -j);
		if (lm < i) segment.ers(--segment.ub({i, oo}));
		if (rm > i) segment.ers(segment.ub({i, oo}));
		segment.ins({lm, rm});
	}
	else {
		pi tmp = *--segment.ub({i, oo});
		int lm = tmp.st, rm = tmp.nd;
		preUpdate(lm, i, i, rm);
		toggleUpdate[j] = Toggle(lm, rm, j);
		segment.ers(--segment.ub({i, oo}));
		if (lm < i) segment.ins({lm, i - 1});
		if (rm > i) segment.ins({i + 1, rm});
	}
}
void preGetAns(multiset<pi> &segment, int a, int b, int j) {
	preGetSum(a, b);
	pi tmp = *--segment.ub({a, oo});
	if (tmp.st <= a && b <= tmp.nd) additionalValue[j] += j;
}
int getAns(int a, int b, int j) {
	return getSum(a, b) + additionalValue[j];
}
void prepareEvent() {
	multiset<pi> segment;
	segment.ins({-oo, -oo});
	segment.ins({oo, oo});
	int pre = 0;
	FOR(i, 1, n + 1)
		if (i == n + 1 || s[i] == '0') {
			if (pre + 1 < i) segment.ins({pre + 1, i - 1});
			pre = i;
		}
	FOR(j, 1, q)
		if (event[j].type == "toggle") {
			int i = event[j].i;
			preToggle(segment, i, j);
		}
		else {
			int a = event[j].a, b = event[j].b;
			preGetAns(segment, a, b, j);
		}
	initFenwickTree();
}

void solve(int test_id) {
	prepareEvent();
	FOR(j, 1, q) {
		if (event[j].type == "toggle") {
			int i = event[j].i, lm = toggleUpdate[j].lm, rm = toggleUpdate[j].rm, v = toggleUpdate[j].v;
			update(lm, i, i, rm, v);
		}
		else {
			int a = event[j].a, b = event[j].b;
			cout << getAns(a, b, j) << '\n';
		}
	}
}
void reinit(int test_id) {
 
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    int test_num = 1;
    // cin >> test_num; // comment if the problem does not requires multitest
    FOR(i, 1, test_num) {
        input(i); // input in noninteractive problems for case #i
        solve(i); // main function to solve case #i
        reinit(i); // reinit global data to default used in case #i
    }
    #ifdef LOCAL
        cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n";
    #endif
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 16 ms 28448 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 13 ms 28516 KB Output is correct
5 Correct 16 ms 28512 KB Output is correct
6 Correct 15 ms 28500 KB Output is correct
7 Correct 14 ms 28500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 586 ms 56120 KB Output is correct
2 Correct 865 ms 64608 KB Output is correct
3 Correct 1285 ms 79772 KB Output is correct
4 Correct 1839 ms 114976 KB Output is correct
5 Correct 1666 ms 105256 KB Output is correct
6 Correct 1963 ms 119244 KB Output is correct
7 Correct 803 ms 66124 KB Output is correct
8 Correct 815 ms 68744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28756 KB Output is correct
2 Correct 16 ms 28672 KB Output is correct
3 Correct 15 ms 28672 KB Output is correct
4 Correct 16 ms 28500 KB Output is correct
5 Correct 2689 ms 144732 KB Output is correct
6 Correct 2187 ms 124100 KB Output is correct
7 Correct 1628 ms 102752 KB Output is correct
8 Correct 777 ms 69468 KB Output is correct
9 Correct 163 ms 39360 KB Output is correct
10 Correct 176 ms 40388 KB Output is correct
11 Correct 187 ms 40856 KB Output is correct
12 Correct 756 ms 66908 KB Output is correct
13 Correct 770 ms 69512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 14 ms 28628 KB Output is correct
3 Correct 15 ms 28628 KB Output is correct
4 Correct 16 ms 28628 KB Output is correct
5 Correct 895 ms 65280 KB Output is correct
6 Correct 1341 ms 90412 KB Output is correct
7 Correct 1898 ms 120020 KB Output is correct
8 Correct 2708 ms 149636 KB Output is correct
9 Correct 988 ms 75816 KB Output is correct
10 Correct 827 ms 72608 KB Output is correct
11 Correct 1024 ms 76076 KB Output is correct
12 Correct 825 ms 73104 KB Output is correct
13 Correct 1014 ms 76324 KB Output is correct
14 Correct 830 ms 72752 KB Output is correct
15 Correct 766 ms 66948 KB Output is correct
16 Correct 762 ms 69560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 16 ms 28448 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 13 ms 28516 KB Output is correct
5 Correct 16 ms 28512 KB Output is correct
6 Correct 15 ms 28500 KB Output is correct
7 Correct 14 ms 28500 KB Output is correct
8 Correct 586 ms 56120 KB Output is correct
9 Correct 865 ms 64608 KB Output is correct
10 Correct 1285 ms 79772 KB Output is correct
11 Correct 1839 ms 114976 KB Output is correct
12 Correct 1666 ms 105256 KB Output is correct
13 Correct 1963 ms 119244 KB Output is correct
14 Correct 803 ms 66124 KB Output is correct
15 Correct 815 ms 68744 KB Output is correct
16 Correct 16 ms 28756 KB Output is correct
17 Correct 16 ms 28672 KB Output is correct
18 Correct 15 ms 28672 KB Output is correct
19 Correct 16 ms 28500 KB Output is correct
20 Correct 2689 ms 144732 KB Output is correct
21 Correct 2187 ms 124100 KB Output is correct
22 Correct 1628 ms 102752 KB Output is correct
23 Correct 777 ms 69468 KB Output is correct
24 Correct 163 ms 39360 KB Output is correct
25 Correct 176 ms 40388 KB Output is correct
26 Correct 187 ms 40856 KB Output is correct
27 Correct 756 ms 66908 KB Output is correct
28 Correct 770 ms 69512 KB Output is correct
29 Correct 15 ms 28500 KB Output is correct
30 Correct 14 ms 28628 KB Output is correct
31 Correct 15 ms 28628 KB Output is correct
32 Correct 16 ms 28628 KB Output is correct
33 Correct 895 ms 65280 KB Output is correct
34 Correct 1341 ms 90412 KB Output is correct
35 Correct 1898 ms 120020 KB Output is correct
36 Correct 2708 ms 149636 KB Output is correct
37 Correct 988 ms 75816 KB Output is correct
38 Correct 827 ms 72608 KB Output is correct
39 Correct 1024 ms 76076 KB Output is correct
40 Correct 825 ms 73104 KB Output is correct
41 Correct 1014 ms 76324 KB Output is correct
42 Correct 830 ms 72752 KB Output is correct
43 Correct 766 ms 66948 KB Output is correct
44 Correct 762 ms 69560 KB Output is correct
45 Correct 164 ms 42196 KB Output is correct
46 Correct 381 ms 49724 KB Output is correct
47 Correct 989 ms 77208 KB Output is correct
48 Correct 1812 ms 114268 KB Output is correct
49 Correct 206 ms 41528 KB Output is correct
50 Correct 202 ms 41300 KB Output is correct
51 Correct 212 ms 41992 KB Output is correct
52 Correct 223 ms 44272 KB Output is correct
53 Correct 220 ms 43980 KB Output is correct
54 Correct 230 ms 44356 KB Output is correct