結果
| 問題 |
No.2338 Range AtCoder Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-02 22:41:24 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,300 ms / 4,000 ms |
| コード長 | 3,008 bytes |
| コンパイル時間 | 2,261 ms |
| コンパイル使用メモリ | 199,144 KB |
| 最終ジャッジ日時 | 2025-02-13 19:37:57 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 34 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:85:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
85 | scanf("%d%s",&p[i],tmp);
| ~~~~~^~~~~~~~~~~~~~~~~~
ソースコード
#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;
}