답안 #706047

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706047 2023-03-05T19:09:20 Z 600Mihnea 가로등 (APIO19_street_lamps) C++17
100 / 100
3342 ms 256340 KB
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

typedef long long ll;

struct T
{
	int i;
	int t1;
	int t2;
};

struct Q
{
	int l;
	int r;
	int iq;
};

struct Segment
{
	int l;
	int r;
	int x;
};

bool operator < (Segment a, Segment b)
{
	return make_pair(a.l, a.r) < make_pair(b.l, b.r);
}

struct Update
{
	int x;
	int y;
	ll val;
};

signed main()
{
#ifdef ONPC	
	FILE* stream;
	freopen_s(&stream, "input.txt", "r", stdin);
#else
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif

	int n, q;
	string init_config;
	cin >> n >> q >> init_config;
	assert((int)init_config.size() == n);
	for (auto& ch : init_config)
	{
		assert(ch == '0' || ch == '1');
	}
	vector<T> v;
	vector<int> since(n, 0);
	string cur_config = init_config;
	vector<vector<int>> ops(q);
	vector<int> printsol(q, -1);
	for (int iq = 0; iq < q; iq++)
	{
		string s;
		cin >> s;
		assert(s == "query" || s == "toggle");
		if (s == "query")
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;
			r--;
			assert(0 <= l && l <= r && r < n);
			ops[iq] = { l, r };
		}
		else
		{
			assert(s == "toggle");
			int i;
			cin >> i;
			i--;
			assert(0 <= i && i < n);
			cur_config[i] = '0' ^ '1' ^ cur_config[i];
			if (cur_config[i] == '1')
			{
				v.push_back({ i, since[i], iq - 1 + 1 });
			}
			else
			{
				since[i] = iq + 1;
			}
			ops[iq] = { i };
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (cur_config[i] == '0')
		{
			v.push_back({ i, since[i], q });
		}
	}
	vector<Q> qs;

	for (int iq = 0; iq < q; iq++)
	{
		if ((int)ops[iq].size() == 2)
		{
			int l = ops[iq][0], r = ops[iq][1];
			qs.push_back({ l, r, iq });

		}
	}

	sort(v.begin(), v.end(), [&](T a, T b) {return a.i > b.i; });
	sort(qs.begin(), qs.end(), [&](Q a, Q b) {return a.l > b.l; });
	vector<vector<int>> interestingPoints(q + 7);
	vector<vector<ll>> aib(q + 7);
	{

		vector<int> what(q + 1, (int)1e9), coef(q + 1, 0);
		set<Segment> s;
		what[0] = -1;
		coef[0] = q + 1;
		s.insert({ 0, q, -1 });
		int pz = 0;
		vector<Update> updates;
		updates.push_back({ 0 + 1, what[0] + 1, coef[0] });
		for (auto& itqs : qs)
		{
			int l = itqs.l;
			int sol = 0;
			while (pz < (int)v.size() && l <= v[pz].i)
			{
				int st = v[pz].t1, dr = v[pz].t2;
				while (1)
				{
					auto it = s.lower_bound({ dr + 1, -1, 0 });
					if (it == s.begin())
					{
						break;
					}
					it--;
					assert(it->l <= dr);
					if (it->r < st)
					{
						break;
					}
					assert(it->l <= dr);
					assert(st <= it->r);
					vector<Segment> bg;
					if (dr + 1 <= it->r)
					{
						bg.push_back({ dr + 1, it->r, it->x });
					}
					if (it->l <= st - 1)
					{
						bg.push_back({ it->l, st - 1, it->x });
					}
					updates.push_back({ it->l + 1, what[it->l] + 1, -coef[it->l] });
					coef[it->l] = 0;
					s.erase(it);
					for (auto& gu : bg)
					{
						updates.push_back({ gu.l + 1, what[gu.l] + 1, -coef[gu.l] });
						what[gu.l] = gu.x;
						coef[gu.l] = gu.r - gu.l + 1;
						updates.push_back({ gu.l + 1, what[gu.l] + 1, +coef[gu.l] });
						s.insert(gu);
					}
				}
				updates.push_back({ st + 1, what[st] + 1, -coef[st] });
				what[st] = pz;
				coef[st] = dr - st + 1;
				updates.push_back({ st + 1, what[st] + 1, coef[st] });
				s.insert({ st, dr, pz });
				pz++;
			}
		}
		for (auto& u : updates)
		{
			for (int i = u.x; i < (int)interestingPoints.size(); i += i & (-i))
			{
				interestingPoints[i].push_back(u.y);
			}
		}
		for (int x = 1; x < (int)interestingPoints.size(); x++)
		{
			sort(interestingPoints[x].begin(), interestingPoints[x].end());
			interestingPoints[x].resize(unique(interestingPoints[x].begin(), interestingPoints[x].end()) - interestingPoints[x].begin());
			aib[x].resize((int)interestingPoints[x].size() + 1, 0);
		}
	}
	{
		auto upd = [&](int x, int y, ll val)
		{
			if (val == 0)
			{
				return;
			}
			for (int i = x; i < (int)interestingPoints.size(); i += i & (-i))
			{
				int go = lower_bound(interestingPoints[i].begin(), interestingPoints[i].end(), y) - interestingPoints[i].begin();
				assert(0 <= go && go < (int)interestingPoints[i].size());
				assert(interestingPoints[i][go] == y);
				go++;
				for (int j = go; j < (int)aib[i].size(); j += j & (-j))
				{
					aib[i][j] += val;
				}
			}
		};
		auto get = [&](int x, int y)
		{
			ll sol = 0;
			for (int i = x; i >= 1; i -= i & (-i))
			{
				int cnt = 0, low = 0, high = (int)interestingPoints[i].size() - 1;
				while (low <= high)
				{
					int mid = (low + high) / 2;
					if (interestingPoints[i][mid] <= y)
					{
						cnt = mid + 1;
						low = mid + 1;
					}
					else
					{
						high = mid - 1;
					}
				}
				for (int j = cnt; j >= 1; j -= j & (-j))
				{
					sol += aib[i][j];
				}
			}
			return sol;
		};
		vector<int> what(q + 1, (int)1e9), coef(q + 1, 0);
		set<Segment> s;
		what[0] = -1;
		coef[0] = q + 1;
		s.insert({ 0, q, -1 });
		int pz = 0;
		upd(0 + 1, what[0] + 1, coef[0]);
		for (auto& itqs : qs)
		{
			int l = itqs.l;
			int sol = 0;
			while (pz < (int)v.size() && l <= v[pz].i)
			{
				int st = v[pz].t1, dr = v[pz].t2;
				while (1)
				{
					auto it = s.lower_bound({ dr + 1, -1, 0 });
					if (it == s.begin())
					{
						break;
					}
					it--;
					assert(it->l <= dr);
					if (it->r < st)
					{
						break;
					}
					assert(it->l <= dr);
					assert(st <= it->r);
					vector<Segment> bg;
					if (dr + 1 <= it->r)
					{
						bg.push_back({ dr + 1, it->r, it->x });
					}
					if (it->l <= st - 1)
					{
						bg.push_back({ it->l, st - 1, it->x });
					}
					upd(it->l + 1, what[it->l] + 1, -coef[it->l]);
					coef[it->l] = 0;
					s.erase(it);
					for (auto& gu : bg)
					{
						upd(gu.l + 1, what[gu.l] + 1, -coef[gu.l]);
						what[gu.l] = gu.x;
						coef[gu.l] = gu.r - gu.l + 1;
						upd(gu.l + 1, what[gu.l] + 1, +coef[gu.l]);
						s.insert(gu);
					}
				}
				upd(st + 1, what[st] + 1, -coef[st]);
				what[st] = pz;
				coef[st] = dr - st + 1;
				upd(st + 1, what[st] + 1, coef[st]);
				s.insert({ st, dr, pz });
				pz++;
			}
			int r = itqs.r, iq = itqs.iq;;
			///it2.i <= r
			/// 0 0 0 0 0 1 1 1 1 1 1 
			int Start = pz, low = 0, high = pz - 1;
			while (low <= high)
			{
				int jo = (low + high) / 2;
				if (v[jo].i <= r)
				{
					Start = jo;
					high = jo - 1;
				}
				else
				{
					low = jo + 1;
				}
			}
			auto it = s.lower_bound({ iq + 1, -1, 0 });
			assert(it != s.begin());
			it--;
			if (it->x <= Start - 1)
			{
				sol += iq - it->l + 1;
			}

			if (it != s.begin())
			{
				it--;
				int ant = it->l;
				sol += get(ant + 1, Start);
			}
			printsol[iq] = sol;
		}

	}
	for (int iq = 0; iq < q; iq++)
	{
		if ((int)ops[iq].size() == 2)
		{
			cout << printsol[iq] << "\n";
		}
	}
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:155:8: warning: unused variable 'sol' [-Wunused-variable]
  155 |    int sol = 0;
      |        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1208 ms 104448 KB Output is correct
2 Correct 1138 ms 108532 KB Output is correct
3 Correct 1064 ms 110460 KB Output is correct
4 Correct 2203 ms 185688 KB Output is correct
5 Correct 1465 ms 142536 KB Output is correct
6 Correct 2530 ms 201688 KB Output is correct
7 Correct 1488 ms 188892 KB Output is correct
8 Correct 166 ms 56920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 4 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 3342 ms 256340 KB Output is correct
6 Correct 2482 ms 208596 KB Output is correct
7 Correct 1514 ms 143276 KB Output is correct
8 Correct 172 ms 56888 KB Output is correct
9 Correct 148 ms 41916 KB Output is correct
10 Correct 148 ms 46060 KB Output is correct
11 Correct 141 ms 45820 KB Output is correct
12 Correct 1601 ms 188896 KB Output is correct
13 Correct 205 ms 56992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 680 KB Output is correct
3 Correct 3 ms 724 KB Output is correct
4 Correct 4 ms 724 KB Output is correct
5 Correct 1462 ms 116692 KB Output is correct
6 Correct 1993 ms 168160 KB Output is correct
7 Correct 2452 ms 201564 KB Output is correct
8 Correct 3045 ms 244532 KB Output is correct
9 Correct 1572 ms 136980 KB Output is correct
10 Correct 2061 ms 163396 KB Output is correct
11 Correct 1795 ms 137136 KB Output is correct
12 Correct 2287 ms 163228 KB Output is correct
13 Correct 1742 ms 136828 KB Output is correct
14 Correct 2039 ms 163128 KB Output is correct
15 Correct 1628 ms 188804 KB Output is correct
16 Correct 175 ms 56924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1208 ms 104448 KB Output is correct
9 Correct 1138 ms 108532 KB Output is correct
10 Correct 1064 ms 110460 KB Output is correct
11 Correct 2203 ms 185688 KB Output is correct
12 Correct 1465 ms 142536 KB Output is correct
13 Correct 2530 ms 201688 KB Output is correct
14 Correct 1488 ms 188892 KB Output is correct
15 Correct 166 ms 56920 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 4 ms 596 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 3342 ms 256340 KB Output is correct
21 Correct 2482 ms 208596 KB Output is correct
22 Correct 1514 ms 143276 KB Output is correct
23 Correct 172 ms 56888 KB Output is correct
24 Correct 148 ms 41916 KB Output is correct
25 Correct 148 ms 46060 KB Output is correct
26 Correct 141 ms 45820 KB Output is correct
27 Correct 1601 ms 188896 KB Output is correct
28 Correct 205 ms 56992 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 3 ms 680 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 4 ms 724 KB Output is correct
33 Correct 1462 ms 116692 KB Output is correct
34 Correct 1993 ms 168160 KB Output is correct
35 Correct 2452 ms 201564 KB Output is correct
36 Correct 3045 ms 244532 KB Output is correct
37 Correct 1572 ms 136980 KB Output is correct
38 Correct 2061 ms 163396 KB Output is correct
39 Correct 1795 ms 137136 KB Output is correct
40 Correct 2287 ms 163228 KB Output is correct
41 Correct 1742 ms 136828 KB Output is correct
42 Correct 2039 ms 163128 KB Output is correct
43 Correct 1628 ms 188804 KB Output is correct
44 Correct 175 ms 56924 KB Output is correct
45 Correct 665 ms 69288 KB Output is correct
46 Correct 766 ms 70464 KB Output is correct
47 Correct 1010 ms 94392 KB Output is correct
48 Correct 2372 ms 185612 KB Output is correct
49 Correct 139 ms 51408 KB Output is correct
50 Correct 143 ms 51404 KB Output is correct
51 Correct 160 ms 51524 KB Output is correct
52 Correct 143 ms 51928 KB Output is correct
53 Correct 151 ms 52000 KB Output is correct
54 Correct 151 ms 51868 KB Output is correct