[BZOJ4523] Routing table

Source: Internet
Author: User

For each query, we can use an array to maintain the insertion time at the first match length of I to calculate how many times it changed in [L,r]

Since the length is now monotonous, the selection will change when and only when the time is monotonous,

So we can change the number of times in [1,x] by the monotone stack calculation.

Ans=solve (R)-solve (L-1)

For multiple queries, we can insert queries into the trie tree, implementing multiple queries to share a maintenance array

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 #defineMAXN 10000055 #defineMaxnd 150000006 structnode{ll X;intL,r;} A[MAXN],Q[MAXN];7 intn,aa,qq;8 inttrie[maxnd][2],nd,mark[maxnd],sta[ +];9 intRead () {Ten     inttmp=0;CharCh=0; One      while(ch<'0'|| Ch>'9') ch=GetChar (); A      while(ch>='0'&&ch<='9') tmp=tmp*Ten+ch-'0', ch=GetChar (); -     returntmp; - } the voidInsert (ll x) { -     intp=0; -      for(intI= +; i;i--){ -         intT=x>> (I-1) &1; +         if(!trie[p][t]) trie[p][t]=++nd; -p=Trie[p][t]; +     } A } at voidinit () { -n=read (); -     Charop[3]; -      for(intI=1; i<=n;i++){ -scanf"%s", op); -         if(op[0]=='A'){ inaa++; -              for(intj=0;j<4; j + +) toA[aa].x= (a[aa].x<<8)|read (); +A[aa].l=read (); -         } the         Else{ *qq++; $              for(intj=0;j<4; j + +)Panax NotoginsengQ[qq].x= (q[qq].x<<8)|read (); -Q[qq].l=read (), q[qq].r=read (); the Insert (q[qq].x); +         } A     } the } + voidGetmark (intPOS) { -     intp=0; $      for(intI= +;i> +-a[pos].l;i--){ $         intT=a[pos].x>> (I-1) &1; -         if(!trie[p][t])return; -p=Trie[p][t]; the     } -     if(!mark[p]) mark[p]=Pos;Wuyi } the intGetans (ll X,intrng) { -     intp=0, top=0; Wu      for(intI= +; i;i--){ -         intT=x>> (I-1) &1; Aboutp=Trie[p][t]; $         if(mark[p]&&mark[p]<=rng) { -              while(Top&&mark[p]<sta[top]) top--; -sta[++top]=Mark[p]; -         } A     } +     returntop; the } - voidsolve () { $      for(intI=1; i<=aa;i++) the Getmark (i); the      for(intI=1; i<=qq;i++){ the         intAns1=getans (q[i].x,q[i].l-1); the         intAns2=Getans (Q[I].X,Q[I].R); -printf"%d\n", ans2-ans1); in     } the } the intMain () { About init (); the solve (); the     return 0; the}
View Code

[BZOJ4523] Routing table

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.