답안 #401576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401576 2021-05-10T13:45:58 Z kai824 가로등 (APIO19_street_lamps) C++17
100 / 100
1195 ms 146668 KB
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define eb emplace_back
#define mp make_pair
typedef pair<int,int> pii;
#define f first
#define s second

#ifdef local
#define debug(x,label) cerr<<"DEBUG "<<label<<" "<<x<<'\n';
#else
#define debug(x,label);
#endif

#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a>b)?a:b)

//const int mod=1000000007 or 998244353;
#define ls(x) (x&(-x))
int n,q,ft[300005];
int ans[300005],curr=0;
/*map<int,int> ft[300005];
void update(int a,int b,int upd){b++;
  //cout<<a<<' '<<b<<' '<<upd<<" UPDATE\n";
  for(;a<=n;a+=ls(a)){
    for(int x=b;x<=n;x+=ls(x)){
      if(ft[a].find(x)==ft[a].end())ft[a][x]=upd;
      else ft[a][x]+=upd;
    }
  }
}
int query(int a,int b){b++;
  int ans=0;
  for(;a;a-=ls(a)){
    for(int x=b;x;x-=ls(x)){
      //if(ft[a].find(x)==ft[a].end())continue;
      ans+=ft[a][x];
    }
  }
  return ans;
}*/ //sparse 2d fenwick TLE :(
void upddd(int p,int v){
  debug(p,"update p")
  for(;p<=n;p+=ls(p))ft[p]+=v;
}
int query(int p){
  debug(p,"query p")
  int ansss=0;
  for(;p;p-=ls(p))ansss+=ft[p];
  return ansss;
}
struct node{
  int s,e;
  vector<pair<pii,int> > v;//type,b, update value OR ans index...
  node *l,*r;
  node(int ss,int ee){
    s=ss;e=ee;
    if(s==e)l=r=NULL;
    else{
      l=new node(s,(s+e)/2);r=new node((s+e)/2+1,e);
    }
  }
  void upd(int a,int b,int c,bool upda=1){//1 to a...
    if(s==1 && e==n){
      b++;
      //if(upda)cout<<a<<' '<<b<<' '<<'\n';
    }
    if(upda==false) goto end;
    if(a<=s){
      if(upda)v.eb(mp(1,b),c);//update
      else v.eb(mp(2,b),c);//query
      return;
    }
    if(a<=(s+e)/2)l->upd(a,b,c,upda);
    r->upd(a,b,c,upda);
    return;

    end:;
    if(true){
      if(upda)v.eb(mp(1,b),c);//update
      else v.eb(mp(2,b),c);//query
    }
    if(s==e)return;
    if(a>(s+e)/2)r->upd(a,b,c,upda);
    else l->upd(a,b,c,upda);
  }
  void dfs(){
    for(auto xx:v){
      if(xx.f.f==1){
        upddd(xx.f.s,xx.s);
      }else ans[xx.s]+=query(xx.f.s);
    }
    for(auto xx:v){
      if(xx.f.f==1)upddd(xx.f.s,-xx.s);//reset ft
    }
    if(s==e)return;
    l->dfs();r->dfs();
  }
} *root;
#define update root->upd

bool lam[300005];

int32_t main(){
  ios_base::sync_with_stdio(false);cin.tie(0);
  int prev=0,a,b,c,d;
  cin>>n>>q;
  root=new node(1,n);
  string s;
  cin>>s;
  set<pair<pii,int> > cur;//"cur ranges"
  set<pair<pii,int> >::iterator it;
  for(int i=0;i<n;i++){
    if(s[i]=='0'){
      if(prev<=i-1){
        cur.insert(mp(mp(prev+1,i),0));
      }
      prev=i+1;
    }
    lam[i+1]=(s[i]=='1');
  }
  if(prev<=n-1){
    cur.insert(mp(mp(prev+1,n),0));
  }

  for(int i=1;i<=q;i++){
    cin>>s;
    if(s[0]=='t'){
      cin>>a;
      if(lam[a]){
        it=--cur.upper_bound(mp(mp(a,INT_MAX),-1));
        b=it->f.f;c=it->f.s;//range b to c...
        root->upd(b,n-c,i-it->s);
        cur.erase(it);
        if(b<a)cur.insert(mp(mp(b,a-1),i));
        if(a<c)cur.insert(mp(mp(a+1,c),i));
      }else{
        b=c=a;
        if(lam[a-1]){
          it=--cur.upper_bound(mp(mp(a-1,INT_MAX),-1));
          b=it->f.f;
          root->upd(it->f.f,n-it->f.s,i-it->s);
          cur.erase(it);
        }
        if(lam[a+1]){
          it=--cur.upper_bound(mp(mp(a+1,INT_MAX),-1));
          c=it->f.s;
          root->upd(it->f.f,n-it->f.s,i-it->s);
          cur.erase(it);
        }
        cur.insert(mp(mp(b,c),i));
      }
      lam[a]^=1;
    }else{
      ans[curr]=0;
      cin>>a>>b;b--;
      it=cur.upper_bound(mp(mp(a,INT_MAX),-1));
      if(it!=cur.begin()){
        it--;
        if(it->f.f<=a && b<=it->f.s){
          ans[curr]+=(i-it->s);
        }
      }
      root->upd(a,n-b,curr,0);
      //ans+=query(a,n-b);//if first index >=a, then n-first index<=n-a
      //cout<<ans<<'\n';
      curr++;
    }
  }
  //for(int i=0;i<curr;i++)cout<<ans[i]<<' ';cout<<'\n';
  debug('x',"dfsing")
  root->dfs();
  for(int i=0;i<curr;i++){
    cout<<ans[i]<<'\n';
  }
  return 0;
}
/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/

Compilation message

street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:107:20: warning: unused variable 'd' [-Wunused-variable]
  107 |   int prev=0,a,b,c,d;
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 201 ms 28628 KB Output is correct
2 Correct 246 ms 36444 KB Output is correct
3 Correct 432 ms 55256 KB Output is correct
4 Correct 1094 ms 122112 KB Output is correct
5 Correct 1195 ms 131512 KB Output is correct
6 Correct 1035 ms 117116 KB Output is correct
7 Correct 824 ms 145916 KB Output is correct
8 Correct 832 ms 146668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 962 ms 94144 KB Output is correct
6 Correct 1128 ms 115000 KB Output is correct
7 Correct 1165 ms 128568 KB Output is correct
8 Correct 799 ms 138544 KB Output is correct
9 Correct 138 ms 31708 KB Output is correct
10 Correct 149 ms 36560 KB Output is correct
11 Correct 154 ms 37272 KB Output is correct
12 Correct 802 ms 137540 KB Output is correct
13 Correct 814 ms 137648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 584 KB Output is correct
5 Correct 1058 ms 140860 KB Output is correct
6 Correct 1034 ms 127016 KB Output is correct
7 Correct 1098 ms 113228 KB Output is correct
8 Correct 1085 ms 97232 KB Output is correct
9 Correct 226 ms 30792 KB Output is correct
10 Correct 179 ms 19836 KB Output is correct
11 Correct 241 ms 30692 KB Output is correct
12 Correct 180 ms 19844 KB Output is correct
13 Correct 234 ms 30600 KB Output is correct
14 Correct 174 ms 20200 KB Output is correct
15 Correct 822 ms 137096 KB Output is correct
16 Correct 836 ms 138396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 201 ms 28628 KB Output is correct
9 Correct 246 ms 36444 KB Output is correct
10 Correct 432 ms 55256 KB Output is correct
11 Correct 1094 ms 122112 KB Output is correct
12 Correct 1195 ms 131512 KB Output is correct
13 Correct 1035 ms 117116 KB Output is correct
14 Correct 824 ms 145916 KB Output is correct
15 Correct 832 ms 146668 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 962 ms 94144 KB Output is correct
21 Correct 1128 ms 115000 KB Output is correct
22 Correct 1165 ms 128568 KB Output is correct
23 Correct 799 ms 138544 KB Output is correct
24 Correct 138 ms 31708 KB Output is correct
25 Correct 149 ms 36560 KB Output is correct
26 Correct 154 ms 37272 KB Output is correct
27 Correct 802 ms 137540 KB Output is correct
28 Correct 814 ms 137648 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 2 ms 584 KB Output is correct
33 Correct 1058 ms 140860 KB Output is correct
34 Correct 1034 ms 127016 KB Output is correct
35 Correct 1098 ms 113228 KB Output is correct
36 Correct 1085 ms 97232 KB Output is correct
37 Correct 226 ms 30792 KB Output is correct
38 Correct 179 ms 19836 KB Output is correct
39 Correct 241 ms 30692 KB Output is correct
40 Correct 180 ms 19844 KB Output is correct
41 Correct 234 ms 30600 KB Output is correct
42 Correct 174 ms 20200 KB Output is correct
43 Correct 822 ms 137096 KB Output is correct
44 Correct 836 ms 138396 KB Output is correct
45 Correct 92 ms 12888 KB Output is correct
46 Correct 134 ms 19136 KB Output is correct
47 Correct 582 ms 60264 KB Output is correct
48 Correct 1119 ms 117928 KB Output is correct
49 Correct 170 ms 40720 KB Output is correct
50 Correct 168 ms 39724 KB Output is correct
51 Correct 178 ms 41872 KB Output is correct
52 Correct 208 ms 49492 KB Output is correct
53 Correct 210 ms 49660 KB Output is correct
54 Correct 204 ms 49516 KB Output is correct