bzoj3809

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3809

题目大意:

    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]的权值的种类数。
题解:莫队+树状数组
代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define maxn 100000
 7 using namespace std;
 8 int n,m;
 9 int pos[maxn],val[maxn],c[maxn],bo[maxn],ans[maxn*10];
10 struct data{
11     int l,r,a,b,id;
12 }a[maxn*10];
13 int read()
14 {
15     int x=0; char ch; bool bo=0;
16     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;
17     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9') ;
18     if (bo) return -x; return x;
19 }
20 int lowbit(int x){ return x&-x;
21 }
22 void add(int x,int v)
23 {
24     for (int i=x; i<=n; i+=lowbit(i)) c[i]+=v;
25 }
26 int query(int x)
27 {
28     int res=0;
29     for (int i=x; i; i-=lowbit(i)) res+=c[i];
30     return res;
31 }
32 bool cmp(data a,data b)
33 {
34     if (pos[a.l]==pos[b.l]) 
35         if (pos[a.l]&1) return a.r<b.r;
36         else return a.r>b.r;
37     return pos[a.l]<pos[b.l];
38 }
39 void init()
40 {
41     n=read(),m=read();
42     for (int i=1; i<=n; i++) val[i]=read();
43     for (int i=1; i<=m; i++)
44     {
45         a[i].l=read(),a[i].r=read(),a[i].a=read(),a[i].b=read(); a[i].id=i;
46     }
47     int kk=int (sqrt(n));
48     for (int i=1; i<=n; i++) pos[i]=(i-1)/kk+1;
49     sort(a+1,a+1+m,cmp);
50 }
51 void updata(int k,int vval)
52 {
53     if (vval==-1)
54     {
55         if (bo[val[k]]==1)    add(val[k],-1);
56         bo[val[k]]--;
57     }
58     else
59     {
60         if (!bo[val[k]]) add(val[k],1);
61         bo[val[k]]++;
62     }
63 }
64 void work()
65 {
66     int r=0,l=1;
67     for (int i=1; i<=m; i++)
68     {
69         for (; r<a[i].r; r++) updata(r+1,1);
70         for (; r>a[i].r; r--) updata(r,-1);
71         for (; l<a[i].l; l++) updata(l,-1);
72         for (; l>a[i].l; l--) updata(l-1,1);
73         ans[a[i].id]=query(a[i].b)-query(a[i].a-1);
74     }
75     for (int i=1;i<=m; i++)
76         printf("%d
",ans[i]);
77 }
78 int main()
79 {
80     init();
81     work();
82 }
View Code

注:还是我太渣,都垫底了,据说还可以分块+莫队,会快的飞起。

原文地址:https://www.cnblogs.com/HQHQ/p/5573457.html