fork download
  1. // #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <deque>
  10. #include <array>
  11. #include <list>
  12. #include <stack>
  13. #include <tuple>
  14. #include <set>
  15. #include <unordered_set>
  16. #include <map>
  17. #include <unordered_map>
  18. #include <string>
  19. #include <cstring>
  20. #include <random>
  21. #include <bitset>
  22. #include <iomanip>
  23. #include <iterator>
  24. #include <functional>
  25. #include <ctime>
  26. #include <chrono>
  27. #include <cctype>
  28. #include <fstream>
  29. #include <ranges>
  30. #include <numeric>
  31. #include <complex>
  32. #include <cassert>
  33.  
  34. using namespace std;
  35.  
  36. // #pragma GCC optimize("Ofast")
  37. // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  38.  
  39. #define int long long
  40. #define sz(x) ((int)(x).size())
  41. #define mp make_pair
  42. #define all(x) (x).begin(), (x).end()
  43. #define pb push_back
  44. #define pf push_front
  45. #define ff first
  46. #define ss second
  47. #define unique(x) (x).erase(unique(all(x)), (x).end())
  48. #define min3(a, b, c) min(a, min(b, c))
  49. #define max3(a, b, c) max(a, max(b, c))
  50. #define FOR(i, a, b) for (int i = (a); i <= (b); i++)
  51. #define ROF(i, a, b) for (int i = (a); i >= (b); i--)
  52.  
  53. using vi = vector<int>;
  54. using vd = vector<double>;
  55. using vpii = vector<pair<int, int>>;
  56. using vpdd = vector<pair<double, double>>;
  57. using pii = pair<int, int>;
  58. using pdd = pair<double, double>;
  59.  
  60. template <typename Container>
  61. void PRINT(const Container& container) {
  62. for (const auto& e : container) {
  63. cout << e << ' ';
  64. } cout << '\n';
  65. }
  66.  
  67. void print_double(double ans, int num) {
  68. cout << fixed << setprecision(num) << ans << '\n';
  69. }
  70.  
  71. const int inf = 1e18;
  72. const double eps = 1e-9;
  73. const double PI = 3.141592653589793;
  74.  
  75. string alh = "abcdefghijklmnopqrstuvwxyz";
  76. string ALH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  77.  
  78. class SegmentTree2D {
  79. private:
  80. struct Node {
  81. map<int, Node*> children;
  82. int value = 0;
  83. };
  84.  
  85. Node* root;
  86.  
  87. void update(Node*& node, int x, int y, int val, int xLeft, int xRight, int yLeft, int yRight) {
  88. if (!node) {
  89. node = new Node();
  90. }
  91.  
  92. if (xLeft == xRight && yLeft == yRight) {
  93. node->value += val;
  94. return;
  95. }
  96.  
  97. int midX = (xLeft + xRight) / 2;
  98. int midY = (yLeft + yRight) / 2;
  99.  
  100. if (x <= midX) {
  101. if (y <= midY) {
  102. update(node->children[0], x, y, val, xLeft, midX, yLeft, midY);
  103. }
  104. else {
  105. update(node->children[1], x, y, val, xLeft, midX, midY + 1, yRight);
  106. }
  107. }
  108. else {
  109. if (y <= midY) {
  110. update(node->children[2], x, y, val, midX + 1, xRight, yLeft, midY);
  111. }
  112. else {
  113. update(node->children[3], x, y, val, midX + 1, xRight, midY + 1, yRight);
  114. }
  115. }
  116.  
  117. node->value = 0;
  118. for (auto& [_, child] : node->children) {
  119. if (child) {
  120. node->value += child->value;
  121. }
  122. }
  123. }
  124.  
  125. int query(Node* node, int x1, int y1, int x2, int y2, int xLeft, int xRight, int yLeft, int yRight) {
  126. if (!node || x1 > xRight || x2 < xLeft || y1 > yRight || y2 < yLeft) {
  127. return 0;
  128. }
  129.  
  130. if (x1 <= xLeft && xRight <= x2 && y1 <= yLeft && yRight <= y2) {
  131. return node->value;
  132. }
  133.  
  134. int midX = (xLeft + xRight) / 2;
  135. int midY = (yLeft + yRight) / 2;
  136.  
  137. int sum = 0;
  138. sum += query(node->children[0], x1, y1, x2, y2, xLeft, midX, yLeft, midY);
  139. sum += query(node->children[1], x1, y1, x2, y2, xLeft, midX, midY + 1, yRight);
  140. sum += query(node->children[2], x1, y1, x2, y2, midX + 1, xRight, yLeft, midY);
  141. sum += query(node->children[3], x1, y1, x2, y2, midX + 1, xRight, midY + 1, yRight);
  142.  
  143. return sum;
  144. }
  145.  
  146. public:
  147. SegmentTree2D() { root = nullptr; }
  148.  
  149. void update(int x, int y, int value) {
  150. update(root, x, y, value, 0, inf, 0, inf);
  151. }
  152.  
  153. int query(int x1, int y1, int x2, int y2) {
  154. return query(root, x1, y1, x2, y2, 0, inf, 0, inf);
  155. }
  156. };
  157.  
  158. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  159.  
  160. class Treap {
  161. public:
  162. struct TreapNode {
  163. int value, priority;
  164. TreapNode* left, * right;
  165. int size;
  166.  
  167. TreapNode(int v) :
  168. value(v), priority(rng()),
  169. left(nullptr), right(nullptr),
  170. size(1) {
  171. }
  172. };
  173.  
  174. Treap() : root(nullptr) {}
  175.  
  176. void insert(int value) {
  177. insert(root, new TreapNode(value));
  178. }
  179.  
  180. void remove(int value) {
  181. remove(root, value);
  182. }
  183.  
  184. int Count_L(int x) {
  185. return Count_L(root, x);
  186. }
  187.  
  188. int Count_R(int x) {
  189. return Count_R(root, x);
  190. }
  191.  
  192. private:
  193. TreapNode* root;
  194.  
  195. int getSize(TreapNode* node) {
  196. return node ? node->size : 0;
  197. }
  198.  
  199. void updateSize(TreapNode* node) {
  200. if (node) {
  201. node->size = 1 + getSize(node->left) + getSize(node->right);
  202. }
  203. }
  204.  
  205. void split(TreapNode* node, int key, TreapNode*& left, TreapNode*& right) {
  206. if (!node) {
  207. left = right = nullptr;
  208. return;
  209. }
  210.  
  211. if (node->value <= key) {
  212. split(node->right, key, node->right, right);
  213. left = node;
  214. }
  215. else {
  216. split(node->left, key, left, node->left);
  217. right = node;
  218. }
  219.  
  220. updateSize(node);
  221. }
  222.  
  223. void merge(TreapNode*& node, TreapNode* left, TreapNode* right) {
  224. if (!left || !right) {
  225. node = left ? left : right;
  226. return;
  227. }
  228.  
  229. if (left->priority > right->priority) {
  230. merge(left->right, left->right, right);
  231. node = left;
  232. }
  233. else {
  234. merge(right->left, left, right->left);
  235. node = right;
  236. }
  237.  
  238. updateSize(node);
  239. }
  240.  
  241. void insert(TreapNode*& node, TreapNode* newNode) {
  242. if (!node) {
  243. node = newNode;
  244. return;
  245. }
  246.  
  247. if (newNode->priority > node->priority) {
  248. split(node, newNode->value, newNode->left, newNode->right);
  249. node = newNode;
  250. }
  251. else {
  252. insert(newNode->value <= node->value ? node->left : node->right, newNode);
  253. }
  254.  
  255. updateSize(node);
  256. }
  257.  
  258. void remove(TreapNode*& node, int value) {
  259. if (!node) {
  260. return;
  261. }
  262.  
  263. if (node->value == value) {
  264. TreapNode* temp = node;
  265. merge(node, node->left, node->right);
  266. delete temp;
  267. }
  268. else {
  269. remove(value < node->value ? node->left : node->right, value);
  270. }
  271.  
  272. updateSize(node);
  273. }
  274.  
  275. int Count_L(TreapNode* node, int x) {
  276. if (!node) {
  277. return 0;
  278. }
  279.  
  280. if (node->value <= x) {
  281. return 1 + getSize(node->left) + Count_L(node->right, x);
  282. }
  283. else {
  284. return Count_L(node->left, x);
  285. }
  286. }
  287.  
  288. int Count_R(TreapNode* node, int x) {
  289. if (!node) {
  290. return 0;
  291. }
  292.  
  293. if (node->value >= x) {
  294. return 1 + getSize(node->right) + Count_R(node->left, x);
  295. }
  296. else {
  297. return Count_R(node->right, x);
  298. }
  299. }
  300. };
  301.  
  302. class SegmentTree {
  303. public:
  304. SegmentTree(const vector<int> a) {
  305. n = 1;
  306. while (n < (int)a.size()) {
  307. n <<= 1;
  308. }
  309. tree.resize(2 * n);
  310. build(a, 1, 0, n - 1);
  311. }
  312.  
  313. void update(int pos, int W, int U) {
  314. update(1, 0, n - 1, pos, W, U);
  315. }
  316.  
  317. int query_L(int l, int r, int x) {
  318. return query_L(1, 0, n - 1, l, r, x);
  319. }
  320.  
  321. int query_R(int l, int r, int x) {
  322. return query_R(1, 0, n - 1, l, r, x);
  323. }
  324.  
  325. private:
  326. int n;
  327. vector<Treap> tree;
  328.  
  329. void build(const vector<int>& a, int v, int tl, int tr) {
  330. if (tl == tr) {
  331. if (tl < (int)a.size()) {
  332. tree[v].insert(a[tl]);
  333. }
  334. }
  335. else {
  336. int tm = (tl + tr) / 2;
  337.  
  338. build(a, 2 * v, tl, tm);
  339. build(a, 2 * v + 1, tm + 1, tr);
  340.  
  341. for (int i = tl; i <= tr; ++i) {
  342. if (i < (int)a.size()) {
  343. tree[v].insert(a[i]);
  344. }
  345. }
  346. }
  347. }
  348.  
  349. void update(int v, int tl, int tr, int pos, int W, int U) {
  350. if (tl == tr) {
  351. tree[v].remove(W);
  352. tree[v].insert(U);
  353. }
  354. else {
  355. int tm = (tl + tr) / 2;
  356. if (pos <= tm) {
  357. update(2 * v, tl, tm, pos, W, U);
  358. }
  359. else {
  360. update(2 * v + 1, tm + 1, tr, pos, W, U);
  361. }
  362. tree[v].remove(W);
  363. tree[v].insert(U);
  364. }
  365. }
  366.  
  367. int query_L(int v, int tl, int tr, int l, int r, int x) {
  368. if (l > r || tl > r || tr < l) {
  369. return 0;
  370. }
  371.  
  372. if (tl >= l && tr <= r) {
  373. return tree[v].Count_L(x);
  374. }
  375.  
  376. int tm = (tl + tr) / 2;
  377.  
  378. return query_L(2 * v, tl, tm, l, min(r, tm), x) +
  379. query_L(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);
  380. }
  381.  
  382. int query_R(int v, int tl, int tr, int l, int r, int x) {
  383. if (l > r || tl > r || tr < l) {
  384. return 0;
  385. }
  386.  
  387. if (tl >= l && tr <= r) {
  388. return tree[v].Count_R(x);
  389. }
  390.  
  391. int tm = (tl + tr) / 2;
  392.  
  393. return query_R(2 * v, tl, tm, l, min(r, tm), x) +
  394. query_R(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);
  395. }
  396. };
  397.  
  398. class Treap_to_Find {
  399. public:
  400. struct Node {
  401. int x, y;
  402.  
  403. int count;
  404. int size;
  405.  
  406. Node* left;
  407. Node* right;
  408.  
  409. Node(int x, int y) :
  410. x(x), y(y), count(1), size(1),
  411. left(nullptr), right(nullptr) {
  412. }
  413. };
  414.  
  415. Treap_to_Find() : root(nullptr) {}
  416.  
  417. void Add(int x) {
  418. root = insert(root, x, rand());
  419. }
  420.  
  421. void Del(int x) {
  422. root = delete_key(root, x);
  423. }
  424.  
  425. bool Find(int x) {
  426. return exists(root, x);
  427. }
  428.  
  429. int Find_ll(int x) {
  430. ans_ll = -1;
  431. find_ll(root, x);
  432. return ans_ll;
  433. }
  434.  
  435. int Find_lr(int x) {
  436. ans_lr = -1;
  437. find_lr(root, x);
  438. return ans_lr;
  439. }
  440.  
  441. int Find_rl(int x) {
  442. ans_rl = -1;
  443. find_rl(root, x);
  444. return ans_rl;
  445. }
  446.  
  447. int Find_rr(int x) {
  448. ans_rr = -1;
  449. find_rr(root, x);
  450. return ans_rr;
  451. }
  452.  
  453. private:
  454. Node* root;
  455. int ans_ll, ans_lr, ans_rl, ans_rr;
  456.  
  457. int get_size(Node* a) {
  458. return a ? a->size : 0;
  459. }
  460.  
  461. void update_size(Node* a) {
  462. if (a) {
  463. a->size = get_size(a->left) + get_size(a->right) + a->count;
  464. }
  465. }
  466.  
  467. pair<Node*, Node*> split(Node* a, int k) {
  468. if (!a) {
  469. return { nullptr, nullptr };
  470. }
  471. if (a->x < k) {
  472. pair<Node*, Node*> lr = split(a->right, k);
  473. a->right = lr.first;
  474. update_size(a);
  475. return { a, lr.second };
  476. }
  477. else {
  478. pair<Node*, Node*> lr = split(a->left, k);
  479. a->left = lr.second;
  480. update_size(a);
  481. return { lr.first, a };
  482. }
  483. }
  484.  
  485. Node* merge(Node* a, Node* b) {
  486. if (!a) return b;
  487. if (!b) return a;
  488. if (a->y > b->y) {
  489. a->right = merge(a->right, b);
  490. update_size(a);
  491. return a;
  492. }
  493. else {
  494. b->left = merge(a, b->left);
  495. update_size(b);
  496. return b;
  497. }
  498. }
  499.  
  500. Node* insert(Node* a, int x, int y) {
  501. pair<Node*, Node*> lr = split(a, x);
  502. Node* l = lr.first;
  503. Node* r = lr.second;
  504.  
  505. if (l && l->x == x) {
  506. l->count++;
  507. update_size(l);
  508. return merge(l, r);
  509. }
  510.  
  511. Node* new_node = new Node(x, y);
  512. return merge(merge(l, new_node), r);
  513. }
  514.  
  515. Node* delete_key(Node* a, int x) {
  516. if (!a) return nullptr;
  517.  
  518. if (a->x == x) {
  519. if (a->count > 1) {
  520. a->count--;
  521. update_size(a);
  522. return a;
  523. }
  524. else {
  525. return merge(a->left, a->right);
  526. }
  527. }
  528.  
  529. if (x < a->x) {
  530. a->left = delete_key(a->left, x);
  531. }
  532. else {
  533. a->right = delete_key(a->right, x);
  534. }
  535.  
  536. update_size(a);
  537. return a;
  538. }
  539.  
  540. bool exists(Node* a, int x) {
  541. if (!a) {
  542. return false;
  543. }
  544. if (a->x == x) {
  545. return true;
  546. }
  547. if (x < a->x) {
  548. return exists(a->left, x);
  549. }
  550. else {
  551. return exists(a->right, x);
  552. }
  553. }
  554.  
  555. void find_ll(Node* a, int x) {
  556. if (!a) return;
  557. if (a->x < x) {
  558. ans_ll = a->x;
  559. find_ll(a->right, x);
  560. }
  561. else {
  562. find_ll(a->left, x);
  563. }
  564. }
  565.  
  566. void find_lr(Node* a, int x) {
  567. if (!a) return;
  568. if (a->x <= x) {
  569. ans_lr = a->x;
  570. find_lr(a->right, x);
  571. }
  572. else {
  573. find_lr(a->left, x);
  574. }
  575. }
  576.  
  577. void find_rl(Node* a, int x) {
  578. if (!a) return;
  579. if (a->x >= x) {
  580. ans_rl = a->x;
  581. find_rl(a->left, x);
  582. }
  583. else {
  584. find_rl(a->right, x);
  585. }
  586. }
  587.  
  588. void find_rr(Node* a, int x) {
  589. if (!a) return;
  590. if (a->x > x) {
  591. ans_rr = a->x;
  592. find_rr(a->left, x);
  593. }
  594. else {
  595. find_rr(a->right, x);
  596. }
  597. }
  598. };
  599.  
  600. void Solve() {
  601. int n; cin >> n;
  602.  
  603. vector<int> a(n);
  604. for (int i = 0; i < n; i++) {
  605. cin >> a[i];
  606. }
  607.  
  608. vector<int> base(n);
  609. for (int i = 0; i < n; i++) {
  610. base[i] = 0;
  611. }
  612.  
  613. SegmentTree LAST(base);
  614.  
  615. map<int, int> last_id;
  616. for (int i = 0; i < n; i++) {
  617. last_id[a[i]] = -1;
  618. }
  619.  
  620. vector<int> last(n);
  621. for (int i = 0; i < n; i++) {
  622. LAST.update(i, 0, last_id[a[i]]);
  623.  
  624. last[i] = 1'000'000 - last_id[a[i]];
  625. last_id[a[i]] = i;
  626. }
  627.  
  628. SegmentTree FUTURE(base);
  629.  
  630. map<int, int> future_id;
  631. for (int i = 0; i < n; i++) {
  632. future_id[a[i]] = n;
  633. }
  634.  
  635. vector<int> future(n);
  636. for (int i = n - 1; i >= 0; i--) {
  637. FUTURE.update(i, 0, future_id[a[i]]);
  638.  
  639. future[i] = future_id[a[i]];
  640. future_id[a[i]] = i;
  641. }
  642.  
  643. vector<int> X;
  644. for (int i = 0; i < n; i++) {
  645. X.push_back(last[i]);
  646. }
  647.  
  648. vector<int> Y;
  649. for (int i = 0; i < n; i++) {
  650. Y.push_back(future[i]);
  651. }
  652.  
  653. vector<pair<int, int>> points;
  654. for (int i = 0; i < n; i++) {
  655. points.push_back({ X[i], Y[i] });
  656. }
  657.  
  658. SegmentTree2D ST;
  659. for (auto p : points) {
  660. ST.update(p.first, p.second, 1);
  661. }
  662.  
  663. map<int, Treap_to_Find> POSITIONS;
  664. for (int i = 0; i < n; i++) {
  665. POSITIONS[a[i]].Add(i);
  666. }
  667. for (int i = 0; i < n; i++) {
  668. if (POSITIONS[a[i]].Find(-1) == false) {
  669. POSITIONS[a[i]].Add(-1);
  670. }
  671. if (POSITIONS[a[i]].Find(n) == false) {
  672. POSITIONS[a[i]].Add(n);
  673. }
  674. }
  675.  
  676. int q; cin >> q;
  677. for (int i = 0; i < q; i++) {
  678. string Type; cin >> Type;
  679.  
  680. if (Type == "!") {
  681. int pos, val; cin >> pos >> val;
  682.  
  683. --pos;
  684.  
  685. if (a[pos] == val) {
  686. continue;
  687. }
  688.  
  689. if (future[pos] != n) {
  690. LAST.update(future[pos], pos, 1'000'000 - last[pos]);
  691. ST.update(last[future[pos]], future[future[pos]], -1);
  692. }
  693. if (1'000'000 - last[pos] != -1) {
  694. FUTURE.update(1'000'000 - last[pos], pos, future[pos]);
  695. ST.update(last[1'000'000 - last[pos]], future[1'000'000 - last[pos]], -1);
  696. }
  697.  
  698. if (future[pos] != n) {
  699. last[future[pos]] = last[pos];
  700. ST.update(last[future[pos]], future[future[pos]], 1);
  701. }
  702. if (1'000'000 - last[pos] != -1) {
  703. future[1'000'000 - last[pos]] = future[pos];
  704. ST.update(last[1'000'000 - last[pos]], future[1'000'000 - last[pos]], 1);
  705. }
  706.  
  707. ST.update(last[pos], future[pos], -1);
  708.  
  709. POSITIONS[a[pos]].Del(pos);
  710. POSITIONS[val].Add(pos);
  711.  
  712. if (POSITIONS[val].Find(-1) == false) {
  713. POSITIONS[val].Add(-1);
  714. }
  715. if (POSITIONS[val].Find(n) == false) {
  716. POSITIONS[val].Add(n);
  717. }
  718.  
  719. int Last_for_val = POSITIONS[val].Find_ll(pos);
  720. int Future_for_val = POSITIONS[val].Find_rr(pos);
  721.  
  722. LAST.update(pos, 1'000'000 - last[pos], Last_for_val);
  723. FUTURE.update(pos, future[pos], Future_for_val);
  724.  
  725. last[pos] = 1'000'000 - POSITIONS[val].Find_ll(pos);
  726. future[pos] = POSITIONS[val].Find_rr(pos);
  727.  
  728. a[pos] = val;
  729.  
  730. ST.update(last[pos], future[pos], 1);
  731.  
  732. if (Future_for_val != n) {
  733. LAST.update(Future_for_val, Last_for_val, pos);
  734. ST.update(last[Future_for_val], future[Future_for_val], -1);
  735. }
  736. if (Last_for_val != -1) {
  737. FUTURE.update(Last_for_val, Future_for_val, pos);
  738. ST.update(last[Last_for_val], future[Last_for_val], -1);
  739. }
  740.  
  741. if (Future_for_val != n) {
  742. last[Future_for_val] = 1'000'000 - pos;
  743. ST.update(last[Future_for_val], future[Future_for_val], 1);
  744. }
  745. if (Last_for_val != -1) {
  746. future[Last_for_val] = pos;
  747. ST.update(last[Last_for_val], future[Last_for_val], 1);
  748. }
  749. }
  750. else {
  751. int l, r; cin >> l >> r;
  752.  
  753. --l;
  754. --r;
  755.  
  756. int min_X = 1'000'000 - l;
  757. int min_Y = r;
  758.  
  759. cout << ST.query(min_X + 1, min_Y + 1, inf, inf) - LAST.query_L(r + 1, n - 1, l - 1) - FUTURE.query_R(0, l - 1, r + 1) << endl;
  760. }
  761. }
  762. }
  763.  
  764. signed main() {
  765. ios_base::sync_with_stdio(false);
  766. cin.tie(0);
  767. cout.tie(0);
  768.  
  769. /*
  770.   ________________
  771.   / Just solved it \
  772.   | in my mind |
  773.   \________________/
  774.   /
  775.   /
  776.      />  フ ___________
  777.      |  _  _| | |
  778.      /`ミ _x 彡 | WA 2 |
  779.      /      | |__________|
  780.     /  ヽ   ノ / /
  781.  / ̄|   | | | /_________ /
  782.  | ( ̄ヽ__ヽ_)_)
  783.  \二つ
  784.  
  785.   */
  786.  
  787. /*
  788.   freopen("input.txt", "r", stdin);
  789.   freopen("output.txt", "w", stdout);
  790.   */
  791.  
  792. // auto start = chrono::high_resolution_clock::now();
  793.  
  794. int multitest = false;
  795. if (multitest == true) {
  796. int ctt; cin >> ctt;
  797.  
  798. for (int i = 0; i < ctt; i++) {
  799. Solve();
  800. }
  801. }
  802. else {
  803. Solve();
  804. }
  805.  
  806. // auto end = chrono::high_resolution_clock::now();
  807.  
  808. /*
  809.   cout << "Time taken: "
  810.   << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  811.   << " milliseconds" << endl;
  812.   */
  813.  
  814. return 0;
  815. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:29:10: fatal error: ranges: No such file or directory
 #include <ranges>
          ^~~~~~~~
compilation terminated.
stdout
Standard output is empty