Submission #1044952

#TimeUsernameProblemLanguageResultExecution timeMemory
1044952vjudge1Street Lamps (APIO19_street_lamps)C++17
40 / 100
62 ms22896 KiB
#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define int long long
#define inf ((int)1e18)
using namespace std;

void solve1(int n, int q, string s, vector<array<int, 3> > Q) {
	vector <string> states;
	states.push_back(s);
	for(int i = 0; i < q; i++) {
		auto [t, a, b] = Q[i];
		if(t == 2) {
			int ans = 0;
			for(auto it:states) {
				bool yes = 1;
				for(int i = a - 1; i < b - 1; i++) {
					if(it[i] != '1') {
						yes = 0;
						break;
					}
				}
				ans += yes;
			}
			states.push_back(states.back());
			cout << ans << "\n";
		}
		else {
			int ind = a;
			string p = states.back();
			p[ind - 1] = (p[ind - 1] - '0' + 1) % 2 + '0';
			states.push_back(p);
		}
	}
}

void solve2(int n, int q, string s, vector<array<int, 3> > Q) {
	vector <int> till(n, -1), ans(n); // till[ind] = 0 means closed
	for(int i = 0; i < n; i++) {
		if(s[i] == '1') till[i] = 0;
	}
	for(int i = 1; i <= q; i++) {
		auto [t, a, b] = Q[i - 1];
		if(t == 1) {
			int ind = a;
			ind--;
			if(~till[ind]) {
				ans[ind] += i - till[ind];
				till[ind] = -1;
			}
			else {
				till[ind] = i;
			}
		}
		else {
			a--, b-=2;
			int tmp = (~till[a]) ? i - till[a] : 0;
			cout << tmp + ans[a] << "\n";
		}
	}
}

void solve3(int n, int q, string s, vector<array<int, 3> > Q) {
	
}

int32_t main(){
	fast
	int n, q;
	cin >> n >> q;
	string s;
	cin >> s;
	vector <array<int, 3> > queries;
	bool yes = 0;
	for(int i = 0; i < q; i++) {
		string type;
		cin >> type;
		if(type == "toggle") {
			int ind;
			cin >> ind;
			queries.push_back({1, ind, 0});
		}
		else {
			int a, b;
			cin >> a >> b;
			if(b - a > 1) yes = 1;
			queries.push_back({2, a, b});
		}
	}
	if(!yes) {
		solve2(n, q, s, queries);
	}
	else if(n <= 100 and q <= 100) {
		solve1(n, q, s, queries);
	}
	else {
		solve3(n, q, s, queries);
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...