HDU6959 zoto
传送门
如果题目没让求区域内不同的\(y\)个个数,而是单纯\(y\)的数量,那传统的主席树或离线树状数组就可做了。
求区间内不同颜色,还是莫队给力。(虽然我有些反感将莫队作为正解的题目,但还是打不过就加入了,这东西确实简单)
莫队说白了就是暴力,只不过将询问离线,巧妙的排序后,使修改和查询的时间复杂度都均衡到了\(O(n \sqrt{n})\).
具体来说,就是将询问区间离线,分块,并以左端点所在块为第一关键字,右端点所在位置为第二关键字排序。然后依次考虑每一个询问区间,通过分别移动左右两个指针处理询问。
时间复杂度的证明略。
那对于这道题,完全就是莫队的板子题,只不过\(y\)那一维我们需要用树状数组进行维护,当某一个\(y\)值不再是\(0\)或是又变成了\(0\),就在树状数组上进行修改,而查询就是通过前缀和查询区间和。
上述做法时间复杂度上限是\(O(n\sqrt{n} \log n)\),但因为不是每一次都会在树状数组上修改,所以达不到上限,实测1800ms.
但题解给出了一个更为优秀的做法,对于\(y\)值的维护,用分块代替了树状数组。维护两个值\(buc[y],sum[i]\),分别表示\(y\)对应的桶,以及第\(i\)块里不为\(0\)的\(y\)值个数。那么修改的时候只需要修改这两个值,是\(O(1)\)的;而查询的时候就是分块的查询方法:中间的整块和两边的零散部分,是\(O(\sqrt{n})\)的。
这个和处理询问的莫队结合起来,会发现莫队\(O(\sqrt{n})\)的指针移动遇到了分块\(O(1)\)的修改,而莫队\(O(1)\)的查询,遇到了分块\(O(\sqrt{n})\)的查询。因此总时间复杂度\(O(n\sqrt{n})\),实测1500ms.
以下给出了对应的两份代码:
莫队+树状数组:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("1.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n, m, S, a[maxn];
struct Node
{
int L, R, blo, yl, yr, id;
bool operator < (const Node& oth)const
{
// return blo < oth.blo || (blo == oth.blo && R < oth.R);
if(blo ^ oth.blo) return blo < oth.blo;
return (blo & 1) ? R < oth.R : R > oth.R;
//优化:奇数块右端点从小到大,偶数块右端点从大到小,可将右端点移动步数减少一半
}
}t[maxn];
int c[maxn];
In int lowbit(int x) {return x & -x;}
In void c_add(int pos, int d)
{
for(; pos < maxn; pos += lowbit(pos)) c[pos] += d;
}
In int query(int pos)
{
int ret = 0;
for(; pos; pos -= lowbit(pos)) ret += c[pos];
return ret;
}
int buc[maxn], ans[maxn];
In void del(int x)
{
if(!--buc[a[x]]) c_add(a[x], -1);
}
In void add(int x)
{
if(!buc[a[x]]++) c_add(a[x], 1);
}
int main()
{
// MYFILE();
int T = read();
while(T--)
{
Mem(c, 0), Mem(buc, 0);
n = read(), m = read(); S = sqrt(n);
for(int i = 1; i <= n; ++i) a[i] = read() + 1;
for(int i = 1; i <= m; ++i)
{
int L = read(), yl = read() + 1, R = read(), yr = read() + 1;
t[i] = (Node){L, R, (L - 1) / S + 1, yl, yr, i};
}
sort(t + 1, t + m + 1);
for(int i = 1, l = 0, r = 0; i <= m; ++i)
{
while(l < t[i].L) del(l++);
while(l > t[i].L) add(--l);
while(r > t[i].R) del(r--);
while(r < t[i].R) add(++r);
ans[t[i].id] = query(t[i].yr) - query(t[i].yl - 1);
}
for(int i = 1; i <= m; ++i) write(ans[i]), enter;
}
return 0;
}
莫队+分块:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("1.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n, m, S, a[maxn];
struct Node
{
int L, R, blo, yl, yr, id;
bool operator < (const Node& oth)const
{
// return blo < oth.blo || (blo == oth.blo && R < oth.R);
if(blo ^ oth.blo) return blo < oth.blo;
return (blo & 1) ? R < oth.R : R > oth.R;
}
}t[maxn];
int buc[maxn], ans[maxn];
int Sy = 300, sum[maxn];
In int bl(const int& x) {return (x - 1) / Sy + 1;}
In void del(int x)
{
if(!--buc[a[x]]) sum[bl(a[x])]--;
}
In void add(int x)
{
if(!buc[a[x]]++) sum[bl(a[x])]++;
}
In int query(int L, int R)
{
int ret = 0, bL = bl(L), bR = bl(R);
if(bL == bR)
{
for(int i = L; i <= R; ++i) ret += (buc[i] != 0);
return ret;
}
for(int i = L; i <= bL * Sy; ++i) ret += (buc[i] != 0);
for(int i = (bR - 1) * Sy + 1; i <= R; ++i) ret += (buc[i] != 0);
for(int i = bL + 1; i < bR; ++i) ret += sum[i];
return ret;
}
int main()
{
// MYFILE();
int T = read();
while(T--)
{
Mem(buc, 0), Mem(sum, 0);
n = read(), m = read(); S = sqrt(n);
for(int i = 1; i <= n; ++i) a[i] = read() + 1;
for(int i = 1; i <= m; ++i)
{
int L = read(), yl = read() + 1, R = read(), yr = read() + 1;
t[i] = (Node){L, R, (L - 1) / S + 1, yl, yr, i};
}
sort(t + 1, t + m + 1);
for(int i = 1, l = 0, r = 0; i <= m; ++i)
{
while(l < t[i].L) del(l++);
while(l > t[i].L) add(--l);
while(r > t[i].R) del(r--);
while(r < t[i].R) add(++r);
ans[t[i].id] = query(t[i].yl, t[i].yr);
}
for(int i = 1; i <= m; ++i) write(ans[i]), enter;
}
return 0;
}