Submission #139551

# Submission time Handle Problem Language Result Execution time Memory
139551 2019-08-01T01:54:04 Z eriksuenderhauf Street Lamps (APIO19_street_lamps) C++11
100 / 100
4211 ms 195860 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 3e5 + 5;
char str[MAXN], tmp[20];
int a[MAXN], segcnt, root[MAXN], n, q;
int seg[MAXN*18*18], L[MAXN*18*18], R[MAXN*18*18];
struct query {
	int a, b, t;

	bool operator<(const query rhs) const {
		return a < rhs.a;
	}
};

set<query> act;

void upd(int l, int r, int& k, int x, int y, int v) {
	if (r < x || y < l || x > y) return;
	if (!k) k = ++segcnt;
	if (x <= l && r <= y) { seg[k] += v; return; }
	int m = (l+r) / 2;
	upd(l,m,L[k],x,y,v);
	upd(m+1,r,R[k],x,y,v);
}

int segqry(int l, int r, int k, int x) {
	if (l == r) return seg[k];
	int m = (l+r) / 2;
	return seg[k] + (x <= m ? segqry(l,m,L[k],x) : segqry(m+1,r,R[k],x));
}

void updF(int ind, int l, int r, int v) {
	while (ind < MAXN) {
		upd(1,MAXN,root[ind],l,r,v);
		ind += ind & -ind;
	}
}

int qryF(int ind, int idx) {
	int ret = 0;
	while (ind) {
		ret += segqry(1,MAXN,root[ind],idx);
		ind -= ind & -ind;
	}
	return ret;
}

int main() {
	clock_t tim = clock();
	scanf("%d %d", &n, &q);
	scanf("%s", str+1);
	for (int i = 1; i <= n; i++) a[i] = str[i]-'0';
	for (int i = 1, j = 1; i <= n+1; i = j + 1) {
		for (j = i; j <= n && a[j]; j++);
		act.insert({i,j,0});
	}
	for (int j = 1; j <= q; j++) {
		scanf("%s", tmp);
		if (tmp[0] == 't') {
			int idx;
			scanf("%d", &idx);
			if (a[idx]) {
				query cur = *prev(act.lower_bound({idx+1,0,0}));
				updF(cur.a, cur.a, cur.b, j-cur.t);
				updF(cur.b+1, cur.a, cur.b, -(j-cur.t));
				act.erase(cur);
				act.insert({cur.a,idx,j});
				act.insert({idx+1,cur.b,j});
			} else {
				query curL = *prev(act.lower_bound({idx+1,0,0}));
				query curR = *prev(act.lower_bound({idx+2,0,0}));
				updF(curL.a, curL.a, curL.b, j-curL.t);
				updF(curL.b+1, curL.a, curL.b, -(j-curL.t));
				updF(curR.a, curR.a, curR.b, j-curR.t);
				updF(curR.b+1, curR.a, curR.b, -(j-curR.t));
				act.erase(curL);
				act.erase(curR);
				act.insert({curL.a,curR.b,j});
			}
			a[idx] ^= 1;
		} else {
			int lo, hi;
			scanf("%d %d", &lo, &hi);
			int ans = qryF(lo, hi);
			query cur = *prev(act.lower_bound({lo+1,0,0}));
			if (hi <= cur.b)
				ans += j-cur.t;
			printf("%d\n",ans);
		}
	}
	//cerr << clock() - tim << "\n";;
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:57:10: warning: unused variable 'tim' [-Wunused-variable]
  clock_t tim = clock();
          ^~~
street_lamps.cpp:58: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:59: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:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", tmp);
   ~~~~~^~~~~~~~~~~
street_lamps.cpp:69:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &idx);
    ~~~~~^~~~~~~~~~~~
street_lamps.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &lo, &hi);
    ~~~~~^~~~~~~~~~~~~~~~~~~
