답안 #254632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254632 2020-07-30T11:21:37 Z Mahdi_Jfri 가로등 (APIO19_street_lamps) C++14
100 / 100
3028 ms 63828 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define lb(a) ((a)&(-(a)))

const int maxn = 3e5 + 20;
const int maxb = 20;

string s , type[maxn];

int l[maxn] , r[maxn] , ind[maxn] , pos[maxn] , where[maxn];

int seg[maxb][maxn] , nq , fen[maxb][maxn];

bool cmp(int x , int y)
{
	return l[x] < l[y];
}

void addFen(int p , int val , int h)
{
	for(p += 5; p < maxn; p += lb(p))
		fen[h][p] += val;
}

int getFen(int p , int h)
{
	int res = 0;
	for(p += 5; p > 0; p -= lb(p))
		res += fen[h][p];

	return res;
}

void build(int s = 0 , int e = nq , int v = 1 , int h = 0)
{
	if(e - s < 2)
	{
		seg[h][s] = r[ind[s]];
		return;
	}

	int m = (s + e) / 2;
	build(s , m , 2 * v , h + 1);
	build(m , e , 2 * v + 1 , h + 1);

	merge(seg[h + 1] + s , seg[h + 1] + m , seg[h + 1] + m , seg[h + 1] + e , seg[h] + s);
}

void add(int l , int r , int lx , int rx , int val , int s = 0 , int e = nq , int v = 1 , int h = 0)
{
	if(l <= s && e <= r)
	{
		int L = lower_bound(seg[h] + s , seg[h] + e , lx) - seg[h];
		int R = lower_bound(seg[h] + s , seg[h] + e , rx) - seg[h];

		addFen(L , val , h);
		addFen(R , -val , h);

		return;
	}
	if(r <= s || e <= l)
		return;

	int m = (s + e) / 2;
	add(l , r , lx , rx , val , s , m , 2 * v , h + 1);
	add(l , r , lx , rx , val , m , e , 2 * v + 1 , h + 1);
}

int get(int p , int r , int s = 0 , int e = nq , int v = 1 , int h = 0)
{
	int x = lower_bound(seg[h] + s , seg[h] + e , r) - seg[h];
	int res = getFen(x , h);

	if(e - s < 2)
		return res;

	int m = (s + e) / 2;

	if(p < m)
		return res + get(p , r , s , m , 2 * v , h + 1);
	else
		return res + get(p , r , m , e , 2 * v + 1 , h + 1);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n , q;
	cin >> n >> q;

	cin >> s;

	for(int i = 0; i < q; i++)
	{
		cin >> type[i];
		if(type[i] == "toggle")
		{
			cin >> pos[i];
			pos[i]--;
		}
		else
		{
			cin >> l[i] >> r[i];
			l[i]--;
			r[i]--;
			ind[nq++] = i;
		}
	}

	sort(ind , ind + nq , cmp);
	for(int i = 0; i < nq; i++)
		where[ind[i]] = i;

	build();

	set<int> st;
	st.insert(-1);
	st.insert(n);

	addFen(0 , q , 0);

	for(int i = 0; i < n; i++)
		if(s[i] == '0')
		{
			st.insert(i);
			auto it = st.lower_bound(i);
			it--;
			int l0 = *it + 1, r0 = *st.upper_bound(i);

			l[q] = l0; int L = lower_bound(ind , ind + nq , q , cmp) - ind;
			l[q] = i; int R = upper_bound(ind , ind + nq , q , cmp) - ind;
			add(L , R , i + 1 , r0 + 1 , -q);
		}

	for(int i = 0; i < q; i++)
	{
		if(type[i] == "toggle")
		{
			auto it = st.lower_bound(pos[i]);
			it--;
			int l0 = *it + 1, r0 = *st.upper_bound(pos[i]);

			l[q] = l0; int L = lower_bound(ind , ind + nq , q , cmp) - ind;
			l[q] = pos[i]; int R = upper_bound(ind , ind + nq , q , cmp) - ind;
			if(s[pos[i]] == '0')
				add(L , R , pos[i] + 1 , r0 + 1 , q - i - 1) , s[pos[i]] = '1' , st.erase(pos[i]);
			else
				add(L , R , pos[i] + 1 , r0 + 1 , -(q - i - 1)) , s[pos[i]] = '0' , st.insert(pos[i]);
		}
		else
		{
			bool is = 1;
			auto it = st.lower_bound(l[i]);
			if(it != st.end() && *it < r[i])
				is = 0;
			cout << get(where[i] , r[i]) - (is? q - i - 1 : 0) << endl; 
		}
	}
}




# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10112 KB Output is correct
2 Correct 6 ms 10112 KB Output is correct
3 Correct 7 ms 10112 KB Output is correct
4 Correct 6 ms 9984 KB Output is correct
5 Correct 6 ms 9984 KB Output is correct
6 Correct 7 ms 9984 KB Output is correct
7 Correct 6 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1333 ms 35832 KB Output is correct
2 Correct 1585 ms 38748 KB Output is correct
3 Correct 1658 ms 37740 KB Output is correct
4 Correct 1760 ms 42576 KB Output is correct
5 Correct 1876 ms 49932 KB Output is correct
6 Correct 1354 ms 35848 KB Output is correct
7 Correct 2624 ms 63828 KB Output is correct
8 Correct 2236 ms 46440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9984 KB Output is correct
2 Correct 8 ms 10240 KB Output is correct
3 Correct 8 ms 10368 KB Output is correct
4 Correct 9 ms 9984 KB Output is correct
5 Correct 741 ms 33808 KB Output is correct
6 Correct 1474 ms 41064 KB Output is correct
7 Correct 2063 ms 49292 KB Output is correct
8 Correct 2321 ms 46348 KB Output is correct
9 Correct 1129 ms 41720 KB Output is correct
10 Correct 1229 ms 46840 KB Output is correct
11 Correct 1172 ms 43896 KB Output is correct
12 Correct 3028 ms 63648 KB Output is correct
13 Correct 2241 ms 46520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10240 KB Output is correct
2 Correct 12 ms 10112 KB Output is correct
3 Correct 9 ms 10112 KB Output is correct
4 Correct 7 ms 9984 KB Output is correct
5 Correct 2946 ms 57868 KB Output is correct
6 Correct 1979 ms 46176 KB Output is correct
7 Correct 1445 ms 35504 KB Output is correct
8 Correct 515 ms 23180 KB Output is correct
9 Correct 863 ms 25464 KB Output is correct
10 Correct 399 ms 14968 KB Output is correct
11 Correct 869 ms 25392 KB Output is correct
12 Correct 416 ms 14976 KB Output is correct
13 Correct 867 ms 25464 KB Output is correct
14 Correct 409 ms 14968 KB Output is correct
15 Correct 2950 ms 63780 KB Output is correct
16 Correct 2104 ms 46636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10112 KB Output is correct
2 Correct 6 ms 10112 KB Output is correct
3 Correct 7 ms 10112 KB Output is correct
4 Correct 6 ms 9984 KB Output is correct
5 Correct 6 ms 9984 KB Output is correct
6 Correct 7 ms 9984 KB Output is correct
7 Correct 6 ms 9856 KB Output is correct
8 Correct 1333 ms 35832 KB Output is correct
9 Correct 1585 ms 38748 KB Output is correct
10 Correct 1658 ms 37740 KB Output is correct
11 Correct 1760 ms 42576 KB Output is correct
12 Correct 1876 ms 49932 KB Output is correct
13 Correct 1354 ms 35848 KB Output is correct
14 Correct 2624 ms 63828 KB Output is correct
15 Correct 2236 ms 46440 KB Output is correct
16 Correct 6 ms 9984 KB Output is correct
17 Correct 8 ms 10240 KB Output is correct
18 Correct 8 ms 10368 KB Output is correct
19 Correct 9 ms 9984 KB Output is correct
20 Correct 741 ms 33808 KB Output is correct
21 Correct 1474 ms 41064 KB Output is correct
22 Correct 2063 ms 49292 KB Output is correct
23 Correct 2321 ms 46348 KB Output is correct
24 Correct 1129 ms 41720 KB Output is correct
25 Correct 1229 ms 46840 KB Output is correct
26 Correct 1172 ms 43896 KB Output is correct
27 Correct 3028 ms 63648 KB Output is correct
28 Correct 2241 ms 46520 KB Output is correct
29 Correct 13 ms 10240 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
31 Correct 9 ms 10112 KB Output is correct
32 Correct 7 ms 9984 KB Output is correct
33 Correct 2946 ms 57868 KB Output is correct
34 Correct 1979 ms 46176 KB Output is correct
35 Correct 1445 ms 35504 KB Output is correct
36 Correct 515 ms 23180 KB Output is correct
37 Correct 863 ms 25464 KB Output is correct
38 Correct 399 ms 14968 KB Output is correct
39 Correct 869 ms 25392 KB Output is correct
40 Correct 416 ms 14976 KB Output is correct
41 Correct 867 ms 25464 KB Output is correct
42 Correct 409 ms 14968 KB Output is correct
43 Correct 2950 ms 63780 KB Output is correct
44 Correct 2104 ms 46636 KB Output is correct
45 Correct 705 ms 25260 KB Output is correct
46 Correct 842 ms 28024 KB Output is correct
47 Correct 1006 ms 28528 KB Output is correct
48 Correct 1658 ms 41868 KB Output is correct
49 Correct 1349 ms 49528 KB Output is correct
50 Correct 1346 ms 49656 KB Output is correct
51 Correct 1338 ms 48760 KB Output is correct
52 Correct 1293 ms 52200 KB Output is correct
53 Correct 1240 ms 53496 KB Output is correct
54 Correct 1262 ms 51320 KB Output is correct