Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <list>
- #include <deque>
- #include <memory>
- #include <utility>
- #include <queue>
- #include <math.h>
- #include <ctime>
- #include <random>
- #include <functional>
- using namespace std;
- const int n = 100050;
- const int bs = 316;
- struct vertex {
- int left0;
- int left1;
- int qq;
- int sum;
- };
- vector<int> v(n, 0);
- vector<vertex> block(n / bs + 2);
- void updates(int x) {
- block[x].left0 = 1e9;
- block[x].left1 = 1e9;
- block[x].sum = 0;
- for (int i = x * bs; i < (x + 1) * bs; i++) {
- if (v[i]) {
- block[x].sum++;
- block[x].left1 = min(block[x].left1, i);
- }
- if (!v[i]) {
- block[x].left0 = min(block[x].left0, i);
- }
- }
- }
- void update(int x) {
- if (block[x].qq >= 0) {
- for (int i = x * bs; i < (x + 1) * bs; i++)
- v[i] = block[x].qq;
- if (block[x].qq)
- block[x].sum = bs;
- block[x].qq = -1;
- }
- }
- int ff0(int l) {
- int ans = 1e9;
- update(l / bs);
- for (int i = l; i < ((l / bs) + 1) * bs; i++) {
- if (!v[i]) {
- ans = i;
- break;
- }
- }
- if (ans != 1e9)
- return ans;
- for (int i = (l / bs) + 1; i < block.size(); i++) {
- if (block[i].left0 != 1e9) {
- ans = block[i].left0;
- return ans;
- }
- }
- }
- int ff1(int l) {
- int ans = 1e9;
- update(l / bs);
- for (int i = l; i < ((l / bs) + 1) * bs; i++) {
- if (v[i]) {
- ans = i;
- break;
- }
- }
- if (ans != 1e9)
- return ans;
- for (int i = (l / bs) + 1; i < block.size(); i++) {
- if (block[i].left1 != 1e9) {
- ans = block[i].left1;
- return ans;
- }
- }
- }
- void set_smth(int L, int R, int q) {
- update(L / bs);
- for (int i = L; i <= min(((L / bs) + 1) * bs - 1, R); i++) {
- v[i] = q;
- }
- updates(L / bs);
- update(R / bs);
- for (int i = max(L, (R / bs) * bs); i <= R; i++) {
- v[i] = q;
- }
- updates(R / bs);
- for (int i = (L / bs) + 1; i < R / bs; i++) {
- if (!q) { // 0
- block[i].qq = q;
- block[i].left1 = 1e9;
- block[i].left0 = i * bs;
- block[i].sum = 0;
- }
- else {
- block[i].qq = q;
- block[i].left1 = i * bs;
- block[i].left0 = 1e9;
- block[i].sum = bs;
- }
- }
- }
- int get_sum() {
- int ans = 0;
- for (auto f: block) {
- ans += f.sum;
- }
- return ans;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- for (int i = 0; i < block.size(); i++) {
- block[i].left0 = i * bs;
- block[i].left1 = 1e9;
- }
- int qqq;
- cin >> qqq;
- for (int zzz = 0; zzz < qqq;zzz++) {
- string s;
- int x;
- cin >> s >> x;
- if (s == "add") {
- int u = ff0(x);
- set_smth(x, u, 0);
- set_smth(u, u, 1);
- cout << get_sum() << '\n';
- }
- else {
- int u = ff1(x);
- set_smth(x, u, 1);
- set_smth(u, u, 0);
- cout << get_sum() << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement