結果

問題 No.2338 Range AtCoder Query
ユーザー lgswdnlgswdn
提出日時 2023-06-02 22:41:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,169 ms / 4,000 ms
コード長 3,008 bytes
コンパイル時間 3,122 ms
コンパイル使用メモリ 202,544 KB
実行使用メモリ 16,180 KB
最終ジャッジ日時 2023-08-28 04:54:06
合計ジャッジ時間 42,728 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,752 KB
testcase_01 AC 4 ms
13,748 KB
testcase_02 AC 4 ms
13,756 KB
testcase_03 AC 4 ms
13,796 KB
testcase_04 AC 4 ms
13,784 KB
testcase_05 AC 3 ms
13,708 KB
testcase_06 AC 4 ms
13,796 KB
testcase_07 AC 5 ms
13,968 KB
testcase_08 AC 4 ms
13,728 KB
testcase_09 AC 4 ms
13,764 KB
testcase_10 AC 5 ms
13,692 KB
testcase_11 AC 1,747 ms
15,480 KB
testcase_12 AC 1,564 ms
15,640 KB
testcase_13 AC 1,833 ms
15,468 KB
testcase_14 AC 1,964 ms
15,180 KB
testcase_15 AC 1,989 ms
15,840 KB
testcase_16 AC 2,137 ms
15,944 KB
testcase_17 AC 2,139 ms
15,964 KB
testcase_18 AC 2,047 ms
16,012 KB
testcase_19 AC 2,148 ms
15,960 KB
testcase_20 AC 2,150 ms
15,960 KB
testcase_21 AC 2,072 ms
16,180 KB
testcase_22 AC 2,120 ms
16,164 KB
testcase_23 AC 2,169 ms
15,984 KB
testcase_24 AC 2,143 ms
16,080 KB
testcase_25 AC 2,043 ms
16,056 KB
testcase_26 AC 42 ms
13,940 KB
testcase_27 AC 36 ms
13,984 KB
testcase_28 AC 37 ms
13,980 KB
testcase_29 AC 1,599 ms
15,936 KB
testcase_30 AC 1,729 ms
16,020 KB
testcase_31 AC 809 ms
16,056 KB
testcase_32 AC 806 ms
15,940 KB
testcase_33 AC 150 ms
16,024 KB
testcase_34 AC 153 ms
16,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}

#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define ac 1
#define wa 0
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
  return x*w;
}

const int N=2e5+5;
int n,m,Q,b[N],bs,p[N],L,R,curac,curpen,st[N],hd[N],nxtsub[N],nxtac[N],disac[N],lstsub[N],lstac[N],pen[N],acc[N];
char tmp[5];
pii ans[N];

struct qry {int l,r,id;} q[N];
bool cmp(const qry &x,const qry &y) {
  return b[x.l]==b[y.l]?x.r<y.r:b[x.l]<b[y.l];
}

void work(int x) {
  if(!hd[x]) pen[x]=acc[x]=0;
  else if(nxtac[hd[x]]>R) pen[x]=acc[x]=0;
  else acc[x]=1, pen[x]=disac[hd[x]];
}
void addleft(int x) {
  curac-=acc[p[x]], curpen-=pen[p[x]];
  hd[p[x]]=x; work(p[x]);
  curac+=acc[p[x]], curpen+=pen[p[x]];
}
void delleft(int x) {
  curac-=acc[p[x]], curpen-=pen[p[x]];
  if(nxtsub[x]<=R) hd[p[x]]=nxtsub[x];
  else hd[p[x]]=0;
  work(p[x]);
  curac+=acc[p[x]], curpen+=pen[p[x]];
}
void addright(int x) {
  curac-=acc[p[x]], curpen-=pen[p[x]];
  if(!hd[p[x]]) hd[p[x]]=x;
  work(p[x]);
  curac+=acc[p[x]], curpen+=pen[p[x]];
}
void delright(int x) {
  curac-=acc[p[x]], curpen-=pen[p[x]];
  if(x==hd[p[x]]) hd[p[x]]=0;
  work(p[x]);
  curac+=acc[p[x]], curpen+=pen[p[x]];
}

signed main() {
  n=read(), m=read(), Q=read();
  bs=sqrt(n);
  rep(i,1,n) b[i]=(i-1)/bs+1;
  rep(i,1,n) {
    scanf("%d%s",&p[i],tmp);
    if(tmp[0]=='W') st[i]=wa;
    else st[i]=ac;
  }
  rep(i,1,m) lstsub[i]=lstac[i]=n+1;
  per(i,n,1) {
    nxtsub[i]=lstsub[p[i]];
    nxtac[i]=lstac[p[i]];
    disac[i]=disac[lstsub[p[i]]]+1;
    lstsub[p[i]]=i;
    if(st[i]==ac) lstac[p[i]]=i, nxtac[i]=i, disac[i]=0;
  }
  rep(i,1,Q) q[i].l=read(), q[i].r=read(), q[i].id=i;
  sort(q+1,q+Q+1,cmp);
  L=1, R=0;
  rep(i,1,Q) {
    while(L>q[i].l) --L, addleft(L);
    while(R<q[i].r) ++R, addright(R);
    while(L<q[i].l) L++, delleft(L-1);
    while(R>q[i].r) R--, delright(R+1);
    ans[q[i].id]=pii(curac,curpen);
  }
  rep(i,1,Q) printf("%d %d\n",ans[i].fi,ans[i].se);
  return 0;
}
0