답안 #169549

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
169549 2019-12-20T22:47:35 Z ZwariowanyMarcin 가로등 (APIO19_street_lamps) C++14
100 / 100
4841 ms 373920 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int) x.size()
#define pb push_back
#define ll long long
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for(int i = 0; i < n; ++i)

using namespace std;	
		
const int nax = 3e5 + 11;
const int pod = (1 << 19);
const int MAX = 70000000;

int n, q;
char s[nax];

struct nod {
	nod *l, *r;
	int sum;
};		

nod w[MAX];
nod *wsk = w;
nod *root[nax];

nod *alo() {
	nod *r = wsk++;
	r->sum = 0;
	return r;
}

#define mak(x) x ? x : x = alo()

void add(int x, int y, int c, nod *u, int l = 0, int r = pod - 1) {
	if(x <= l && r <= y) {
		u->sum += c;
		return;
	}
	int m = (l + r) / 2;
	if(x <= m)
		add(x, y, c, mak(u->l), l, m);
	if(m < y)
		add(x, y, c, mak(u->r), m + 1, r);
}

int qq(int x, nod *u, int l = 0, int r = pod - 1) {
	if(!u) return 0;
	if(l == r) return u->sum;
	int m = (l + r) / 2;
	if(x <= m)
		return u->sum + qq(x, u->l, l, m);
	else
		return u->sum + qq(x, u->r, m + 1, r);
}

void addf(int p, int l, int r, int c) {
	for(int i = p; i <= n; i += i & -i)
		add(l, r, c, root[i]);
}

struct gao {
	int l, r, tim;
	gao (int l, int r, int tim) : l(l), r(r), tim(tim) {}
	bool operator < (gao a) const{
		return mp(l, r) < mp(a.l, a.r);
	}
};

set <gao> secik;

int st[nax];

auto daj(int x) {
	auto it = secik.upper_bound(gao(x, 111111111, 0));
	assert(it != secik.begin());
	it--;
	return *it;
}

void akt(gao v, int czas) {
	addf(v.l, v.l, v.r, czas - v.tim);
	addf(v.r + 1, v.l, v.r, -(czas - v.tim));
}

void upd(int x, int czas) {
	st[x] ^= 1;
	if(st[x]) {
		auto L = daj(x);
		auto R = daj(x + 1);
		akt(L, czas);
		akt(R, czas);
		secik.erase(L);
		secik.erase(R);
		secik.insert(gao(L.l, R.r, czas));
	}
	else {
		auto L = daj(x);
		akt(L, czas);
		secik.insert(gao(L.l, x, czas));
		secik.insert(gao(x + 1, L.r, czas));
		secik.erase(L);
	}
}
		
int query(int a, int b) {
	int res = 0;
	for(int i = a; 0 < i; i -= i & -i)
		res += qq(b, root[i]);
	return res;
}
		
int main() {	
	scanf("%d %d", &n, &q);
	scanf("%s", s + 1);
	for(int i = 1; i <= n; ++i)
		st[i] = s[i] - '0';
	for(int i = 1; i <= n + 1;) {
		int j = i;
		while(j <= n && st[j])
			j++;
		secik.insert(gao(i, j, 0));
		i = j + 1;
	}
	for(int i = 1; i <= n + 1; ++i)
		mak(root[i]);
	for(int i = 1; i <= q; ++i) {
		scanf("%s", s);
		if(s[0] == 't') {
			int a;
			scanf("%d", &a);
			upd(a, i);
		}
		else {
			int a, b;
			scanf("%d %d", &a, &b);
			int res = query(a, b);
			auto L = daj(a);
			if(b <= L.r)
				res += i - L.tim;
			printf("%d\n", res);
		}
	}
	
	
	
	
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:116: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:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", s + 1);
  ~~~~~^~~~~~~~~~~~~
street_lamps.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", s);
   ~~~~~^~~~~~~~~
street_lamps.cpp:133:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &a);
    ~~~~~^~~~~~~~~~
street_lamps.cpp:138:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &a, &b);
    ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 380 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 448 ms 4600 KB Output is correct
