Submission #336877

# Submission time Handle Problem Language Result Execution time Memory
336877 2020-12-17T06:23:09 Z oolimry Street Lamps (APIO19_street_lamps) C++14
100 / 100
1421 ms 391704 KB
#include <bits/stdc++.h>
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int,int> ii;

int n, Q, SZ; 

struct query{
	int l, r, t, ans;
	bool operator< (const query &Q){
		return this->r > Q.r;
	}
};
struct update{
	int l, r, t;
	bool operator< (const update &U){
		return this->r > U.r;
	}
};

namespace fenwick{
	int n = 300005;
	int tree[300007];
	vector<ii> toundo;
	void update(int i, int v, bool undo = false){
		if(!undo) toundo.push_back(ii(i,v));
		for(;i <= n;i += i&(-i)) tree[i] += v;
	}
	int query(int i){
		int ans = 0;
		for(;i > 0;i -= i&(-i)) ans += tree[i];
		return ans;
	}
	void undo(){
		for(ii B : toundo) update(B.first, -B.second, true);
		toundo.clear();
	}
};


set<ii> pairs;

vector<int> leafs;
vector<query> queries[600006];
vector<update> updates[600006];

void makeupdate(int l, int r, int t, bool add){
	if(add){
		pairs.insert(ii(l,r));
		updates[leafs[t]].push_back({l,r,-t});
	}
	else{
		pairs.erase(ii(l,r));
		updates[leafs[t]].push_back({l,r,t});
	}
}

void makequery(int l, int r, int t){
	auto it = pairs.upper_bound(ii(l,1e9));
	int ans = 0;
	
	if(it != pairs.begin()){
		it--;
		if(it->first <= l && r <= it->second) ans = t;
	}
	
	queries[leafs[t]].push_back({l, r, t, ans}); 
}

void dfs(int u){
	if(u >= 2*SZ) return;
	if(u >= SZ) leafs.push_back(u);
	dfs(u<<1); dfs(u<<1|1);
}

int arr[300005];
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	cin >> n >> Q; SZ = Q+1;
	
	string original; cin >> original;
	for(int i = 1;i <= n;i++) arr[i] = original[i-1] - '0';
	
	
	int l = -1;
	for(int i = 1;i <= n;i++){
		if(arr[i] == 1 && l == -1) l = i;
		if(arr[i] == 1 && arr[i+1] == 0){
			pairs.insert(ii(l,i));
			l = -1;
		}
	}
	
	for(int i = 1;i <= n;i++) assert(fenwick::tree[i] == 0);
	dfs(1);
	
	for(int t = 1;t <= Q;t++){
		string s; cin >> s;
		
		if(s[0] == 't'){
			int x; cin >> x;
			if(arr[x] == 0){ ///turn on
				makeupdate(x,x,t,true);
				if(arr[x-1] == 1){
					auto it = pairs.lower_bound(ii(x,-1));
					auto pre = prev(it);
					
					makeupdate(pre->first, it->second, t, true);
					makeupdate(pre->first, pre->second, t, false);
					makeupdate(it->first, it->second, t, false);
				}
				if(arr[x+1] == 1){
					auto nxt = pairs.upper_bound(ii(x,1e9));
					auto it = prev(nxt);
					
					makeupdate(it->first, nxt->second, t, true);
					makeupdate(nxt->first, nxt->second, t, false);
					makeupdate(it->first, it->second, t, false);
				}
				arr[x] = 1;
			}
			else{
				auto it = --pairs.upper_bound(ii(x,1e9));
				
				if(it->first != x) makeupdate(it->first, x-1, t, true); // pairs.insert(ii(it->first,x-1));
				if(it->second != x) makeupdate(x+1, it->second, t, true);
				
				pairs.erase(it);
				makeupdate(it->first, it->second, t, false);
				
				arr[x] = 0;
			}
		}
		else{
			int l, r; cin >> l >> r;
			makequery(l, r-1, t);
		}
	}
			
	for(int node = SZ;node < 2*SZ;node++){
		sort(all(queries[node]));
		sort(all(updates[node]));
	}
		
	for(int node = SZ-1;node >= 1;node--){
		int LC = node<<1, RC = node<<1|1;
		
		auto it = updates[LC].begin();
		
		for(query &q : queries[RC]){
			while(it != updates[LC].end()){
				if(it->r < q.r) break;
				else{
					fenwick::update(it->l, it->t);
					it++;
				}
			}
			q.ans += fenwick::query(q.l);
		}
		
		fenwick::undo();
		
		merge(all(queries[LC]), all(queries[RC]), back_inserter(queries[node]));
		merge(all(updates[LC]), all(updates[RC]), back_inserter(updates[node]));
	}
	
	sort(all(queries[1]), [&](query a, query b){ return a.t < b.t; });
	for(query q : queries[1]) cout << q.ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28524 KB Output is correct
