3809: Gty的二逼妹子序列 (莫队+树状数组)ps:强大的我没用分块,树状数组卡过去了哈哈

3809: Gty的二逼妹子序列

Time Limit: 80 Sec   Memory Limit: 28 MB
Submit: 1394   Solved: 402
[ Submit][ Status][ Discuss]

Description

Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
为了方便,我们规定妹子们的美丽度全都在[1,n]中。
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),对于m(1<=m<=1000000)次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。

Input

第一行包括两个整数n,m(1<=n<=100000,1<=m<=1000000),表示数列s中的元素数和询问数。
第二行包括n个整数s1...sn(1<=si<=n)。
接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
保证涉及的所有数在C++的int内。
保证输入合法。

Output

对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。

Sample Input

10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4

Sample Output

2
0
0
2
1
1
1
0
1
2

HINT

样例的部分解释:


5 9 1 2

子序列为4 1 5 1 2

在[1,2]里的权值有1,1,2,有2种,因此答案为2。


3 4 7 9

子序列为5 1

在[7,9]里的权值有5,有1种,因此答案为1。


4 4 2 5

子序列为1

没有权值在[2,5]中的,因此答案为0。


2 3 4 7

子序列为4 5

权值在[4,7]中的有4,5,因此答案为2。


建议使用输入/输出优化。

Source




哈哈哈,一开始就用莫队+树状数组写,TLE了,看到网上说树状数组会被卡,可我太懒不想改分块,就开始卡常,试了各种姿势都无用,最后胡乱把lowbit宏定义就卡过去啦!!!!!开心!


代码:

/**************************************************************
    Problem: 3809
    User: ********
    Language: C++
    Result: Accepted
    Time:78020 ms
    Memory:26304 kb
****************************************************************/
 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<climits>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define N 100001
#define M 1000001
#define lowbit(x) x&-x
using namespace std;
inline int read()
{
    int x=0,f=1;char ch;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,s[N],bel[N],B;
struct node{int l,r,a,b,id;}p[M];
bool operator < (node a,node b)
{return bel[a.l]==bel[b.l]?a.r<b.r:a.l<b.l;}
int C[N],ans[M],num[N];
//inline int lowbit(int x){return x&(-x);}
inline int ask(int x)
{
    int ret=0;
    while(x)
    {
        ret+=C[x];
        x-=lowbit(x);
    }return ret;
}
inline void add(int x,int val)
{
    while(x<=n)
    {
        C[x]+=val;
        x+=lowbit(x);
    }
}
void solve()
{
    int x=1,y=1;add(s[x],1);num[s[x]]++;
    for(int i=1;i<=m;i++)
    {
        while(x<p[i].l){num[s[x]]--;if(!num[s[x]])add(s[x],-1);x++;}
        while(x>p[i].l){x--;if(!num[s[x]])add(s[x],1);num[s[x]]++;}
        while(y<p[i].r){y++;if(!num[s[y]])add(s[y],1);num[s[y]]++;}
        while(y>p[i].r){num[s[y]]--;if(!num[s[y]])add(s[y],-1);y--;}
        ans[p[i].id]=ask(p[i].b)-ask(p[i].a-1);
    }
}
int buf[30];
inline void write(int x)
{
    if (x<0) putchar('-'),x=-x;
    buf[0]=0;
    while (x) buf[++buf[0]]=x%10,x/=10;
    if (!buf[0]) buf[0]=1,buf[1]=0;
    while (buf[0]) putchar('0'+buf[buf[0]--]);
}
int main()
{
//  freopen("3809.in","r",stdin);
//  freopen("3809.out","w",stdout);
    n=read();B=sqrt(n);m=read();
    for(int i=1;i<=n;i++)
        s[i]=read(),bel[i]=(i-1)/B+1;   
    for(int i=1;i<=m;i++)
    {
        p[i].l=read();p[i].r=read();
        p[i].a=read(),p[i].b=read();p[i].id=i;
    }sort(p+1,p+1+m);
    solve();
    for(int i=1;i<=m;i++)
    {
        write(ans[i]);
        puts("");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值