CodeForces 145 E. Lucky Queries(線段樹)

Tony5t4rk發表於2019-03-07

E. Lucky Queries

Description:

Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya brought home string s with the length of n. The string only consists of lucky digits. The digits are numbered from the left to the right starting with 1. Now Petya should execute m queries of the following form:

  • switch l r — “switch” digits (i.e. replace them with their opposites) at all positions with indexes from l to r, inclusive: each digit 4 is replaced with 7 and each digit 7 is replaced with 4 (1 ≤ l ≤ r ≤ n);
  • count — find and print on the screen the length of the longest non-decreasing subsequence of string s.

Subsequence of a string s is a string that can be obtained from s by removing zero or more of its elements. A string is called non-decreasing if each successive digit is not less than the previous one.
Help Petya process the requests.

Input:

The first line contains two integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 3·105) — the length of the string s and the number of queries correspondingly. The second line contains n lucky digits without spaces — Petya’s initial string. Next m lines contain queries in the form described in the statement.

Output

For each query count print an answer on a single line.

Sample Input:

2 3
47
count
switch 1 2
count

Sample Output:

2
1

Sample Input:

3 5
747
count
switch 1 1
count
switch 1 3
count

Sample Output:

2
3
2

題目連結

有一串只有 4477 的數列,需要支援兩種操作

  1. switch l r 將區間 [l,r][l,r] 內的 44 變為 7777 變為 44
  2. count 求整個數列的最長不下降子序列長度

看題目範圍很容易想到線段樹做法,其中對線段樹每個節點四個屬性:覆蓋區間內 44 的數量、 77 的數量、最長不下降子序列長度、最長不上升子序列長度

其中進行區間更新時把 474、7 的數量互換,把最長不下降子序列長度、最長不上升子序列長度互換

而進行更新上傳時時計算最長不下降子序列有三種情況

  1. 左子樹最長不下降子序列長度+右子樹 77 的數量
  2. 左子樹 44 的數量+右子樹最長不下降子序列長度
  3. 左子樹 44 的數量+右子樹 77 的數量

其中對三種情況取最大值即可,計算最長不上升子序列同理

AC程式碼:

#include <bits/stdc++.h>
using namespace std;

class seg_tree {
public:
  typedef int type_t;
  struct node {
    type_t cnt4, cnt7, rise, fall, lazy;
    node(type_t _cnt4 = 0, type_t _cnt7 = 0,
         type_t _rise = 1, type_t _fall = 1,
         type_t _lazy = 0): cnt4(_cnt4), cnt7(_cnt7), 
                            rise(_rise), fall(_fall),
                            lazy(_lazy) {}
  };

  int n;
  vector<node> tree;

  node Unite(const node &k1, const node &k2) {
    node ans;
    ans.cnt4 = k1.cnt4 + k2.cnt4;
    ans.cnt7 = k1.cnt7 + k2.cnt7;
    ans.rise = max(k1.rise + k2.cnt7, max(k1.cnt4 + k2.cnt7, k1.cnt4 + k2.rise));
    ans.fall = max(k1.fall + k2.cnt4, max(k1.cnt7 + k2.cnt4, k1.cnt7 + k2.fall));
    return ans;
  }

  void Pull(int o) {
    tree[o] = Unite(tree[o << 1], tree[o << 1 | 1]);
  }

  void Push(int o, int l, int r) {
    if (tree[o].lazy != 0) {
      swap(tree[o << 1].cnt4, tree[o << 1].cnt7);
      swap(tree[o << 1].rise, tree[o << 1].fall);
      swap(tree[o << 1 | 1].cnt4, tree[o << 1 | 1].cnt7);
      swap(tree[o << 1 | 1].rise, tree[o << 1 | 1].fall);
      tree[o << 1].lazy = !tree[o << 1].lazy;
      tree[o << 1 | 1].lazy = !tree[o << 1 | 1].lazy;
      tree[o].lazy = 0;
    }
  }

  void Build(int o, int l, int r, const vector<type_t> &v) {
    if (l == r) {
      tree[o].cnt4 = (v[l - 1] == 4);
      tree[o].cnt7 = (v[l - 1] == 7);
      return;
    }
    int m = (l + r) >> 1;
    Build(o << 1, l, m, v);
    Build(o << 1 | 1, m + 1, r, v);
    Pull(o);
  }

  seg_tree(const vector<type_t> &v) {
    n = v.size();
    tree.resize(n << 2);
    Build(1, 1, n, v);
  }

  void Modify(int o, int l, int r, int ll, int rr) {
    if (ll <= l && rr >= r) {
      swap(tree[o].cnt4, tree[o].cnt7);
      swap(tree[o].rise, tree[o].fall);
      tree[o].lazy = !tree[o].lazy;
      return;
    }
    Push(o, l, r);
    int m = (l + r) >> 1;
    if (ll <= m) Modify(o << 1, l, m, ll, rr);
    if (rr > m) Modify(o << 1 | 1, m + 1, r, ll, rr);
    Pull(o);
  }
  void Modify(int ll, int rr) {
    Modify(1, 1, n, ll, rr);
  }
};

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m; cin >> n >> m;
  vector<int> arr(n);
  for (int i = 0; i < n; ++i) {
    char x; cin >> x;
    arr[i] = x - '0';
  }
  seg_tree sgt(arr);
  for (int i = 0, l, r; i < m; ++i) {
    string s; cin >> s;
    if (s == "count") cout << sgt.tree[1].rise << endl;
    else {
      cin >> l >> r;
      sgt.Modify(l, r);
    }
  }
  return 0;
}

相關文章