2 Correct 19 ms 28524 KB Output is correct
3 Correct 19 ms 28652 KB Output is correct
4 Correct 21 ms 28652 KB Output is correct
5 Correct 19 ms 28652 KB Output is correct
6 Correct 19 ms 28524 KB Output is correct
7 Correct 19 ms 28524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 837 ms 232068 KB Output is correct
2 Correct 839 ms 232980 KB Output is correct
3 Correct 850 ms 233940 KB Output is correct
4 Correct 963 ms 240880 KB Output is correct
5 Correct 1231 ms 347412 KB Output is correct
6 Correct 719 ms 247148 KB Output is correct
7 Correct 371 ms 144624 KB Output is correct
8 Correct 384 ms 144752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 29292 KB Output is correct
2 Correct 21 ms 29420 KB Output is correct
3 Correct 21 ms 29292 KB Output is correct
4 Correct 20 ms 28780 KB Output is correct
5 Correct 1359 ms 391464 KB Output is correct
6 Correct 1421 ms 391704 KB Output is correct
7 Correct 1178 ms 347624 KB Output is correct
8 Correct 380 ms 144624 KB Output is correct
9 Correct 230 ms 114992 KB Output is correct
10 Correct 256 ms 128860 KB Output is correct
11 Correct 255 ms 127964 KB Output is correct
12 Correct 372 ms 144624 KB Output is correct
13 Correct 376 ms 144624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 28780 KB Output is correct
2 Correct 21 ms 28908 KB Output is correct
3 Correct 21 ms 29036 KB Output is correct
4 Correct 22 ms 29164 KB Output is correct
5 Correct 496 ms 152048 KB Output is correct
6 Correct 634 ms 196964 KB Output is correct
7 Correct 713 ms 247020 KB Output is correct
8 Correct 843 ms 300524 KB Output is correct
9 Correct 574 ms 258008 KB Output is correct
10 Correct 600 ms 290304 KB Output is correct
11 Correct 581 ms 258008 KB Output is correct
12 Correct 602 ms 290528 KB Output is correct
13 Correct 571 ms 258392 KB Output is correct
14 Correct 609 ms 290576 KB Output is correct
15 Correct 372 ms 144624 KB Output is correct
16 Correct 380 ms 144760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28524 KB Output is correct
2 Correct 19 ms 28524 KB Output is correct
3 Correct 19 ms 28652 KB Output is correct
4 Correct 21 ms 28652 KB Output is correct
5 Correct 19 ms 28652 KB Output is correct
6 Correct 19 ms 28524 KB Output is correct
7 Correct 19 ms 28524 KB Output is correct
8 Correct 837 ms 232068 KB Output is correct
9 Correct 839 ms 232980 KB Output is correct
10 Correct 850 ms 233940 KB Output is correct
11 Correct 963 ms 240880 KB Output is correct
12 Correct 1231 ms 347412 KB Output is correct
13 Correct 719 ms 247148 KB Output is correct
14 Correct 371 ms 144624 KB Output is correct
15 Correct 384 ms 144752 KB Output is correct
16 Correct 21 ms 29292 KB Output is correct
17 Correct 21 ms 29420 KB Output is correct
18 Correct 21 ms 29292 KB Output is correct
19 Correct 20 ms 28780 KB Output is correct
20 Correct 1359 ms 391464 KB Output is correct
21 Correct 1421 ms 391704 KB Output is correct
22 Correct 1178 ms 347624 KB Output is correct
23 Correct 380 ms 144624 KB Output is correct
24 Correct 230 ms 114992 KB Output is correct
25 Correct 256 ms 128860 KB Output is correct
26 Correct 255 ms 127964 KB Output is correct
27 Correct 372 ms 144624 KB Output is correct
28 Correct 376 ms 144624 KB Output is correct
29 Correct 21 ms 28780 KB Output is correct
30 Correct 21 ms 28908 KB Output is correct
31 Correct 21 ms 29036 KB Output is correct
32 Correct 22 ms 29164 KB Output is correct
33 Correct 496 ms 152048 KB Output is correct
34 Correct 634 ms 196964 KB Output is correct
35 Correct 713 ms 247020 KB Output is correct
36 Correct 843 ms 300524 KB Output is correct
37 Correct 574 ms 258008 KB Output is correct
38 Correct 600 ms 290304 KB Output is correct
39 Correct 581 ms 258008 KB Output is correct
40 Correct 602 ms 290528 KB Output is correct
41 Correct 571 ms 258392 KB Output is correct
42 Correct 609 ms 290576 KB Output is correct
43 Correct 372 ms 144624 KB Output is correct
44 Correct 380 ms 144760 KB Output is correct
45 Correct 526 ms 160528 KB Output is correct
46 Correct 543 ms 164824 KB Output is correct
47 Correct 593 ms 167764 KB Output is correct
48 Correct 947 ms 241388 KB Output is correct
49 Correct 309 ms 140764 KB Output is correct
50 Correct 286 ms 140764 KB Output is correct
51 Correct 297 ms 140912 KB Output is correct
52 Correct 291 ms 141656 KB Output is correct
53 Correct 310 ms 141660 KB Output is correct
54 Correct 300 ms 141124 KB Output is correct