莫隊演算法模板

來源:互聯網
上載者:User

標籤:std   return   print   add   names   node   const   sort   莫隊演算法   

 1 #include <bits/stdc++.h> 2  3 using namespace std; 4 const int N = 1<<20; 5 struct node 6 { 7     int l,r,id; 8 }Q[N]; 9 10 int ans[N];11 long long answer[N];12 long long flag[N];13 int a[N];14 bool cmp(node x,node y)15 {16     if(ans[x.l]==ans[y.l])17         return x.r<y.r;18     return ans[x.l]<ans[y.l];19 }20 int n,m,k;21 int L = 1,R = 0;22 long long Ans= 0;23 void add(int x)24 {25     Ans+=flag[a[x]^k];26     flag[a[x]]++;27 }28 void del(int x)29 {30     flag[a[x]]--;31     Ans-=flag[a[x]^k];32 }33 int main()34 {35 36     scanf("%d%d%d",&n,&m,&k);37     int sz = sqrt(n);38     for(int i = 1; i <= n; i++){39         scanf("%d",&a[i]);40         a[i] = a[i-1]^a[i];41         ans[i] = i/sz;42     }43     flag[0] = 1;44     for(int i = 1; i <= m; i++)45     {46         scanf("%d%d",&Q[i].l,&Q[i].r);47         Q[i].id = i;48     }49     sort(Q+1,Q+m+1,cmp);50     for(int i = 1; i <= m; i++)51     {52         while(L<Q[i].l)53         {54             del(L-1);55             L++;56         }57         while(L>Q[i].l)58         {59             L--;60             add(L-1);61         }62         while(R<Q[i].r)63         {64             R++;65             add(R);66         }67         while(R>Q[i].r)68         {69             del(R);70             R--;71         }72         answer[Q[i].id] = Ans;73     }74     for(int i = 1; i <= m; i++)75     {76         printf("%lld\n",answer[i]);77     }78     return 0;79 }

 

莫隊演算法模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.