其他分享
首页 > 其他分享> > 【洛谷 P2709】 小B的询问

【洛谷 P2709】 小B的询问

作者:互联网

小B的询问

洛谷

题目大意

最基础的莫队算法

show the code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep2(i,st,ed) for(int i = st; i < ed; ++i)
#define mk(x,y) make_pair(x,y)
#define pb(x) push_back(x)
const ll mod = 1e9 + 7;
const ll N = 1e5 + 200;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;

int a[N], belong[N];
int cnt[N], ans[N];
struct nod{
    int l,r;
    int id;
}q[N];

bool cmp(nod a, nod b){
    return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
}
bool cmp2(nod a ,nod b){
    return belong[a.l] == belong[b.l] ? a.r < b.r : belong[a.l] < belong[b.l];  //奇偶排序,相比普通比较,时间复杂度优化十分明显
}
int main(){
    ios::sync_with_stdio(false);
    int n,m,k;
    cin>>n>>m>>k;
    int siz = (int)sqrt(n);
    rep(i,n){
        cin>>a[i+1];
        belong[i] = i/siz + 1;		//分块
    }
    rep(i,m){
        cin>>q[i].l>>q[i].r;
        q[i].id = i;
    }
    sort(q,q+m,cmp);				//将查询排序
    int l = q[0].l;						//left,right指针,进行区间的遍历
    int r = l-1;
    int now = 0;
    rep(i,m){
        int ql = q[i].l, qr = q[i].r;
        while(l>ql)l--,cnt[a[l]]++,now +=2*cnt[a[l]]-1;
        while(r<qr)r++,cnt[a[r]]++,now +=2*cnt[a[r]]-1;
        while(l<ql)cnt[a[l]]--,now -=2*cnt[a[l]]+1,l++;
        while(r>qr)cnt[a[r]]--,now -=2*cnt[a[r]]+1,r--;
        ans[q[i].id] = now;
    }
    rep(i,m){
        cout<<ans[i]<<endl;
    }

    return 0;
}

标签:cnt,洛谷,int,询问,belong,nod,P2709,rep,define
来源: https://blog.csdn.net/qq_41991981/article/details/106281100