莫队模板代码

 1 //node保存询问的区间、顺序
 2 //arr保存1-n的数字数组
 3 //cnt记数数组
 4 bool cmp(node a,node b){
 5     if(a.l/bblock != b.l/bblock)
 6         return   a.l/bblock < b.l/bblock;
 7     return a.r<b.r;
 8 }
 9 
10 void update(){
11     int val=arr[pos];
12     if(n==1)
13         ans+=2*cnt[val]+1;
14     else
15         ans-=2*cnt[val]+1;
16     cnt[val]+=n;
17 }
18 
19 void solve(){
20     for(int i=1;i<=m;i++){
21         while(L>node[i].l)
22             update(--L,1);
23         while(R<node[i].r)
24             update(++R,1);
25         while(L<node[i].l)
26             update(L++,-1);
27         while(R>node[i].r)
28             update(R--,-1);
29         res[node[i].id]=ans;
30     }
31 }

猜你喜欢

转载自www.cnblogs.com/gn1314/p/11444238.html
今日推荐