询问某区间内有多少满足某个要求的/某区间以某式子计算出的值(莫队算法,不修改只询问区间)

我觉得能用线段树就用线段树吧,实在用不了了再来考虑莫队算法,比如下面两道题:

http://codeforces.com/contest/617/problem/E  (询问某区间内有多少个子区间的异或值是K)

题解:http://blog.csdn.net/huayunhualuo/article/details/50585720

#include<bits/stdc++.h>
#define LL long long  
using namespace std;  
const int Max = 1100000;
const int MAXM = 1<<22;
typedef struct node
{
    int L ,R;
    int Id;
}Point ;
Point a[Max];
LL sum[Max];
LL ans[Max];
int n,m;
LL k;
int L,R;
LL cnt[MAXM],ant;
bool cmp(Point b,Point c)//将区间分块排序
{
    if(b.L/400==c.L/400)
    {
        return b.R<c.R;
    }
    else
    {
        return b.L<c.L;
    }
}
void Dec(LL s) //将多算的数目去除
{
    --cnt[s];

    ant-=cnt[s^k];
}

void Inc(LL s)//将没有遍历的点对应的数目加上
{
    ant += cnt[s^k];

    cnt[s]++;
}

int main()
{
    scanf("%d %d %lld",&n,&m,&k);
    LL data;

    for(int i=1;i<=n;i++) 
    {
        scanf("%lld",&sum[i]);
		sum[i]^=sum[i-1];
    }

    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&a[i].L,&a[i].R);
        a[i].Id = i;
        a[i].L--;// 在这里提前处理
    }
    sort(a+1,a+m+1,cmp);
    L=0,R=0,cnt[0]=1,ant=0;
    for(int i=1;i<=m;i++)
    {
        while(R<a[i].R)
        {
	        R++;
            Inc(sum[R]);
        }
        while(R>a[i].R)
        {
            Dec(sum[R]);
		    R--;
        }
        while(L<a[i].L)
        {		
            Dec(sum[L]);
		    L++;
        }
        while(L>a[i].L)
        {
		    --L;
            Inc(sum[L]);
        } 
        ans[a[i].Id]=ant;
    }
    for(int i=1;i<=m;i++)
    {
        printf("%lld\n",ans[i]);
    }
    return 0;
}

http://www.lydsy.com/JudgeOnline/problem.php?id=2038 (询问某区间内取到一对值相等的数的概率)

#include<bits/stdc++.h>
#define LL long long  
using namespace std;  
const int Max = 50010;
const int MAXM = 50010; 
typedef struct node  
{  
    int L ,R;  
    int Id;  
}Point ;  
Point a[Max];  
LL sum[Max];  
pair<LL,LL> ans[Max];  
int n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值