Submission #120530

# Submission time Handle Problem Language Result Execution time Memory
120530 2019-06-24T18:09:27 Z gs14004 Street Lamps (APIO19_street_lamps) C++17
100 / 100
1561 ms 52344 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
using lint = long long;
using pi = pair<int, int>;

struct bit{
	int tree[MAXN];
	void add(int x, int v){
		while(x < MAXN){
			tree[x] += v;
			x += x & -x;
		}
	}
	int query(int x){
		int ret = 0;
		while(x){
			ret += tree[x];
			x -= x & -x;
		}
		return ret;
	}
}bit;

struct intv{
	int s, e, x;
	bool operator<(const intv &i)const{
		return s < i.s;
	}
};

struct event{
	int x, y, t, type;
	bool operator<(const event &e)const{
		return pi(x, y) < pi(e.x, e.y);
	}
};

int ret[MAXN];
vector<event> ev;
set<intv> s;

void add_rect(int s, int e, int x){
	ev.push_back({s, s, +x, 1});
	ev.push_back({s, e + 1, -x, 1});
	ev.push_back({e + 1, s, -x, 1});
	ev.push_back({e + 1, e + 1, +x, 1});
}

void solve(int s, int e){
	if(s == e) return;
	int m = (s+e)/2;
	solve(s, m);
	solve(m + 1, e);
	vector<event> pupd, quer;
	for(int i=s; i<=m; i++){
		if(ev[i].type == 1){
			pupd.push_back(ev[i]);
		}
	}
	for(int i=m+1; i<=e; i++){
		if(ev[i].type == 2){
			quer.push_back(ev[i]);
		}
	}
	sort(pupd.begin(), pupd.end());
	sort(quer.begin(), quer.end());
	int ptr = 0;
	for(auto &i : quer){
		while(ptr < pupd.size() && pupd[ptr].x <= i.x){
			bit.add(pupd[ptr].y, pupd[ptr].t);
			ptr++;
		}
		ret[i.t] += bit.query(i.y);
	}
	for(int i=0; i<ptr; i++){
		bit.add(pupd[i].y, -pupd[i].t);
	}
}

int n, q;
char str[MAXN];

int main(){
	scanf("%d %d",&n,&q);
	scanf("%s", str + 1);
	for(int i=1; i<=n; ){
		if(str[i] == '0'){
			i++;
			continue;
		}
		int e = i;
		while(e <= n && str[e] == '1') e++;
		s.insert({i, e - 1, 1});
		i = e;
	}
	char buf[10];
	for(int i=1; i<=q; i++){
		scanf("%s", buf);
		if(*buf == 't'){
			int t; scanf("%d",&t);
			if(str[t] == '0'){
				int st = t, ed = t;
				auto l = s.lower_bound({t + 1, -1, -1});
				if(l != s.end() && l->s == t + 1){
					add_rect(l->s, l->e, i + 1 - l->x);
					ed = l->e;
					l = s.erase(l);
				}
				if(l != s.begin() && prev(l)->e == t - 1){
					l--;
					add_rect(l->s, l->e, i + 1 - l->x);
					st = l->s;
					s.erase(l);
				}
				s.insert({st, ed, i + 1});
			}
			if(str[t] == '1'){
				auto l = --s.lower_bound({t + 1, -1, -1});
				add_rect(l->s, l->e, i + 1 - l->x);
				vector<intv> newV;
				if(l->s < t){
					newV.push_back({l->s, t - 1, i + 1});
				}
				if(t < l->e){
					newV.push_back({t + 1, l->e, i + 1});
				}
				s.erase(l);
				for(auto &i : newV) s.insert(i);
			}
			str[t] ^= 1;
			ret[i] = -1;
		}
		else{
			int a, b; scanf("%d %d",&a,&b);
			b--;
			ev.push_back({a, b, i, 2});
			auto l = s.lower_bound({a + 1, -1, -1});
			if(l != s.begin()){
				l--;
				if(l->s <= a && b <= l->e){
					ret[i] += i + 1 - l->x;
				}
			}
		}
	}
	solve(0, ev.size() - 1);
	for(int i=1; i<=q; i++){
		if(ret[i] >= 0) printf("%d\n", ret[i]);
	}
}

Compilation message