# 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 3 ms 376 KB Output is correct
4 Correct 3 ms 508 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1234 ms 1744 KB Output is correct
2 Correct 1268 ms 5200 KB Output is correct
3 Correct 1493 ms 9664 KB Output is correct
4 Correct 2517 ms 121112 KB Output is correct
5 Correct 2440 ms 132076 KB Output is correct
6 Correct 2601 ms 128192 KB Output is correct
7 Correct 772 ms 25692 KB Output is correct
8 Correct 338 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1120 KB Output is correct
2 Correct 10 ms 1016 KB Output is correct
3 Correct 8 ms 936 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 4211 ms 195860 KB Output is correct
6 Correct 3581 ms 167624 KB Output is correct
7 Correct 2373 ms 128088 KB Output is correct
8 Correct 340 ms 4296 KB Output is correct
9 Correct 137 ms 1912 KB Output is correct
10 Correct 146 ms 4472 KB Output is correct
11 Correct 150 ms 4748 KB Output is correct
12 Correct 717 ms 27228 KB Output is correct
13 Correct 337 ms 9732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 6 ms 760 KB Output is correct
3 Correct 8 ms 888 KB Output is correct
4 Correct 10 ms 1016 KB Output is correct
5 Correct 753 ms 20584 KB Output is correct
6 Correct 1735 ms 97228 KB Output is correct
7 Correct 2445 ms 124068 KB Output is correct
8 Correct 3383 ms 142852 KB Output is correct
9 Correct 1662 ms 1568 KB Output is correct
10 Correct 2190 ms 3588 KB Output is correct
11 Correct 1673 ms 4608 KB Output is correct
12 Correct 2203 ms 3704 KB Output is correct
13 Correct 1706 ms 4612 KB Output is correct
14 Correct 2208 ms 3548 KB Output is correct
15 Correct 763 ms 25904 KB Output is correct
16 Correct 345 ms 8312 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 3 ms 376 KB Output is correct
4 Correct 3 ms 508 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 1234 ms 1744 KB Output is correct
9 Correct 1268 ms 5200 KB Output is correct
10 Correct 1493 ms 9664 KB Output is correct
11 Correct 2517 ms 121112 KB Output is correct
12 Correct 2440 ms 132076 KB Output is correct
13 Correct 2601 ms 128192 KB Output is correct
14 Correct 772 ms 25692 KB Output is correct
15 Correct 338 ms 8312 KB Output is correct
16 Correct 12 ms 1120 KB Output is correct
17 Correct 10 ms 1016 KB Output is correct
18 Correct 8 ms 936 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 4211 ms 195860 KB Output is correct
21 Correct 3581 ms 167624 KB Output is correct
22 Correct 2373 ms 128088 KB Output is correct
23 Correct 340 ms 4296 KB Output is correct
24 Correct 137 ms 1912 KB Output is correct
25 Correct 146 ms 4472 KB Output is correct
26 Correct 150 ms 4748 KB Output is correct
27 Correct 717 ms 27228 KB Output is correct
28 Correct 337 ms 9732 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 6 ms 760 KB Output is correct
31 Correct 8 ms 888 KB Output is correct
32 Correct 10 ms 1016 KB Output is correct
33 Correct 753 ms 20584 KB Output is correct
34 Correct 1735 ms 97228 KB Output is correct
35 Correct 2445 ms 124068 KB Output is correct
36 Correct 3383 ms 142852 KB Output is correct
37 Correct 1662 ms 1568 KB Output is correct
38 Correct 2190 ms 3588 KB Output is correct
39 Correct 1673 ms 4608 KB Output is correct
40 Correct 2203 ms 3704 KB Output is correct
41 Correct 1706 ms 4612 KB Output is correct
42 Correct 2208 ms 3548 KB Output is correct
43 Correct 763 ms 25904 KB Output is correct
44 Correct 345 ms 8312 KB Output is correct
45 Correct 831 ms 2828 KB Output is correct
46 Correct 912 ms 2952 KB Output is correct
47 Correct 1417 ms 53192 KB Output is correct
48 Correct 2219 ms 121516 KB Output is correct
49 Correct 165 ms 4600 KB Output is correct
50 Correct 165 ms 4560 KB Output is correct
51 Correct 173 ms 4744 KB Output is correct
52 Correct 193 ms 4940 KB Output is correct
53 Correct 190 ms 5104 KB Output is correct
54 Correct 191 ms 4984 KB Output is correct