Advertisement
cincout

Untitled

Mar 3rd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef long double ld;
  6. typedef double db;
  7. typedef string str;
  8.  
  9. typedef pair<int, int> pi;
  10. typedef pair<ll, ll> pl;
  11. typedef pair<ld, ld> pd;
  12.  
  13. typedef vector<int> vi;
  14. typedef vector<ll> vl;
  15. typedef vector<ld> vd;
  16. typedef vector<str> vs;
  17. typedef vector<pi> vpi;
  18. typedef vector<pl> vpl;
  19. typedef vector<pd> vpd;
  20.  
  21. #define sz(x) (int)x.size()
  22. #define all(x) (x).begin(), (x).end()
  23. #define rall(x) (x).rbegin(), (x).rend()
  24. #define mp make_pair
  25. #define nxt_prm next_permutation
  26.  
  27. #define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
  28. #define F0R(i, a) FOR(i, 0, a)
  29. #define ROF(i, a, b) for (int i = (b); i >= (a); --i)
  30. #define R0F(i, a) ROF(i, 0, a)
  31. #define trav(a, x) for (auto& a : x)
  32.  
  33. const int INT_INF = 2e9;
  34. const ll LL_INF = 1e18;
  35. const ld PI = acos(ld(-1));
  36.  
  37. int dx[] = { 0, 0, 1, -1 };
  38. int dy[] = { 1, -1, 0, 0 };
  39.  
  40. template<class T> bool ckmin(T &a, const T &b) {
  41. return a > b ? a = b, 1 : 0;
  42. }
  43. template<class T> bool ckmax(T &a, const T &b) {
  44. return a < b ? a = b, 1 : 0;
  45. }
  46.  
  47. namespace io {
  48. void setIn(string s) { freopen(s.c_str(), "r", stdin); }
  49. void setOut(string s) { freopen(s.c_str(), "w", stdout); }
  50. void setIO(string s = "", bool inter = false) {
  51. #ifdef ac
  52. if (!inter) {
  53. setIn("a.txt");
  54. }
  55. #endif
  56. cout.setf(ios::fixed);
  57. cout.precision(20);
  58. ios_base::sync_with_stdio(0); cin.tie(0);
  59. if (sz(s)) { setIn(s + ".in"), setOut(s + ".out"); }
  60. }
  61. }
  62.  
  63. using namespace io;
  64.  
  65. pi getNorm(pi a) {
  66. if (a.first > a.second) swap(a.first, a.second);
  67. return a;
  68. }
  69.  
  70. ll safeMult(ll a, ll b) {
  71. if (a == 0 || b == 0)
  72. return 0;
  73. if (!((LL_INF / a) / b))
  74. return LL_INF;
  75. return a * b;
  76. }
  77.  
  78. mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
  79.  
  80. const int MAXN = 1e6 + 2;
  81. int ans[MAXN], Q, cnt[MAXN], beg[MAXN];
  82. vpi o[MAXN];
  83. vi query[MAXN];
  84.  
  85. int main() {
  86. setIO();
  87.  
  88. fill(beg, beg + MAXN, INT_INF);
  89. fill(ans, ans + MAXN, -1);
  90.  
  91. cin >> Q;
  92.  
  93. FOR(i, 0, Q - 1) {
  94. int t, x;
  95. cin >> t >> x;
  96. if (t == 1) {
  97. cnt[x]++;
  98. ckmin(beg[x], i);
  99. }
  100. else if (t == 2) {
  101. cnt[x]--;
  102. if (cnt[x] == 0) {
  103. o[x].push_back({ beg[x], i });
  104. beg[x] = INT_INF;
  105. }
  106. }
  107. else {
  108. query[x].push_back(i);
  109. }
  110. }
  111.  
  112. FOR(i, 0, MAXN - 1) {
  113. if (beg[i] != INT_INF) {
  114. o[i].push_back({ beg[i], Q + 1 });
  115. }
  116. }
  117.  
  118. FOR(i, 1, MAXN - 1) {
  119. if (sz(query[i]) == 0) continue;
  120. vpi scan;
  121. for (int j = i; j < MAXN; j += i) {
  122. trav(k, o[j]) {
  123. scan.push_back({ k.first, -1 });
  124. scan.push_back({ k.second + 1, 1 });
  125. }
  126. }
  127. sort(all(scan));
  128. vi pref(sz(query[i]));
  129. int ptr = 0;
  130. trav(j, scan) {
  131. while (ptr < sz(query[i]) && query[i][ptr] < j.first)
  132. ptr++;
  133. if (ptr < sz(query[i]))
  134. pref[ptr] += -1 * j.second;
  135. }
  136. FOR(j, 0, sz(query[i]) - 1) {
  137. if (j > 0) pref[j] += pref[j - 1];
  138. ans[query[i][j]] += pref[j];
  139. }
  140. }
  141.  
  142. FOR(i, 0, Q - 1) {
  143. if (ans[i] != -1) {
  144. cout << ans[i] << "\n";
  145. }
  146. }
  147.  
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement