답안 #520231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520231 2022-01-29T00:44:13 Z czhang2718 가로등 (APIO19_street_lamps) C++17
100 / 100
2952 ms 214448 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
const int N=300001;

struct Fenwick{
  int n;
  vector<ll> sum;

  Fenwick(){ 
    this->n=1;
    sum.resize(1, 0);
  }

  Fenwick(int n){
    this->n=n;
    sum.resize(n+1, 0);
  }

  void add(int i, int v){
    i++;
    while(i<=n){
      sum[i]+=v;
      i+=i&-i;
    }
  }

  void add(int l, int r, int v){
    // cout << l << " to " << r << " add " << v << '\n';
    if(r<l) return;
    add(l, v); 
    add(r+1, -v);
  }

  ll get(int i){
    i++;
    ll ret=0;
    while(i){
      ret+=sum[i];
      i-=i&-i;
    }
    return ret;
  }
};

int n, q;
vi range[4*N];
Fenwick seg[4*N];
vector<int> li[N];
int r[N], l[N];
string init;

void build(int x, int lx, int rx){
  if(rx-lx==1){
    for(int i:li[lx]){
      range[x].push_back(i);
    }
    sort(all(range[x]), [&](int i, int j){ return make_pair(r[i], i)<make_pair(r[j], j); });
    Fenwick bit(sz(range[x]));
    seg[x]=bit;
    return;
  }
  int m=(lx+rx)/2;
  build(2*x+1, lx, m);
  build(2*x+2, m, rx);

  int n=sz(range[2*x+1]); m=sz(range[2*x+2]);
  int i=0, j=0;
  while(i+j<n+m){
    if(i==n){
      range[x].push_back(range[2*x+2][j]);
      j++;
    }
    else if(j==m){
      range[x].push_back(range[2*x+1][i]);
      i++;
    }
    else{
      if(make_pair(r[range[2*x+1][i]], range[2*x+1][i])<make_pair(r[range[2*x+2][j]], range[2*x+2][j])){
        range[x].push_back(range[2*x+1][i]);
        i++;
      }
      else{
        range[x].push_back(range[2*x+2][j]);
        j++;
      }
    }
  }
  Fenwick bit(n+m);
  seg[x]=bit;
}

void upd(int L, int M, int R, int v, int x, int lx, int rx){
  if(lx>M || rx<=L) return;
  if(lx>=L && rx<=M+1){
    int n=sz(range[x]);
    if(!n) return;
    int a=n-1;
    for(int i=n-1; i; i/=2){
      while(a-i>=0 && r[range[x][a-i]]>=M) a-=i;
    }
    int b=0;
    for(int i=n-1; i; i/=2){
      while(b+i<n && r[range[x][b+i]]<=R) b+=i;
    }
    if(r[range[x][a]]>=M && r[range[x][a]]<=R) seg[x].add(a, b, v);
    return;
  }
  int m=(lx+rx)/2;
  upd(L, M, R, v, 2*x+1, lx, m);
  upd(L, M, R, v, 2*x+2, m, rx);
}

void upd(int L, int M, int R, int v){
  upd(L, M, R, v, 0, 0, n);
}

ll query(int i, int x, int lx, int rx){
  int j=0;
  for(int add=sz(range[x])-1; add; add/=2){
    while(j+add<sz(range[x]) && make_pair(r[range[x][j+add]], range[x][j+add])<=make_pair(r[i], i)) j+=add;
  }
  assert(range[x][j]==i);
  ll ret=seg[x].get(j);
  if(rx-lx==1) return ret;
  int m=(lx+rx)/2;
  if(l[i]<m) ret+=query(i, 2*x+1, lx, m);
  else ret+=query(i, 2*x+2, m, rx);
  return ret;
}

ll query(int i){
  return query(i, 0, 0, n);
}

int main(){
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> q >> init;
  vector<pii> qu;
  for(int i=1; i<=q; i++){
    string op; cin >> op;
    if(op=="toggle"){
      int j; cin >> j;
      --j;
      qu.push_back({0, j});
    }
    else{
      cin >> l[i] >> r[i];
      --l[i]; r[i]-=2;
      qu.push_back({1, i});
      li[l[i]].push_back(i);
    }
  }
  build(0, 0, n);
  set<pii> s={{-1e9,-1e9},{1e9,1e9}};

  auto on=[&](int i, int X){ // turn ith lamp on, update s, segtree
    auto it=s.lower_bound({i, 0});
    auto prv=prev(it);
    int l=((*prv).s==i-1?(*prv).f:i);
    int r=((*it).f==i+1?(*it).s:i);
    if(s.find({l, i-1})!=s.end()) s.erase({l, i-1});
    if(s.find({i+1, r})!=s.end()) s.erase({i+1, r});
    s.insert({l, r});
    // cout << "on " << l << " " << i << " " << r << " " << X << '\n';
    upd(l, i, r, X);
  };

  auto off=[&](int i, int X){
    auto it=--s.upper_bound({i, 1e9});
    int l=(*it).f;
    int r=(*it).s;
    upd(l, i, r, X);
    s.erase({l, r});
    if(l!=i) s.insert({l, i-1});
    if(r!=i) s.insert({i+1, r});
  };

  Fenwick state(n);
  for(int i=0; i<n; i++){
    if(init[i]=='1') state.add(i, 1), on(i, 0);
  }
  for(int t=1; t<=q; t++) {
    auto p=qu[t-1];
    int op=p.f;
    int i=p.s;
    if(op){
      assert(i==t);
      cout << query(i)+(state.get(r[i])-state.get(l[i]-1)==r[i]-l[i]+1?t:0) << "\n";
    }
    else{
      if(state.get(i)-state.get(i-1)) off(i, t), state.add(i, -1);
      else on(i, -t), state.add(i, 1);
    }
  }
}