street_lamps.cpp: In function 'void solve(int, int)':
street_lamps.cpp:70:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ptr < pupd.size() && pupd[ptr].x <= i.x){
         ~~~~^~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&q);
  ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str + 1);
  ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", buf);
   ~~~~~^~~~~~~~~~~
street_lamps.cpp:101:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int t; scanf("%d",&t);
           ~~~~~^~~~~~~~~
street_lamps.cpp:135:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int a, b; scanf("%d %d",&a,&b);
              ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 997 ms 26124 KB Output is correct
2 Correct 1014 ms 26324 KB Output is correct
3 Correct 973 ms 26476 KB Output is correct
4 Correct 1178 ms 32552 KB Output is correct
5 Correct 1263 ms 34876 KB Output is correct
6 Correct 970 ms 35748 KB Output is correct
7 Correct 333 ms 13140 KB Output is correct
8 Correct 337 ms 13008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 1343 ms 47896 KB Output is correct
6 Correct 1561 ms 47300 KB Output is correct
7 Correct 1231 ms 32008 KB Output is correct
8 Correct 367 ms 13140 KB Output is correct
9 Correct 184 ms 9052 KB Output is correct
10 Correct 199 ms 11856 KB Output is correct
11 Correct 199 ms 11864 KB Output is correct
12 Correct 363 ms 12996 KB Output is correct
13 Correct 365 ms 12972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 556 KB Output is correct
4 Correct 4 ms 612 KB Output is correct
5 Correct 590 ms 19408 KB Output is correct
6 Correct 808 ms 29380 KB Output is correct
7 Correct 980 ms 35708 KB Output is correct
8 Correct 1227 ms 52344 KB Output is correct
9 Correct 839 ms 32488 KB Output is correct
10 Correct 1008 ms 45888 KB Output is correct
11 Correct 857 ms 32320 KB Output is correct
12 Correct 1070 ms 45976 KB Output is correct
13 Correct 848 ms 32448 KB Output is correct
14 Correct 1036 ms 45776 KB Output is correct
15 Correct 354 ms 12880 KB Output is correct
16 Correct 363 ms 13264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 997 ms 26124 KB Output is correct
9 Correct 1014 ms 26324 KB Output is correct
10 Correct 973 ms 26476 KB Output is correct
11 Correct 1178 ms 32552 KB Output is correct
12 Correct 1263 ms 34876 KB Output is correct
13 Correct 970 ms 35748 KB Output is correct
14 Correct 333 ms 13140 KB Output is correct
15 Correct 337 ms 13008 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 4 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 1343 ms 47896 KB Output is correct
21 Correct 1561 ms 47300 KB Output is correct
22 Correct 1231 ms 32008 KB Output is correct
23 Correct 367 ms 13140 KB Output is correct
24 Correct 184 ms 9052 KB Output is correct
25 Correct 199 ms 11856 KB Output is correct
26 Correct 199 ms 11864 KB Output is correct
27 Correct 363 ms 12996 KB Output is correct
28 Correct 365 ms 12972 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 4 ms 512 KB Output is correct
31 Correct 4 ms 556 KB Output is correct
32 Correct 4 ms 612 KB Output is correct
33 Correct 590 ms 19408 KB Output is correct
34 Correct 808 ms 29380 KB Output is correct
35 Correct 980 ms 35708 KB Output is correct
36 Correct 1227 ms 52344 KB Output is correct
37 Correct 839 ms 32488 KB Output is correct
38 Correct 1008 ms 45888 KB Output is correct
39 Correct 857 ms 32320 KB Output is correct
40 Correct 1070 ms 45976 KB Output is correct
41 Correct 848 ms 32448 KB Output is correct
42 Correct 1036 ms 45776 KB Output is correct
43 Correct 354 ms 12880 KB Output is correct
44 Correct 363 ms 13264 KB Output is correct
45 Correct 601 ms 15824 KB Output is correct
46 Correct 635 ms 16336 KB Output is correct
47 Correct 640 ms 18388 KB Output is correct
48 Correct 1162 ms 32456 KB Output is correct
49 Correct 229 ms 12752 KB Output is correct
50 Correct 234 ms 12880 KB Output is correct
51 Correct 231 ms 12752 KB Output is correct
52 Correct 223 ms 12752 KB Output is correct
53 Correct 217 ms 12624 KB Output is correct
54 Correct 217 ms 12776 KB Output is correct