2 Correct 535 ms 4880 KB Output is correct
3 Correct 845 ms 11116 KB Output is correct
4 Correct 2327 ms 229280 KB Output is correct
5 Correct 2549 ms 251312 KB Output is correct
6 Correct 2738 ms 242680 KB Output is correct
7 Correct 570 ms 32376 KB Output is correct
8 Correct 209 ms 14840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1272 KB Output is correct
2 Correct 5 ms 1156 KB Output is correct
3 Correct 4 ms 1016 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 4841 ms 373920 KB Output is correct
6 Correct 3495 ms 325908 KB Output is correct
7 Correct 2250 ms 254564 KB Output is correct
8 Correct 204 ms 19232 KB Output is correct
9 Correct 114 ms 4088 KB Output is correct
10 Correct 125 ms 4456 KB Output is correct
11 Correct 123 ms 4616 KB Output is correct
12 Correct 662 ms 36636 KB Output is correct
13 Correct 206 ms 19268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 888 KB Output is correct
3 Correct 5 ms 1016 KB Output is correct
4 Correct 6 ms 1144 KB Output is correct
5 Correct 510 ms 40448 KB Output is correct
6 Correct 1438 ms 192028 KB Output is correct
7 Correct 2570 ms 245388 KB Output is correct
8 Correct 3765 ms 282364 KB Output is correct
9 Correct 647 ms 4472 KB Output is correct
10 Correct 662 ms 3704 KB Output is correct
11 Correct 653 ms 4676 KB Output is correct
12 Correct 660 ms 3448 KB Output is correct
13 Correct 646 ms 4516 KB Output is correct
14 Correct 665 ms 3604 KB Output is correct
15 Correct 676 ms 36600 KB Output is correct
16 Correct 206 ms 19100 KB Output is correct
# 결과 실행 시간 메모리 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 380 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 448 ms 4600 KB Output is correct
9 Correct 535 ms 4880 KB Output is correct
10 Correct 845 ms 11116 KB Output is correct
11 Correct 2327 ms 229280 KB Output is correct
12 Correct 2549 ms 251312 KB Output is correct
13 Correct 2738 ms 242680 KB Output is correct
14 Correct 570 ms 32376 KB Output is correct
15 Correct 209 ms 14840 KB Output is correct
16 Correct 6 ms 1272 KB Output is correct
17 Correct 5 ms 1156 KB Output is correct
18 Correct 4 ms 1016 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 4841 ms 373920 KB Output is correct
21 Correct 3495 ms 325908 KB Output is correct
22 Correct 2250 ms 254564 KB Output is correct
23 Correct 204 ms 19232 KB Output is correct
24 Correct 114 ms 4088 KB Output is correct
25 Correct 125 ms 4456 KB Output is correct
26 Correct 123 ms 4616 KB Output is correct
27 Correct 662 ms 36636 KB Output is correct
28 Correct 206 ms 19268 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 4 ms 888 KB Output is correct
31 Correct 5 ms 1016 KB Output is correct
32 Correct 6 ms 1144 KB Output is correct
33 Correct 510 ms 40448 KB Output is correct
34 Correct 1438 ms 192028 KB Output is correct
35 Correct 2570 ms 245388 KB Output is correct
36 Correct 3765 ms 282364 KB Output is correct
37 Correct 647 ms 4472 KB Output is correct
38 Correct 662 ms 3704 KB Output is correct
39 Correct 653 ms 4676 KB Output is correct
40 Correct 660 ms 3448 KB Output is correct
41 Correct 646 ms 4516 KB Output is correct
42 Correct 665 ms 3604 KB Output is correct
43 Correct 676 ms 36600 KB Output is correct
44 Correct 206 ms 19100 KB Output is correct
45 Correct 190 ms 2928 KB Output is correct
46 Correct 290 ms 2968 KB Output is correct
47 Correct 1134 ms 92548 KB Output is correct
48 Correct 2100 ms 231744 KB Output is correct
49 Correct 138 ms 4600 KB Output is correct
50 Correct 140 ms 4476 KB Output is correct
51 Correct 136 ms 4728 KB Output is correct
52 Correct 127 ms 5012 KB Output is correct
53 Correct 125 ms 4956 KB Output is correct
54 Correct 125 ms 4984 KB Output is correct