답안 #333398

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333398 2020-12-05T20:10:35 Z NaimSS 가로등 (APIO19_street_lamps) C++14
100 / 100
1453 ms 58308 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;


template<class T, int SZ> struct OffBIT2D { 
  bool mode = 0; // mode = 1 -> initialized
  vector<pii> todo; // locations of updates to process
  int cnt[SZ], st[SZ];
  vi val; vector<T> bit; // store all BITs in single vector
  void init() { assert(!mode); mode = 1;
    int lst[SZ]; 
    rep(i,0,SZ) lst[i] = cnt[i] = 0;
    sort(all(todo),[](const pii& a, const pii& b) { 
      return a.ss < b.ss; });
    trav(t,todo) for (int x = t.ff; x < SZ; x += x&-x) {
          if (lst[x] != t.ss) lst[x] = t.ss, cnt[x] ++;
    }

    int sum = 0; rep(i,0,SZ) lst[i] = 0, st[i] = (sum += cnt[i]);
    val.resize(sum); bit.resize(sum); reverse(all(todo));
    trav(t,todo) for (int x = t.ff; x < SZ; x += x&-x) 
      if (lst[x] != t.ss) lst[x] = t.ss, val[--st[x]] = t.ss;
  }
  int rank(int y, int l, int r) {
    return upper_bound(begin(val)+l,begin(val)+r,y)-begin(val)-l; }
  void UPD(int x, int y, T t) {
    for (y = rank(y,st[x],st[x]+cnt[x]); y <= cnt[x]; y += y&-y) 
      bit[st[x]+y-1] += t; }
  void upd(int x, int y, T t) { 
    if (!mode) todo.pb({x,y});
    else for (;x<SZ;x+=x&-x) UPD(x,y,t); }
  int QUERY(int x, int y) { T res = 0;
    for (y = rank(y,st[x],st[x]+cnt[x]); y; y -= y&-y) res += bit[st[x]+y-1];
    return res; }
  T query(int x, int y) { assert(mode);
    T res = 0; for (;x;x-=x&-x) res += QUERY(x,y);
    return res; }
  T query(int xl, int xr, int yl, int yr) { 
    return query(xr,yr)-query(xl-1,yr)
      -query(xr,yl-1)+query(xl-1,yl-1); }
};

typedef long long ll;

struct event{
  int l,r,mx;
  ll v;
};

OffBIT2D<ll,300100> BIT;

vector<event> ev;

int n,q;
void add_event(int l,int r,int mx,ll v,int A=0){
  BIT.upd(l,mx,v);
  if(r!=n)BIT.upd(r+1,mx,-v);
  if(!A)ev.pb({l,r,mx,v});
}

ll ask(int a,int b,int A=0){
  ll res=0;
  if(!A)ev.pb({a,b,-1,-1});
  else{
    res = BIT.query(1,a,b,n);
  }
  return res;
}

const int N = 300100;
char s[N];

int fim[N];

int last[N];
string Q[N];
int a[N],b[N];


int32_t main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  cin >> n >> q;
  for(int i=1;i<=n;i++){
    cin >> s[i];
  }
  s[0]='0';
  set<pii> S;
  s[n+1]='0';
  for(int i=n;i>=0;i--){
    last[i] = 0;
    if(s[i] =='1'){
      if(s[i+1]=='1')fim[i] = fim[i+1];
      else{
        fim[i] = i;
      }
      continue;
    }
    if(s[i+1]=='1'){
      S.insert(pii(i+1,fim[i+1]));
    }
  }
  
  for(int i=1;i<=q;i++){
    cin >> Q[i];
    if(Q[i][0]=='t'){
      cin >> a[i];
      int pos = a[i];
      if(s[pos] == '0'){
        s[pos] ='1';
        int l = pos,r = pos;
        if(s[pos-1]=='1'){
          auto it = S.lower_bound(pii(pos,-1));
          assert(it!=S.begin());
          it--;
          l = it->ff;
          add_event(it->ff,it->ss,it->ss,i-last[it->ff]);
          last[it->ff] = i;
          S.erase(it);
        }
        if(s[pos+1]=='1'){
          auto it = S.lower_bound(pii(pos,-1));
          assert(it!=S.end());
          r=it->ss;
          add_event(it->ff,it->ss,it->ss,i-last[it->ff]);
          last[it->ff] = i;
          S.erase(it);
        }
        S.insert(pii(l,r));
        last[l] = i;
        
       // db2(l,r);
      }else{

        s[pos]='0';
        auto it = S.lower_bound(pii(pos+1,-1));
        assert(it!=S.begin());
        it--;
        int t = last[it->ff];
        int l = it->ff,r = it->ss;
        
        if(pos!=r)
          last[pos+1] = t;
        
        add_event(it->ff,pos,it->ss,i-last[it->ff]);
        
        S.erase(it);
        last[l] = i;
        if(pos!=l)S.insert(pii(l,pos-1));
        if(pos!=r)S.insert(pii(pos+1,r));
      }

    }else{
      cin >> a[i] >> b[i];
      b[i]--;
      if(s[a[i]] == '0'){
        ask(a[i],b[i]);
        continue;
      }
      auto it = S.lower_bound(pii(a[i]+1,-1));
      it--;
      add_event(it->ff,it->ss,it->ss,i-last[it->ff]);
      last[it->ff] = i;
      ask(a[i],b[i]);
    }
  }
  BIT.init();

  for(auto it : ev){
    if(it.mx != -1){
      add_event(it.l,it.r,it.mx,it.v,1);
    }else{
      cout << ask(it.l,it.r,1) << '\n';
    }
  }
  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 13292 KB Output is correct
