Submission #170910

# Submission time Handle Problem Language Result Execution time Memory
170910 2019-12-26T16:58:06 Z songc Street Lamps (APIO19_street_lamps) C++14
100 / 100
1335 ms 26708 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, Q;
int A[303030];
bool chk[303030];
int ans[303030];

struct Sq{
	int x, y, t;
	bool operator<(const Sq &r)const{
		if (x == r.x) return y < r.y;
		return x < r.x;
	}
};
set<Sq> S;
vector<Sq> V, L;

int T[303030];

void update(int k, int v){
	while (k<=N){
		T[k] += v;
		k += k & -k;
	}
}

int read(int k){
	int ret=0;
	while (k){
		ret += T[k];
		k ^= k & -k; 
	}
	return ret;
}

void sweep(){
	for (Sq it : L){
		if (it.t < 0) ans[-it.t] += read(it.y);
		else if (it.x <= it.y){
			update(it.x, it.t);
			update(it.y+1, -it.t);
		}
		else{
			update(it.y, it.t);
			update(it.x, -it.t);
		}
	}
	for (Sq it : L){
		if (it.t < 0) continue;
		if (it.x <= it.y){
			update(it.x, -it.t);
			update(it.y+1, it.t);
		}
		else{
			update(it.y, -it.t);
			update(it.x, it.t);
		}
	}
}

void DNC(int s, int e){
	if (s >= e) return;
	int mid = (s+e)/2;
	DNC(s, mid);
	DNC(mid+1, e);
	for (int i=s; i<=mid; i++) if (V[i].t > 0){
		L.push_back((Sq){V[i].x, V[i].y, V[i].t});
		L.push_back((Sq){V[i].y+1, V[i].x, V[i].t});
	}
	for (int i=mid+1; i<=e; i++) if (V[i].t < 0) L.push_back((Sq){V[i].x, V[i].y, V[i].t});
	sort(L.begin(), L.end(), [&](Sq a, Sq b){
		if (a.x == b.x) return a.t > b.t;
		return a.x < b.x;
	});
	sweep();
	L.clear();
}

int main(){
	scanf("%d %d", &N, &Q);
	
	int s=0;
	for (int i=1; i<=N; i++){
		scanf("%1d", &A[i]);
		if (A[i] && !s) s = i;
		if (!A[i] && s) {
			S.insert((Sq){s, i-1, 0});
			s = 0;
		}
	}
	if (s) S.insert((Sq){s, N, 0});
	char str[10];
	for (int i=1; i<=Q; i++){
		scanf("%s", str+1);
		if (str[1] == 't'){
			int k;
			scanf("%d", &k);
			if (A[k]){
				auto it = --S.lower_bound((Sq){k, N+1, 0});
				Sq a = *it;
				S.erase(it);
				V.push_back((Sq){a.x, a.y, i-a.t});
				if (a.x != k) S.insert((Sq){a.x, k-1, i});
				if (a.y != k) S.insert((Sq){k+1, a.y, i});
				A[k] = 0;
			}
			else{
				auto it = S.lower_bound((Sq){k, N+1, 0});
				if (A[k-1] && A[k+1]){
					auto pr = it;
					Sq a = *it;
					Sq b = *(--pr);
					S.erase(it), S.erase(pr);
					V.push_back((Sq){a.x, a.y, i-a.t});
					V.push_back((Sq){b.x, b.y, i-b.t});
					S.insert((Sq){b.x, a.y, i});
				}
				else if (A[k-1]){
					Sq a = *(--it);
					S.erase(it);
					V.push_back((Sq){a.x, a.y, i-a.t});
					S.insert((Sq){a.x, a.y+1, i});
				}
				else if (A[k+1]){
					Sq a = *it;
					S.erase(it);
					V.push_back((Sq){a.x, a.y, i-a.t});
					S.insert((Sq){a.x-1, a.y, i});
				}
				else S.insert((Sq){k, k, i});
				A[k] = 1;
			}
		}
		else{
			int x, y;
			scanf("%d %d", &x, &y);
			y--;
			chk[i] = true;
			if (A[x]){
				auto it = --S.lower_bound((Sq){x, N+1, 0});
				if (it->y >= y) ans[i] += i - it->t;
			}
			V.push_back((Sq){x, y, -i});
		}
	}
	DNC(0, V.size()-1);
	for (int i=1; i<=Q; i++) if (chk[i]) printf("%d\n", ans[i]);
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:83: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:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%1d", &A[i]);
   ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", str+1);
   ~~~~~^~~~~~~~~~~~~
street_lamps.cpp:100:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &k);
    ~~~~~^~~~~~~~~~
