rt,在 P1972 [SDOI2009] HH的项链 中,如果对 c 进行 Hash 则可以使用莫队过,反之会被卡掉 40 分 代码如下
#include <bits/stdc++.h>
#define re register
using namespace std;
constexpr int mod=2000007;
constexpr int qt = 1024;
inline int read(){
int f = 1,x = 0;
char ch = getchar();
while(!isdigit(ch)){
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void print(int x){
if(x > 9)print(x / 10);
putchar(x % 10 + '0');
}
const int N = 1000001;
int n, a[N], cnt[N], ans[N];
struct node{
int l, r, id;
}q[N];
signed main(){
n = read();
for(re int i = 1;i <= n;++i)
a[i] = read();
re int m = read();
for(re int i = 1;i <= m;++i){
q[i].l = read(), q[i].r = read(), q[i].id = i;
}
std::sort(q + 1, q + m + 1, [](const node aa, const node bb){
return ((aa.l / qt) == (bb.l / qt)) ? (((aa.l / qt) & 1) ? aa.r < bb.r : aa.r > bb.r) : aa.l < bb.l;
});
re int l = q[1].l, r = q[1].l - 1, now = 0;
for(re int i = 1;i <= m;++i){
while(l < q[i].l) now -= !--cnt[a[l++]];
while(l > q[i].l) now += !cnt[a[--l]]++;
while(r < q[i].r) now += !cnt[a[++r]]++;
while(r > q[i].r) now -= !--cnt[a[r--]];
ans[q[i].id] = now;
}
for(int i = 1;i <= m;++i)
print(ans[i]), putchar('\n');
return 0;
}
以上是没有 Hash 的代码 60 point。 进行 Hash。
#include <bits/stdc++.h>
#define re register
using namespace std;
const int mod=2000007;
const int qt = 1024;
inline int read(){
int f = 1,x = 0;
char ch = getchar();
while(!isdigit(ch)){
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void print(int x){
if(x > 9)print(x / 10);
putchar(x % 10 + '0');
}
struct HASH{
int id[mod], val[mod], cnt;
int find(int x){
int z = x % mod;
while(id[z]){if(id[z] == x){return val[z];}++z;}
return id[z] = x, val[z] = ++cnt;
}
}Hash;
const int N = 1000001;
int n, a[N], ans[N];
struct node{
int l, r, id;
}q[N];
vector<int> cnt(N);
signed main(){
n = read();
for(re int i = 1;i <= n;++i)
a[i] = read(), a[i] = Hash.find(a[i]);
re int m = read();
for(re int i = 1;i <= m;++i){
q[i].l = read(), q[i].r = read(), q[i].id = i;
}
std::sort(q + 1, q + m + 1, [](const node aa, const node bb){
return ((aa.l / qt) == (bb.l / qt)) ? (((aa.l / qt) & 1) ? aa.r < bb.r : aa.r > bb.r) : aa.l < bb.l;
});
re int l = q[1].l, r = q[1].l - 1, now = 0;
for(re int i = 1;i <= m;++i){
while(l < q[i].l) now -= !--cnt[a[l++]];
while(l > q[i].l) now += !cnt[a[--l]]++;
while(r < q[i].r) now += !cnt[a[++r]]++;
while(r > q[i].r) now -= !--cnt[a[r--]];
ans[q[i].id] = now;
}
for(int i = 1;i <= m;++i)
print(ans[i]), putchar('\n');
return 0;
}
可以拿到 100 分