登录日志
题目。\ 水了,1h 过的。
如果 login/logout。\
因此考虑对于每一个 login 预处理出对应的影响区间,记区间为
::::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define N 300005
#define max(A,B) ((A)>(B)?(A):(B))
#define inf 1000000005
int n,q,m,La[N],a[N],ans[N];
struct ppp{
int u,L,R;
}tk[N<<1];
struct ppp2{
int id,R;
};
struct ppp3{
int da,lz;
};
bitset<N>vis;
vector<int>lgin[N],lgout[N];
vector<ppp2>qry[N];
#define mid ((l+r)>>1)
#define emp (l == r)
#define in (ql <= l && r <= qr)
struct SEG{
ppp3 tree[N<<2];
void build(const int x,const int l,const int r){
tree[x].lz = 0;
if(emp){tree[x].da = a[l];return;}
build(x<<1,l,mid),build(x<<1|1,mid+1,r);
tree[x].da = max(tree[x<<1].da,tree[x<<1|1].da);
}
void pushdown(const int x){
if(tree[x].lz == 0) return;
tree[x<<1].da += tree[x].lz,tree[x<<1].lz += tree[x].lz;
tree[x<<1|1].da += tree[x].lz,tree[x<<1|1].lz += tree[x].lz;
tree[x].lz = 0;
}
void update(const int x,const int l,const int r,const int ql,const int qr,const int d){
if(in){tree[x].da += d,tree[x].lz += d;return;}
pushdown(x);
if(ql <= mid) update(x<<1,l,mid,ql,qr,d);
if(qr > mid) update(x<<1|1,mid+1,r,ql,qr,d);
tree[x].da = max(tree[x<<1].da,tree[x<<1|1].da);
}
int ask(const int x,const int l,const int r,const int ql,const int qr){
if(in) return tree[x].da;
pushdown(x);
int Ans1 = -inf,Ans2 = -inf;
if(ql <= mid) Ans1 = ask(x<<1,l,mid,ql,qr);
if(qr > mid) Ans2 = ask(x<<1|1,mid+1,r,ql,qr);
return max(Ans1,Ans2);
}
}T;
#undef mid
#undef emp
#undef in
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> q;
for(int i = 1,op,u;i <= n;++i){
cin >> op >> u;
a[i] = a[i-1];
if(La[u] != 0) tk[++m] = (ppp){u,La[u],i-1},La[u] = 0;
if(op == 1){
if(vis[u] == 0) ++a[i];
La[u] = i,vis[u] = 1;
}
else if(vis[u] == 1) vis[u] = 0,--a[i];
}
for(int i = 1;i <= n;++i) if(La[i] != 0) tk[++m] = (ppp){i,La[i],n};
for(int i = 1;i <= m;++i){
lgin[tk[i].L].push_back(i);
if(tk[i].R != n) lgout[tk[i].R+1].push_back(i);
}
for(int i = 1,L,R;i <= q;++i){
cin >> L >> R;
qry[L].push_back((ppp2){i,R});
}
T.build(1,1,n);
for(int i = 1;i <= n;++i){
for(int j : lgout[i]) T.update(1,1,n,tk[j].L,tk[j].R,1);
if(i != 1) for(int j : lgin[i-1]) T.update(1,1,n,tk[j].L,tk[j].R,-1);
for(ppp2 qfr : qry[i]) ans[qfr.id] = T.ask(1,1,n,i,qfr.R);
}
for(int i = 1;i <= q;++i) cout << ans[i] << '\n';
return 0;
}
::::