2 Correct 9 ms 13292 KB Output is correct
3 Correct 9 ms 13292 KB Output is correct
4 Correct 9 ms 13292 KB Output is correct
5 Correct 9 ms 13292 KB Output is correct
6 Correct 9 ms 13292 KB Output is correct
7 Correct 9 ms 13292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 396 ms 29192 KB Output is correct
2 Correct 499 ms 29128 KB Output is correct
3 Correct 750 ms 30404 KB Output is correct
4 Correct 1109 ms 50108 KB Output is correct
5 Correct 1453 ms 55360 KB Output is correct
6 Correct 1132 ms 51532 KB Output is correct
7 Correct 162 ms 25680 KB Output is correct
8 Correct 281 ms 45184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 13420 KB Output is correct
2 Correct 11 ms 13420 KB Output is correct
3 Correct 11 ms 13420 KB Output is correct
4 Correct 9 ms 13420 KB Output is correct
5 Correct 1193 ms 56556 KB Output is correct
6 Correct 1399 ms 58308 KB Output is correct
7 Correct 1434 ms 54808 KB Output is correct
8 Correct 277 ms 45184 KB Output is correct
9 Correct 284 ms 33920 KB Output is correct
10 Correct 292 ms 34276 KB Output is correct
11 Correct 302 ms 36216 KB Output is correct
12 Correct 162 ms 25680 KB Output is correct
13 Correct 277 ms 45320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 13420 KB Output is correct
2 Correct 10 ms 13420 KB Output is correct
3 Correct 10 ms 13420 KB Output is correct
4 Correct 11 ms 13420 KB Output is correct
5 Correct 875 ms 44416 KB Output is correct
6 Correct 1036 ms 47576 KB Output is correct
7 Correct 1155 ms 51020 KB Output is correct
8 Correct 1265 ms 53948 KB Output is correct
9 Correct 527 ms 27232 KB Output is correct
10 Correct 443 ms 27516 KB Output is correct
11 Correct 522 ms 27120 KB Output is correct
12 Correct 440 ms 27516 KB Output is correct
13 Correct 521 ms 27244 KB Output is correct
14 Correct 467 ms 27352 KB Output is correct
15 Correct 167 ms 25712 KB Output is correct
16 Correct 284 ms 45184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 13292 KB Output is correct
2 Correct 9 ms 13292 KB Output is correct
3 Correct 9 ms 13292 KB Output is correct
4 Correct 9 ms 13292 KB Output is correct
5 Correct 9 ms 13292 KB Output is correct
6 Correct 9 ms 13292 KB Output is correct
7 Correct 9 ms 13292 KB Output is correct
8 Correct 396 ms 29192 KB Output is correct
9 Correct 499 ms 29128 KB Output is correct
10 Correct 750 ms 30404 KB Output is correct
11 Correct 1109 ms 50108 KB Output is correct
12 Correct 1453 ms 55360 KB Output is correct
13 Correct 1132 ms 51532 KB Output is correct
14 Correct 162 ms 25680 KB Output is correct
15 Correct 281 ms 45184 KB Output is correct
16 Correct 11 ms 13420 KB Output is correct
17 Correct 11 ms 13420 KB Output is correct
18 Correct 11 ms 13420 KB Output is correct
19 Correct 9 ms 13420 KB Output is correct
20 Correct 1193 ms 56556 KB Output is correct
21 Correct 1399 ms 58308 KB Output is correct
22 Correct 1434 ms 54808 KB Output is correct
23 Correct 277 ms 45184 KB Output is correct
24 Correct 284 ms 33920 KB Output is correct
25 Correct 292 ms 34276 KB Output is correct
26 Correct 302 ms 36216 KB Output is correct
27 Correct 162 ms 25680 KB Output is correct
28 Correct 277 ms 45320 KB Output is correct
29 Correct 10 ms 13420 KB Output is correct
30 Correct 10 ms 13420 KB Output is correct
31 Correct 10 ms 13420 KB Output is correct
32 Correct 11 ms 13420 KB Output is correct
33 Correct 875 ms 44416 KB Output is correct
34 Correct 1036 ms 47576 KB Output is correct
35 Correct 1155 ms 51020 KB Output is correct
36 Correct 1265 ms 53948 KB Output is correct
37 Correct 527 ms 27232 KB Output is correct
38 Correct 443 ms 27516 KB Output is correct
39 Correct 522 ms 27120 KB Output is correct
40 Correct 440 ms 27516 KB Output is correct
41 Correct 521 ms 27244 KB Output is correct
42 Correct 467 ms 27352 KB Output is correct
43 Correct 167 ms 25712 KB Output is correct
44 Correct 284 ms 45184 KB Output is correct
45 Correct 183 ms 23640 KB Output is correct
46 Correct 265 ms 23664 KB Output is correct
47 Correct 715 ms 33624 KB Output is correct
48 Correct 1190 ms 49612 KB Output is correct
49 Correct 225 ms 30456 KB Output is correct
50 Correct 209 ms 29540 KB Output is correct
51 Correct 270 ms 34552 KB Output is correct
52 Correct 264 ms 33628 KB Output is correct
53 Correct 209 ms 28920 KB Output is correct
54 Correct 289 ms 35200 KB Output is correct