Codeforces 558E A Simple Task segment Tree

Source: Internet
Author: User

Topic links

Test instructions is relatively simple.

Ideas:

Because there are only 26 letters, it is easier to maintain them with 26 tree segments.

#include <iostream> #include <string> #include <vector> #include <cstring> #include <cstdio > #include <map> #include <queue> #include <algorithm> #include <stack> #include <cstring > #include <cmath> #include <set> #include <vector>using namespace std;template <class t>  inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | C> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} typedef long Long Ll;typedef pair<ll, ll> pii;const int N = 1e5 +, #define Lson (id<<1) #define Rson (id< <1|1) #define L (x) tree[x].l#define R (x) tree[x].r#define Hav (x) tree[x].hav#define Siz (x) tree[x].Siz#define Lazy (x) tree[x].lazystruct tree {struct Node {int L, R, Siz;//siz indicates interval length int hav;//hav indicates this interval and int lazy;//lazy is 2 table The empty interval, lazy 1, indicates that the interval is changed to 1}tree[n << 2];void build (int l, int r, int id) {L (id) = l; R (ID) = R; Siz (id) = r-l + 1; Hav (id) = Lazy (id) = 0;if (L = = r) Return;int mid = (L + R) >> 1;build (L, Mid, Lson); Build (mid + 1, R, Rson);} void down (int id) {if (lazy (id) = = 1) {lazy (id) = 0; Hav (Lson) = Siz (Lson); Hav (Rson) = Siz (Rson); Lazy (Lson) = Lazy (Rson) = 1;} else if (lazy (id) = = 2) {lazy (id) = 0; HAV (Lson) = hav (Rson) = 0; Lazy (Lson) = Lazy (Rson) = 2;}} void up (int id) {hav (id) = hav (Lson) + hav (Rson);} void Updata (int l, int r, int val, int id) {if (L = = L (ID) && r (id) = = r) {if (val = = 1) Hav (id) = Siz (ID); else Ha V (id) = 0; Lazy (id) = Val;return;} Down (ID); int mid = (L (ID) + R (ID)) >> 1;if (R <= Mid) Updata (L, R, Val, Lson), else if (Mid < L) Updata (L, R, Val , Rson); else {Updata (L, Mid, Val, Lson); Updata (mid + 1, R, Val, Rson);} Up (ID);} int query (int l, int r, int id) {if (L = = L (ID) && r (id) = = r) {return Hav (ID);} Down (ID); int mid = (L (ID) + R (ID)) >> 1, ans = 0;if (R <= mid) ans = query (l, R, Lson); else if (Mid < L) ans = q Uery (L, R, Rson), else {ans = query (l, Mid, Lson) + query (mid + 1, R, Rson);} Up (ID); return ans;}; Tree alph[26];int N, Q;char s[n];int sum[26];int Main () {rd (n); Rd (q); for (int i = 0; i <; i++) Alph[i].build (1, N, 1 ); scanf ("%s", S + 1), for (int i = 1; I <= n; i++) {alph[s[i]-' A '].updata (i, I, 1, 1);} while (q--) {int L, R, In;rd (l); Rd (R); Rd (in); memset (sum, 0, sizeof sum); for (int i = 0; i <; i++) {Sum[i] + = Alph[i ].query (L, R, 1); Alph[i].updata (L, R, 2, 1);} int Tim = n, i;if (in) i = 0; else I = +, in = -1;for (; tim--; i + = in) {if (sum[i] = = 0) continue;alph[i].updata (l, L + sum[i]-1, 1, 1); L + = sum[i];} }for (int i = 1; I <= n; i++) {for (int j = 0; J <; J + +) if (Alph[j].query (i, I, 1)) {Putchar (j + ' a '); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 558E A Simple Task segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.