street_lamps.cpp:139:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &x, &y);
    ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 368 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 631 ms 13308 KB Output is correct
2 Correct 667 ms 15476 KB Output is correct
3 Correct 718 ms 16104 KB Output is correct
4 Correct 988 ms 23908 KB Output is correct
5 Correct 1116 ms 24976 KB Output is correct
6 Correct 1052 ms 25716 KB Output is correct
7 Correct 442 ms 17880 KB Output is correct
8 Correct 428 ms 19268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 1195 ms 21896 KB Output is correct
6 Correct 1250 ms 23080 KB Output is correct
7 Correct 1098 ms 22972 KB Output is correct
8 Correct 423 ms 19416 KB Output is correct
9 Correct 195 ms 11080 KB Output is correct
10 Correct 219 ms 13528 KB Output is correct
11 Correct 223 ms 13592 KB Output is correct
12 Correct 416 ms 18008 KB Output is correct
13 Correct 454 ms 19428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 609 ms 23884 KB Output is correct
6 Correct 834 ms 26708 KB Output is correct
7 Correct 1045 ms 25784 KB Output is correct
8 Correct 1335 ms 24640 KB Output is correct
9 Correct 781 ms 17000 KB Output is correct
10 Correct 806 ms 16136 KB Output is correct
11 Correct 792 ms 16924 KB Output is correct
12 Correct 807 ms 16252 KB Output is correct
13 Correct 786 ms 16984 KB Output is correct
14 Correct 809 ms 16052 KB Output is correct
15 Correct 415 ms 18008 KB Output is correct
16 Correct 439 ms 19388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 368 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 631 ms 13308 KB Output is correct
9 Correct 667 ms 15476 KB Output is correct
10 Correct 718 ms 16104 KB Output is correct
11 Correct 988 ms 23908 KB Output is correct
12 Correct 1116 ms 24976 KB Output is correct
13 Correct 1052 ms 25716 KB Output is correct
14 Correct 442 ms 17880 KB Output is correct
15 Correct 428 ms 19268 KB Output is correct
16 Correct 4 ms 376 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 1195 ms 21896 KB Output is correct
21 Correct 1250 ms 23080 KB Output is correct
22 Correct 1098 ms 22972 KB Output is correct
23 Correct 423 ms 19416 KB Output is correct
24 Correct 195 ms 11080 KB Output is correct
25 Correct 219 ms 13528 KB Output is correct
26 Correct 223 ms 13592 KB Output is correct
27 Correct 416 ms 18008 KB Output is correct
28 Correct 454 ms 19428 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
31 Correct 4 ms 376 KB Output is correct
32 Correct 4 ms 376 KB Output is correct
33 Correct 609 ms 23884 KB Output is correct
34 Correct 834 ms 26708 KB Output is correct
35 Correct 1045 ms 25784 KB Output is correct
36 Correct 1335 ms 24640 KB Output is correct
37 Correct 781 ms 17000 KB Output is correct
38 Correct 806 ms 16136 KB Output is correct
39 Correct 792 ms 16924 KB Output is correct
40 Correct 807 ms 16252 KB Output is correct
41 Correct 786 ms 16984 KB Output is correct
42 Correct 809 ms 16052 KB Output is correct
43 Correct 415 ms 18008 KB Output is correct
44 Correct 439 ms 19388 KB Output is correct
45 Correct 346 ms 10236 KB Output is correct
46 Correct 409 ms 10564 KB Output is correct
47 Correct 547 ms 13892 KB Output is correct
48 Correct 956 ms 23616 KB Output is correct
49 Correct 252 ms 14300 KB Output is correct
50 Correct 251 ms 14372 KB Output is correct
51 Correct 255 ms 14552 KB Output is correct
52 Correct 254 ms 14852 KB Output is correct
53 Correct 257 ms 14936 KB Output is correct
54 Correct 256 ms 14808 KB Output is correct