답안 #497299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497299 2021-12-23T01:27:40 Z maomao90 가로등 (APIO19_street_lamps) C++17
100 / 100
669 ms 26236 KB
// She will give birth to a son, and you are to give him the name Jesus,
// because he will save his people from their sins.
// Matthew 1:21
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifdef DEBUG
#define debug(args...) printf(args)
#else
#define debug(args...)
#endif

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 300005

struct upd {
	int x1, x2, y1, y2, v, isq;
	bool operator< (const upd& o) const {
		return x1 < o.x1;
	}
};

int n, q;
char s[MAXN];
set<ii> st;
upd upds[MAXN];
int ans[MAXN];
bool isq[MAXN];

struct rect {
	int x, s, e, v;
	bool operator< (const rect& o) const {
		return x < o.x;
	}
};

vector<rect> events;

int fw[MAXN];
void incre(int i, int v) {
	for (; i <= n + 2; i += i & -i) {
	   	fw[i] += v;
	}
}
inline void incre(int s, int e, int v) {
	incre(s, v); incre(e + 1, -v);
}
int qsm(int i) {
	int res = 0;
	for (; i > 0; i -= i & -i) {
		res += fw[i];
	}
	return res;
}

void dnc(int l, int r) {
	if (l >= r) return;
	int m = l + r >> 1;
	dnc(l, m); dnc(m + 1, r);
	debug("%d %d %d\n", l, r, m);
	REP (i, l, m + 1) {
		if (upds[i].isq) continue;
		debug("%d %d %d %d %d\n", upds[i].x1, upds[i].x2, upds[i].y1, upds[i].y2, upds[i].v);
		events.pb((rect) {upds[i].x1, upds[i].y1, upds[i].y2, upds[i].v});
		events.pb((rect) {upds[i].x2 + 1, upds[i].y1, upds[i].y2, -upds[i].v});
	}
	sort(ALL(events));
	sort(upds + m + 1, upds + r + 1);
	int ptr = 0;
	REP (i, m + 1, r + 1) {
		if (!upds[i].isq) continue;
		while (ptr < events.size() && events[ptr].x <= upds[i].x1) {
			debug(" %d to %d + %d\n", events[ptr].s, events[ptr].e, events[ptr].v);
			incre(events[ptr].s, events[ptr].e, events[ptr].v);
			ptr++;
		}
		debug("%d: + %d\n", upds[i].v, qsm(upds[i].y1));
		ans[upds[i].v] += qsm(upds[i].y1);
	}
	REP (i, 0, ptr) {
		incre(events[i].s, events[i].e, -events[i].v);
	}
	events.clear();
}

int main() {
	int _t = 1;
   	//scanf("%d", &_t);
	while (_t--) {
		scanf("%d%d", &n, &q);
		scanf(" %s", s + 1);
		s[0] = s[n + 1] = '0';
		int prv = 0;
		REP (i, 1, n + 2) {
			if (s[i] == '0' && s[i - 1] == '1') {
				debug("%d %d\n", prv + 1, i - 1);
				st.insert(MP(prv + 1, i - 1));
			}
			if (s[i] == '0') {
				prv = i;
			}
		}
		REP (i, 1, q + 1) {
			char t[10]; scanf(" %s", t);
			if (t[0] == 't') {
				int a; scanf("%d", &a);
				if (s[a] == '0') {
					s[a] = '1';
					auto it = st.insert(MP(a, a)).FI;
					if (it != st.begin() && prev(it) -> SE == a - 1) {
						auto prv = prev(it);
						ii nw = MP(prv -> FI, it -> SE);
						st.erase(prv);
						st.erase(it);
						it = st.insert(nw).FI;
					}
					auto nxt = next(it);
					if (nxt != st.end() && nxt -> FI == a + 1) {
						ii nw = MP(it -> FI, nxt -> SE);
						st.erase(nxt);
						st.erase(it);
						it = st.insert(nw).FI;
					}
					upds[i] = (upd) {it -> FI, a, a + 1, it -> SE + 1, -i, 0};
				} else {
					s[a] = '0';
					auto it = st.upper_bound(MP(a, INF));
					assert(it != st.begin());
					it = prev(it);
					assert(it -> FI <= a && it -> SE >= a);
					if (it -> FI != a) {
						st.insert(MP(it -> FI, a - 1));
					}
					if (it -> SE != a) {
						st.insert(MP(a + 1, it -> SE));
					}
					upds[i] = (upd) {it -> FI, a, a + 1, it -> SE + 1, i, 0};
					st.erase(it);
				}
			} else {
				isq[i] = 1;
				int a, b; scanf("%d%d", &a, &b);
				auto it = st.upper_bound(MP(a, INF));
				if (it != st.begin()) {
					it = prev(it);
				}
				if (it -> FI <= a && it -> SE >= b - 1) {
					debug("%d: +%d\n", i, i);
					ans[i] += i;
				}
				upds[i] = (upd) {a, -1, b, -1, i, 1};
			}
		}
		dnc(1, q);
		REP (i, 1, q + 1) {
			if (isq[i]) {
				debug("%d: ", i);
				printf("%d\n", ans[i]);
			}
		}
	}
	return 0;
}