// set of all-1 range
// 2d segtree, sort l, sort r
// need ind[x] in each segtree layer

// toggle->1: [a, b, c] for l[a,b], r[b,c] -t
// toggle->0: [a, b, c] for l[a,b], r[b,c] +t
// query: sum of segtree vals at each layer

// inner- range add, get point OR fenwick ?
// outer: ind[i][x], vector for lowerbound
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 110660 KB Output is correct
2 Correct 91 ms 110652 KB Output is correct
3 Correct 84 ms 110684 KB Output is correct
4 Correct 85 ms 110672 KB Output is correct
5 Correct 87 ms 110656 KB Output is correct
6 Correct 87 ms 110656 KB Output is correct
7 Correct 92 ms 110660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 544 ms 135152 KB Output is correct
2 Correct 652 ms 138084 KB Output is correct
3 Correct 1032 ms 146852 KB Output is correct
4 Correct 1602 ms 168688 KB Output is correct
5 Correct 1934 ms 175852 KB Output is correct
6 Correct 1228 ms 161756 KB Output is correct
7 Correct 2546 ms 213204 KB Output is correct
8 Correct 2729 ms 214448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 110672 KB Output is correct
2 Correct 87 ms 110752 KB Output is correct
3 Correct 87 ms 110852 KB Output is correct
4 Correct 100 ms 110860 KB Output is correct
5 Correct 527 ms 123004 KB Output is correct
6 Correct 1238 ms 149932 KB Output is correct
7 Correct 1917 ms 174304 KB Output is correct
8 Correct 2926 ms 211120 KB Output is correct
9 Correct 579 ms 145008 KB Output is correct
10 Correct 649 ms 148500 KB Output is correct
11 Correct 641 ms 149508 KB Output is correct
12 Correct 2607 ms 209908 KB Output is correct
13 Correct 2952 ms 211148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 110920 KB Output is correct
2 Correct 87 ms 110788 KB Output is correct
3 Correct 90 ms 110760 KB Output is correct
4 Correct 85 ms 110720 KB Output is correct
5 Correct 2637 ms 210264 KB Output is correct
6 Correct 1913 ms 186704 KB Output is correct
7 Correct 1296 ms 161440 KB Output is correct
8 Correct 588 ms 124792 KB Output is correct
9 Correct 478 ms 128760 KB Output is correct
10 Correct 287 ms 117612 KB Output is correct
11 Correct 478 ms 128764 KB Output is correct
12 Correct 287 ms 117696 KB Output is correct
13 Correct 488 ms 128808 KB Output is correct
14 Correct 282 ms 117700 KB Output is correct
15 Correct 2613 ms 209780 KB Output is correct
16 Correct 2864 ms 211112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 110660 KB Output is correct
2 Correct 91 ms 110652 KB Output is correct
3 Correct 84 ms 110684 KB Output is correct
4 Correct 85 ms 110672 KB Output is correct
5 Correct 87 ms 110656 KB Output is correct
6 Correct 87 ms 110656 KB Output is correct
7 Correct 92 ms 110660 KB Output is correct
8 Correct 544 ms 135152 KB Output is correct
9 Correct 652 ms 138084 KB Output is correct
10 Correct 1032 ms 146852 KB Output is correct
11 Correct 1602 ms 168688 KB Output is correct
12 Correct 1934 ms 175852 KB Output is correct
13 Correct 1228 ms 161756 KB Output is correct
14 Correct 2546 ms 213204 KB Output is correct
15 Correct 2729 ms 214448 KB Output is correct
16 Correct 86 ms 110672 KB Output is correct
17 Correct 87 ms 110752 KB Output is correct
18 Correct 87 ms 110852 KB Output is correct
19 Correct 100 ms 110860 KB Output is correct
20 Correct 527 ms 123004 KB Output is correct
21 Correct 1238 ms 149932 KB Output is correct
22 Correct 1917 ms 174304 KB Output is correct
23 Correct 2926 ms 211120 KB Output is correct
24 Correct 579 ms 145008 KB Output is correct
25 Correct 649 ms 148500 KB Output is correct
26 Correct 641 ms 149508 KB Output is correct
27 Correct 2607 ms 209908 KB Output is correct
28 Correct 2952 ms 211148 KB Output is correct
29 Correct 87 ms 110920 KB Output is correct
30 Correct 87 ms 110788 KB Output is correct
31 Correct 90 ms 110760 KB Output is correct
32 Correct 85 ms 110720 KB Output is correct
33 Correct 2637 ms 210264 KB Output is correct
34 Correct 1913 ms 186704 KB Output is correct
35 Correct 1296 ms 161440 KB Output is correct
36 Correct 588 ms 124792 KB Output is correct
37 Correct 478 ms 128760 KB Output is correct
38 Correct 287 ms 117612 KB Output is correct
39 Correct 478 ms 128764 KB Output is correct
40 Correct 287 ms 117696 KB Output is correct
41 Correct 488 ms 128808 KB Output is correct
42 Correct 282 ms 117700 KB Output is correct
43 Correct 2613 ms 209780 KB Output is correct
44 Correct 2864 ms 211112 KB Output is correct
45 Correct 273 ms 123528 KB Output is correct
46 Correct 412 ms 127824 KB Output is correct
47 Correct 1038 ms 145020 KB Output is correct
48 Correct 1640 ms 170968 KB Output is correct
49 Correct 712 ms 153668 KB Output is correct
50 Correct 726 ms 153132 KB Output is correct
51 Correct 716 ms 154892 KB Output is correct
52 Correct 709 ms 163356 KB Output is correct
53 Correct 703 ms 163372 KB Output is correct
54 Correct 714 ms 163316 KB Output is correct