热门关键字:
jquery > jquery教程 > html5 > 莫队算法例题,模板及小结

莫队算法例题,模板及小结

380
作者:管理员
发布时间:2020/3/23 10:48:08
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=1084

  以上是弱渣的学习过程,及参考的大神博客和知乎,以下是做到的题目

  G-XORandFavoriteNumber

  Bobhasafavoritenumberkandaioflengthn.Nowheasksyoutoanswermqueries.Eachqueryisgivenbyapairliandriandasksyoutocountthenumberofpairsofintegersiandj,suchthatl ≤ i ≤ j ≤ randthexorofthenumbersai, ai + 1, …, ajisequaltok.

  Input

  Thefirstlineoftheinputcontainsintegersn,mandk(1 ≤ n, m ≤ 100 000,0 ≤ k ≤ 1 000 000)—thelengthofthearray,thenumberofqueriesandBob’sfavoritenumberrespectively.

  Thesecondlinecontainsnintegersai(0 ≤ ai ≤ 1 000 000)—Bob’sarray.

  Thenmlinesfollow.Thei-thlinecontainsintegersliandri(1 ≤ li ≤ ri ≤ n)—theparametersofthei-thquery.

  Output

  Printmlines,answerthequeriesintheordertheyappearintheinput.

  Examples

  Input

  623

  121103

  16

  35

  Output

  7

  0

  Input

  531

  11111

  15

  24

  13

  Output

  9

  4

  4

  Note

  Inthefirstsamplethesuitablepairsofiandjforthefirstqueryare:(1,2),(1,4),(1,5),(2,3),(3,6),(5,6),(6,6).Notasingleofthesepairsissuitableforthesecondquery.

  Inthesecondsamplexorequals1forallsubarraysofanoddlength.

  题目链接:https://vjudge.net/contest/239536#problem/G

  题解:由于a^a=0;

  如果a^b=k;则a^k=b;

  若要a[i]^a[i+1]….^a[j])=k;

  即(a[1]^a[2]^…a[i])^(a[1]^a[2]^…^a[i-1]^a[i]^a[i+1]….^a[j])。

  就可以采用前缀和思想。

  让flag[a[i]^k]表示a[i]^k出现的次数。





如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:html
友荐云推荐