Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<bits/stdc++.h>
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <unordered_set>
- #include <unordered_map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <iterator>
- #include <bitset>
- #include <assert.h>
- #include <new>
- #include <sstream>
- #include <time.h>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <limits>
- #define endl '\n'
- #define READ() freopen("input", "r", stdin)
- #define WRITE() freopen("output", "w", stdout)
- #define TIME() fprintf(stderr, "\nRuntime: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)
- #define CLOCK() clock_t tStart = clock()
- #define pb push_back
- #define vStr vector<string>
- #define vi vector<int>
- #define vll vector<ll>
- #define vLL vector<ll>
- #define Mii map<int, int>
- #define Msi map<string, int>
- #define Mci map<c, int>
- #define ll long long int
- #define LL long long int
- #define ui unsigned int
- #define ull unsigned long long int
- #define FASTio \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL);
- using namespace std;
- // BIT (Binary Indexed Tree) (Fenwick tree)------------------
- void update(vector<int> &BIT, int idx, int value)
- {
- // vector<int> BIT length should be main array's (length + 1)
- int len = BIT.size();
- while (idx <= len)
- {
- BIT[idx] += value;
- idx += (idx & -idx);
- }
- }
- int sum(vector<int> &BIT, int idx)
- {
- int res = 0;
- while (idx > 0)
- {
- res += BIT[idx];
- idx -= (idx & -idx);
- }
- return res;
- }
- //===========================================================
- int main()
- {
- //---------------------------code_start_from_here-------------------------
- FASTio;
- int n, q;
- cin >> n >> q;
- vector<int> mainar(n + 1, 0), bit(n + 2, 0);
- while (q--)
- {
- int a, b;
- cin >> a >> b;
- if (a == 1)
- {
- mainar[b] += 1;
- update(bit, b, 1);
- cout << sum(bit, n) << endl;
- }
- else if (a == 2)
- {
- update(bit, b, -mainar[b]);
- mainar[b] = 0;
- cout << sum(bit, n) << endl;
- }
- else
- {
- int counter = 0;
- for (int i = 1; i <= n; i++)
- {
- if (mainar[i])
- {
- if (counter <= b)
- {
- if (counter + mainar[i] > b)
- {
- update(bit, i, -(b - counter));
- mainar[i] -= (b - counter);
- counter += (b - counter);
- }
- else
- {
- counter += mainar[i];
- update(bit, i, -mainar[i]);
- mainar[i] = 0;
- }
- }
- else
- break;
- }
- }
- cout << sum(bit, n) << endl;
- }
- }
- //---------------------------code_finished--------------------------------
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement