[BZOJ4523] [CQOI2016] 路由表 - Trie - 单调栈

来源:互联网 发布:战舰世界排位赛数据 编辑:程序博客网 时间:2024/05/16 13:50

对于加入就直接跑一遍在最后的结点标记时间就行了,询问就是将整个段维护一个单调栈,保证每个时间所在的位置之后没有比其小的时间即可,具体看代码。

#include"bits/stdc++.h"using namespace std;const int L=40000005;char _buff[L];int _pos;void read(){fread(_buff,L,1,stdin);}#define gch _buff[_pos++]inline void read(char&a,bool flag){while((a=gch)==' '||a=='\n'||a=='\r');}template<class integer>inline void read(integer&x){    x=0;int f=1;char ch=gch;    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=gch;}    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gch;}    x*=f;} const int N=32000005,M=1000005;int go[N][2],ti[N],k,Q,c,rt;const int move=256;struct IP{    int now,L;    unsigned int id;    inline void pre(bool flag){        id=now=L=0;        for(int i=0,j;i<4;i++)            id=id*move+(read(j),j);        if(flag)read(L);else L=32;    }    inline bool end(){return now>=L;}    inline bool nxt(){return !!(id&1ll<<(31-now++));}} x; void add(int&now){    if(!now)now=++k;    if(x.end()){ti[now]=c;return;}    bool e=x.nxt();add(go[now][e]);} int st[40],sl; void qry(int now){    if(ti[now]){        while(st[sl]>ti[now])sl--;        st[++sl]=ti[now];    }    if(x.end())return;    qry(go[now][x.nxt()]);}int main(){    read();read(Q);int i,j,ans;char opt;k=rt=1;    for(i=1;i<=Q;i++){        read(opt,1);x.pre(opt=='A');int l,r;        if(opt=='A')++c,add(rt);        else {            read(l),read(r),sl=0,qry(1);            for(j=1,ans=0;j<=sl;j++)                if(st[j]>=l&&st[j]<=r)                    ans++;            printf("%d\n",ans);        }    }    return 0;}


0 0
原创粉丝点击