[Trie] BZOJ4896: [Thu Summer Camp2016] Select __ String related

Source: Internet
Author: User
Tags stdin
the

X is a teacher of big T, and every year he teaches many students the basics of C + +. In t large, each student in the semester before the beginning of the class, each elective course is divided into three stages: preselection, is selected, the replacement of the election; the "fallback" stage is busiest. In the completion of the election stage, students can choose the course, can also be returned to class.
For teacher X, the following two events may occur during the completion of the withdrawal phase:
1: A student named S chose his class (name S will appear on the list of selected students in X)
2: A student named S has returned his class (name s will be removed from the list of selected students in X)
At the same time, teacher X is very concerned about which students have chosen his class, so he will query the list of selected students on a regular basis, the format of each query is as follows:
The number of students whose names are prefixed with s after the earliest event is more than VX teacher See your bones surprised, so want to use this question to test you, you certainly do not fear, so brave to take this task.
Note 1: The student's name may be the same, and if the students with the same name have been selected for teacher X's class, their names will appear on Teacher X's list p times.
NOTE 2: Only students who have already selected a class will retire, and if the student whose name is S is returned, then there must be a name on the list of teacher X before he can retire.
Note 3: Select classes, Retreat classes, and queries are defined as "events," and "events" are numbered starting from 1.
n<=100000, String length <= 60, all strings in the input will contain only the first 10 lowercase letters.
Force online. Solving

See prefix easy to think of trie, normal insert Delete, and at each point on a vector record the number of times the prefix appears to increase the time sequence, when asked on the above two points.

#include <cstdio> #include <vector> #include <algorithm> using namespace std;
    Inline Char GC () {static Char buf[100000],*p1=buf,*p2=buf; Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,100000,stdin), P1==P2)?
eof:*p1++;
    inline int getint () {char ch=gc (); int res=0,ff=1; while (!) ('
    0 ' <=ch&&ch<= ' 9 ')) {if (ch== '-') ff=-1; CH=GC ();}
    while (' 0 ' <=ch&&ch<= ' 9 ') res= (res<<3) + (res<<1) +ch-' 0 ', CH=GC ();
return RES*FF;
    struct node{int cnt; vector< pair<int,int> > evt;
    node* CH[10];    
Node (node* son=null) {cnt=0; evt.clear (); for (int i=0;i<=9;i++) Ch[i]=son;}
Nil, *null=&nil, *root=null;
typedef node* P_node; int Q;
Long Long Lstans;
    void Insert (P_node &p,char* now,int k) {if (P==null) p=new node (null); 
    p->cnt++;
    int len=p->evt.size (); if (len==0| |
    p->evt[len-1].first<p->cnt) P->evt.push_back (Make_pair (p->cnt,k)); if ((*now) = = ' \000 ') return; InsERT (p->ch[(*now)-' a '],now+1,k); 
    } void Erase (P_node p,char* now) {p->cnt--;
    if ((*now) = = ' \000 ') return;
Erase (p->ch[(*now)-' a '],now+1);
    int Query (P_node p,char* now,int val) {if (p==null) return-1;
        if ((*now) = = ' \000 ') {int len=p->evt.size (); if (len==0| |
        P->evt[len-1].first<val) return-1;
        int l=0,r=len-1;
            while (l<=r) {int mid= (L+R) >>1;
            if (p->evt[mid].first==val) return p->evt[mid].second;
                                 if (p->evt[mid].first<val) l=mid+1;
        else r=mid-1;
} return Query (p->ch[(*now)-' a '],now+1,val);
} Char st[65];
    int main () {freopen ("bzoj4896.in", "R", stdin);
    Freopen ("Bzoj4896.out", "w", stdout);
    Q=getint ();
        for (int ii=1;ii<=q;ii++) {int pd=getint (), m=-1; Char CH=GC (); while (!) ('
        A ' <=ch&&ch<= ' Z ') CH=GC (); for (; ' A ' <=ch&&ch<= ' z '; CH=GC ()) st[++m]=ch; St[m+1]= ' \000 '; if (pd==1) Insert (ROOT,ST,II); else if (pd==2) Erase (root,st);
            else if (pd==3) {int t1=getint (), T2=getint (), T3=getint ();
            if (lstans==-1) Lstans=1;
        printf ("%lld\n", Lstans=query (Root,st, (LSTANS*T1+T2)%t3+1));
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.