3809: Gty的二逼妹子序列

    xiaoxiao2021-12-14  15

    3809: Gty的二逼妹子序列

    Time Limit: 80 Sec   Memory Limit: 28 MB Submit: 1381   Solved: 399 [ 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

    [ Submit][ Status][ Discuss]

    

    如果用莫队处理询问,能用树状数组做到O(m*sqrt(n)*logn)

    不过这样显然是T飞的。。。用个小技巧

    对于权值分块维护,这样单次查询能O(sqrt(n)),修改O(1)

    总复杂度就能降一个log了

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<vector> #include<queue> #include<set> #include<map> #include<stack> #include<bitset> #include<ext/pb_ds/priority_queue.hpp> using namespace std; const int maxn = 1E5 + 10; const int maxm = 1E6 + 10; int n,m,Sqrt,sum[320],cnt[maxn],ans[maxm],s[maxn]; int Getpos(int x) { return (x % Sqrt == 0)?x / Sqrt:x / Sqrt + 1; } struct Query{ int l,r,a,b,num; Query(){} Query(int l,int r,int a,int b,int num): l(l),r(r),a(a),b(b),num(num){} bool operator < (const Query &B) const { if (Getpos(l) < Getpos(B.l)) return 1; if (Getpos(l) > Getpos(B.l)) return 0; return r < B.r; } }Q[maxm]; void Add(int x) { if (!cnt[x]) ++sum[Getpos(x)]; ++cnt[x]; } void Dec(int x) { if (cnt[x] == 1) --sum[Getpos(x)]; --cnt[x]; } int getint() { char ch = getchar(); int ret = 0; while (ch < '0' || '9' < ch) ch = getchar(); while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar(); return ret; } int main() { #ifdef DMC freopen("DMC.txt","r",stdin); #endif n = getint(); m = getint(); Sqrt = sqrt(n); for (int i = 1; i <= n; i++) s[i] = getint(); for (int i = 1; i <= m; i++) { int l,r,a,b; l = getint(); r = getint(); a = getint(); b = getint(); Q[i] = Query(l,r,a,b,i); } sort(Q + 1,Q + m + 1); int L = 1,R = 0; for (int i = 1; i <= m; i++) { while (R < Q[i].r) Add(s[++R]); while (R > Q[i].r) Dec(s[R--]); while (L < Q[i].l) Dec(s[L++]); while (L > Q[i].l) Add(s[--L]); int Ans = 0,pa = Getpos(Q[i].a),pb = Getpos(Q[i].b); if (pa == pb) { for (int j = Q[i].a; j <= Q[i].b; j++) Ans += (cnt[j])?1:0; } else { for (int j = pa + 1; j < pb; j++) Ans += sum[j]; for (int j = Q[i].a,t = pa*Sqrt; j <= t; j++) Ans += (cnt[j])?1:0; for (int j = pb*Sqrt - Sqrt + 1; j <= Q[i].b; j++) Ans += (cnt[j])?1:0; } ans[Q[i].num] = Ans; } for (int i = 1; i <= m; i++) printf("%d\n",ans[i]); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-965709.html

    最新回复(0)