답안 #169550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
169550 2019-12-20T22:49:55 Z ZwariowanyMarcin 가로등 (APIO19_street_lamps) C++14
100 / 100
4560 ms 372072 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 = 40000000;

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 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 454 ms 1508 KB Output is correct
2 Correct 531 ms 1856 KB Output is correct
3 Correct 868 ms 8016 KB Output is correct
4 Correct 2557 ms 226652 KB Output is correct
5 Correct 2944 ms 249644 KB Output is correct
6 Correct 2700 ms 240772 KB Output is correct
7 Correct 600 ms 30784 KB Output is correct
8 Correct 218 ms 13204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1272 KB Output is correct
2 Correct 6 ms 1272 KB Output is correct
3 Correct 4 ms 1016 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 4560 ms 372072 KB Output is correct
6 Correct 3461 ms 321276 KB Output is correct
7 Correct 2259 ms 249024 KB Output is correct
8 Correct 206 ms 13176 KB Output is correct
9 Correct 113 ms 1272 KB Output is correct
10 Correct 125 ms 1328 KB Output is correct
11 Correct 122 ms 1428 KB Output is correct
12 Correct 569 ms 30628 KB Output is correct
13 Correct 215 ms 13308 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 497 ms 34396 KB Output is correct
6 Correct 1489 ms 186712 KB Output is correct
7 Correct 2572 ms 240464 KB Output is correct
8 Correct 3684 ms 278072 KB Output is correct
9 Correct 645 ms 1212 KB Output is correct
10 Correct 662 ms 772 KB Output is correct
11 Correct 648 ms 1144 KB Output is correct
12 Correct 661 ms 788 KB Output is correct
13 Correct 648 ms 1404 KB Output is correct
14 Correct 665 ms 540 KB Output is correct
15 Correct 560 ms 30584 KB Output is correct
16 Correct 207 ms 13304 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 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 454 ms 1508 KB Output is correct
9 Correct 531 ms 1856 KB Output is correct
10 Correct 868 ms 8016 KB Output is correct
11 Correct 2557 ms 226652 KB Output is correct
12 Correct 2944 ms 249644 KB Output is correct
13 Correct 2700 ms 240772 KB Output is correct
14 Correct 600 ms 30784 KB Output is correct
15 Correct 218 ms 13204 KB Output is correct
16 Correct 7 ms 1272 KB Output is correct
17 Correct 6 ms 1272 KB Output is correct
18 Correct 4 ms 1016 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 4560 ms 372072 KB Output is correct
21 Correct 3461 ms 321276 KB Output is correct
22 Correct 2259 ms 249024 KB Output is correct
23 Correct 206 ms 13176 KB Output is correct
24 Correct 113 ms 1272 KB Output is correct
25 Correct 125 ms 1328 KB Output is correct
26 Correct 122 ms 1428 KB Output is correct
27 Correct 569 ms 30628 KB Output is correct
28 Correct 215 ms 13308 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 497 ms 34396 KB Output is correct
34 Correct 1489 ms 186712 KB Output is correct
35 Correct 2572 ms 240464 KB Output is correct
36 Correct 3684 ms 278072 KB Output is correct
37 Correct 645 ms 1212 KB Output is correct
38 Correct 662 ms 772 KB Output is correct
39 Correct 648 ms 1144 KB Output is correct
40 Correct 661 ms 788 KB Output is correct
41 Correct 648 ms 1404 KB Output is correct
42 Correct 665 ms 540 KB Output is correct
43 Correct 560 ms 30584 KB Output is correct
44 Correct 207 ms 13304 KB Output is correct
45 Correct 188 ms 888 KB Output is correct
46 Correct 290 ms 832 KB Output is correct
47 Correct 1148 ms 89520 KB Output is correct
48 Correct 2346 ms 226680 KB Output is correct
49 Correct 137 ms 1016 KB Output is correct
50 Correct 138 ms 1148 KB Output is correct
51 Correct 136 ms 1144 KB Output is correct
52 Correct 126 ms 984 KB Output is correct
53 Correct 124 ms 1032 KB Output is correct
54 Correct 124 ms 1076 KB Output is correct