/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6


5 10
01011
toggle 1
query 4 6
toggle 3
query 1 6
toggle 1
toggle 4
query 1 6
query 2 5
toggle 3
query 2 4


5 10
01011
toggle 1
11011
query 4 6
11011
toggle 3
11111
query 1 6
11111
toggle 1
01111
toggle 4
01101
query 1 6
01101
query 2 5
01101
toggle 3
01001
query 2 4
*/

Compilation message

street_lamps.cpp: In function 'void dnc(int, int)':
street_lamps.cpp:82:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |  int m = l + r >> 1;
      |          ~~^~~
street_lamps.cpp:96:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<rect>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |   while (ptr < events.size() && events[ptr].x <= upds[i].x1) {
      |          ~~~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |   scanf("%d%d", &n, &q);
      |   ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |   scanf(" %s", s + 1);
      |   ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:128:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |    char t[10]; scanf(" %s", t);
      |                ~~~~~^~~~~~~~~~
street_lamps.cpp:130:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |     int a; scanf("%d", &a);
      |            ~~~~~^~~~~~~~~~
street_lamps.cpp:166:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |     int a, b; scanf("%d%d", &a, &b);
      |               ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 373 ms 12976 KB Output is correct
2 Correct 396 ms 16440 KB Output is correct
3 Correct 475 ms 17024 KB Output is correct
4 Correct 611 ms 23040 KB Output is correct
5 Correct 560 ms 21856 KB Output is correct
6 Correct 484 ms 26236 KB Output is correct
7 Correct 235 ms 15556 KB Output is correct
8 Correct 246 ms 17004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 668 ms 22064 KB Output is correct
6 Correct 669 ms 17848 KB Output is correct
7 Correct 541 ms 15860 KB Output is correct
8 Correct 231 ms 16964 KB Output is correct
9 Correct 132 ms 10744 KB Output is correct
10 Correct 143 ms 11724 KB Output is correct
11 Correct 144 ms 11880 KB Output is correct
12 Correct 232 ms 15556 KB Output is correct
13 Correct 245 ms 16920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 324 KB Output is correct
4 Correct 1 ms 380 KB Output is correct
5 Correct 326 ms 20428 KB Output is correct
6 Correct 421 ms 22372 KB Output is correct
7 Correct 481 ms 26184 KB Output is correct
8 Correct 540 ms 25008 KB Output is correct
9 Correct 346 ms 19376 KB Output is correct
10 Correct 362 ms 18628 KB Output is correct
11 Correct 351 ms 19344 KB Output is correct
12 Correct 361 ms 18764 KB Output is correct
13 Correct 352 ms 19344 KB Output is correct
14 Correct 357 ms 18600 KB Output is correct
15 Correct 235 ms 15548 KB Output is correct
16 Correct 240 ms 17020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 373 ms 12976 KB Output is correct
9 Correct 396 ms 16440 KB Output is correct
10 Correct 475 ms 17024 KB Output is correct
11 Correct 611 ms 23040 KB Output is correct
12 Correct 560 ms 21856 KB Output is correct
13 Correct 484 ms 26236 KB Output is correct
14 Correct 235 ms 15556 KB Output is correct
15 Correct 246 ms 17004 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 668 ms 22064 KB Output is correct
21 Correct 669 ms 17848 KB Output is correct
22 Correct 541 ms 15860 KB Output is correct
23 Correct 231 ms 16964 KB Output is correct
24 Correct 132 ms 10744 KB Output is correct
25 Correct 143 ms 11724 KB Output is correct
26 Correct 144 ms 11880 KB Output is correct
27 Correct 232 ms 15556 KB Output is correct
28 Correct 245 ms 16920 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 324 KB Output is correct
32 Correct 1 ms 380 KB Output is correct
33 Correct 326 ms 20428 KB Output is correct
34 Correct 421 ms 22372 KB Output is correct
35 Correct 481 ms 26184 KB Output is correct
36 Correct 540 ms 25008 KB Output is correct
37 Correct 346 ms 19376 KB Output is correct
38 Correct 362 ms 18628 KB Output is correct
39 Correct 351 ms 19344 KB Output is correct
40 Correct 361 ms 18764 KB Output is correct
41 Correct 352 ms 19344 KB Output is correct
42 Correct 357 ms 18600 KB Output is correct
43 Correct 235 ms 15548 KB Output is correct
44 Correct 240 ms 17020 KB Output is correct
45 Correct 196 ms 10108 KB Output is correct
46 Correct 237 ms 10428 KB Output is correct
47 Correct 350 ms 12860 KB Output is correct
48 Correct 602 ms 23120 KB Output is correct
49 Correct 159 ms 12900 KB Output is correct
50 Correct 170 ms 12860 KB Output is correct
51 Correct 161 ms 12996 KB Output is correct
52 Correct 168 ms 13356 KB Output is correct
53 Correct 160 ms 13248 KB Output is correct
54 Correct 161 ms 13252 KB Output is correct