Submission #307712

# Submission time Handle Problem Language Result Execution time Memory
307712 2020-09-29T07:40:36 Z noclever Street Lamps (APIO19_street_lamps) C++14
100 / 100
1476 ms 39612 KB
#include <bits/stdc++.h>
using namespace std;
 
#define MAX_QUERY_NUMBER 600005
#define MAXF 300005
#define QUERY_OPT  0
#define UPDATE_OPT 1
#define DUMMY 0
 
struct Query {
  // type == QUERY_OPT: query for ans indexed at extra, val is not relevent;
  // type == UPDATE_OPT: update with val for [y, extra).
  int x, y, val, type, extra;  
};
 
Query queries[MAX_QUERY_NUMBER];
Query sorted_queries[MAX_QUERY_NUMBER];
 
int ans[MAXF];
 
inline int lowbit( int num ) { return num&(-num); }
namespace BIT { 
  int c[MAXF] = {0};
  void add( int x, int v ) {
    for( ; x <= MAXF-1; x += lowbit(x) ) {
      c[x] += v;
    }
  }
  
  int query( int x ) {
    int sum = 0;
    for( ; x; x -= lowbit(x) ) {
      sum += c[x];
    }
    return sum;
  }

  void clear(int x) {
    for( ; x <= MAXF-1; x += lowbit(x) ) {
      c[x] = 0;
    }
  }
}
 
void solve(int l, int r) {
  if (r - l <= 1) return;
  int m = (l + r) / 2;
  solve(l, m);
  solve(m, r);
 
  int p = l, q = m, t = l;
  while (p < m && q < r) {
    if (queries[p].x <= queries[q].x) {
      if (queries[p].type == UPDATE_OPT) {
        BIT::add(queries[p].y, queries[p].val);
        BIT::add(queries[p].extra, - queries[p].val);
      }
      sorted_queries[t++] = queries[p];
      p++;
    } else {
      if (queries[q].type == QUERY_OPT) {
        ans[queries[q].extra] += BIT::query(queries[q].y);
      }
      sorted_queries[t++] = queries[q];
      q++;
    }
  }
 
  while (q < r) {
    if (queries[q].type == QUERY_OPT) {
      ans[queries[q].extra] += BIT::query(queries[q].y);
    }
    sorted_queries[t++] = queries[q];
    q++;    
  }
 
  while (p < m) {
    sorted_queries[t++] = queries[p++];
  }
  
  for (int i = l; i < r; i++) {
    if (queries[i].type == UPDATE_OPT) {
      BIT::clear(queries[i].y);
      BIT::clear(queries[i].extra);
    }
    queries[i] = sorted_queries[i];
  }
 
  return;
}
 
struct comp {
  inline bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    return a.second < b.second;
  }
};
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;  
  cin >> n >> q;
  set< pair<int, int>, comp> st;
  for (int i = 1; i <= n + 1; i++) {
    st.insert({i, i});
  }
  string s;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '1') {
      auto it = st.upper_bound({0, i});
      pair<int, int> tmp = {prev(it)->first, it->second};
      st.erase(prev(it)); st.erase(it);
      st.insert(tmp);
    }
  }
 
  int idx = 0, ans_number = 0;
 
  for (int cur = 1; cur <= q; cur++) {
    string t;
    cin >> t;
    if (t == "query") {
      int l, r;
      cin >> l >> r;
      
      if (st.lower_bound({0, l}) == st.lower_bound({0, r})) {
        ans[ans_number] += cur;
      }
      queries[idx++] = (Query){l, r, DUMMY, QUERY_OPT, ans_number++};
 
    } else {
      int x;
      cin >> x;
      int i_1, j_1, i_2, j_2;
      int val = cur;
      if (s[x - 1] == '1') {
        auto it = st.lower_bound({0, x});
        i_1 = it->first;
        j_1 = x + 1;
        i_2 = x;
        j_2 = it->second;
        pair<int, int> tmp_1 = {it->first, x}, tmp_2 = {x + 1, it->second};
        st.erase({it->first, it->second});
        st.insert(tmp_1); st.insert(tmp_2);
        s[x - 1] = '0';
      } else {
        auto it = st.upper_bound({0, x});
        i_1 = prev(it)->first, i_2 = prev(it)->second, j_1 = it->first, j_2 = it->second;
        pair<int, int> tmp = {i_1, j_2};
        val = - val; 
        st.erase({i_1, i_2}); st.erase({j_1, j_2});
        st.insert(tmp);
        s[x - 1] = '1';
      }      
      queries[idx++] = Query{i_1, j_1, val, UPDATE_OPT, j_2 + 1};
      queries[idx++] = Query{i_2 + 1, j_1, - val, UPDATE_OPT, j_2 + 1};     
    }
  }
 
  solve(0, idx);
  for (idx = 0; idx < ans_number; idx ++ ) {
    cout << ans[idx] << "\n";
  }
 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 782 ms 19656 KB Output is correct
2 Correct 786 ms 19704 KB Output is correct
3 Correct 784 ms 19832 KB Output is correct
4 Correct 1110 ms 35084 KB Output is correct
5 Correct 942 ms 34444 KB Output is correct
6 Correct 1129 ms 35980 KB Output is correct
7 Correct 588 ms 28424 KB Output is correct
8 Correct 359 ms 29836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1389 ms 39564 KB Output is correct
6 Correct 1215 ms 36748 KB Output is correct
7 Correct 980 ms 33548 KB Output is correct
8 Correct 361 ms 29760 KB Output is correct
9 Correct 119 ms 11768 KB Output is correct
10 Correct 131 ms 12728 KB Output is correct
11 Correct 133 ms 12920 KB Output is correct
12 Correct 715 ms 28552 KB Output is correct
13 Correct 360 ms 29836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 724 ms 29836 KB Output is correct
6 Correct 944 ms 32908 KB Output is correct
7 Correct 1158 ms 35596 KB Output is correct
8 Correct 1476 ms 39612 KB Output is correct
9 Correct 1008 ms 21328 KB Output is correct
10 Correct 1255 ms 23672 KB Output is correct
11 Correct 1014 ms 21368 KB Output is correct
12 Correct 1255 ms 23800 KB Output is correct
13 Correct 1008 ms 21368 KB Output is correct
14 Correct 1251 ms 23928 KB Output is correct
15 Correct 705 ms 28428 KB Output is correct
16 Correct 355 ms 29836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 782 ms 19656 KB Output is correct
9 Correct 786 ms 19704 KB Output is correct
10 Correct 784 ms 19832 KB Output is correct
11 Correct 1110 ms 35084 KB Output is correct
12 Correct 942 ms 34444 KB Output is correct
13 Correct 1129 ms 35980 KB Output is correct
14 Correct 588 ms 28424 KB Output is correct
15 Correct 359 ms 29836 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1389 ms 39564 KB Output is correct
21 Correct 1215 ms 36748 KB Output is correct
22 Correct 980 ms 33548 KB Output is correct
23 Correct 361 ms 29760 KB Output is correct
24 Correct 119 ms 11768 KB Output is correct
25 Correct 131 ms 12728 KB Output is correct
26 Correct 133 ms 12920 KB Output is correct
27 Correct 715 ms 28552 KB Output is correct
28 Correct 360 ms 29836 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 4 ms 512 KB Output is correct
33 Correct 724 ms 29836 KB Output is correct
34 Correct 944 ms 32908 KB Output is correct
35 Correct 1158 ms 35596 KB Output is correct
36 Correct 1476 ms 39612 KB Output is correct
37 Correct 1008 ms 21328 KB Output is correct
38 Correct 1255 ms 23672 KB Output is correct
39 Correct 1014 ms 21368 KB Output is correct
40 Correct 1255 ms 23800 KB Output is correct
41 Correct 1008 ms 21368 KB Output is correct
42 Correct 1251 ms 23928 KB Output is correct
43 Correct 705 ms 28428 KB Output is correct
44 Correct 355 ms 29836 KB Output is correct
45 Correct 503 ms 13176 KB Output is correct
46 Correct 515 ms 12920 KB Output is correct
47 Correct 603 ms 18040 KB Output is correct
48 Correct 1142 ms 34824 KB Output is correct
49 Correct 151 ms 13948 KB Output is correct
50 Correct 150 ms 13944 KB Output is correct
51 Correct 154 ms 14088 KB Output is correct
52 Correct 157 ms 13944 KB Output is correct
53 Correct 166 ms 13944 KB Output is correct
54 Correct 158 ms 13944 KB Output is correct