洛谷P1972 [SDOI2009]HH的项链
1 , 2 , 4 , 5 , 2 , 4 , 2 1,2,4,5,2,4,2 1,2,4,5,2,4,2
1 , 2 , 4 , 5 , 2 , 4 , 2 1,2,4,5,2,4,2 1,2,4,5,2,4,2
1 , 0 , 0 , 1 , 0 , 1 , 1 1,0,0,1,0,1,1 1,0,0,1,0,1,1
inline int read()
{
int ans = 0;
char r = getchar(); bool neg = false;
while (r<'0' || r>'9') { if (r == '-')neg = true; r = getchar(); }
while (r >= '0'&&r <= '9') { ans = ans * 10 + r - '0'; r = getchar(); }
return (neg) ? -ans : ans;
}
void write(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9)write(x / 10);
putchar(x % 10 + '0');
}//write不带换行符,需要自己putchar('\n')
bool operator < (const Query& a)const
{
return pos[l] ^ pos[a.l] ? pos[l] < pos[a.l] : pos[l] & 1 ? r<a.r : r>a.r;
}
int len = n / sqrt(n * 2 / 3);
#pragma GCC optimize(2)
#include
#include
#include
using namespace std;
inline int read()
{
int ans = 0;
char r = getchar(); bool neg = false;
while (r<'0' || r>'9') { if (r == '-')neg = true; r = getchar(); }
while (r >= '0'&&r <= '9') { ans = ans * 10 + r - '0'; r = getchar(); }
return (neg) ? -ans : ans;
}
void write(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9)write(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 1e6 + 10;
int pos[maxn];
struct Query
{
int l, r, id;
bool operator < (const Query& a)const
{
return pos[l] ^ pos[a.l] ? pos[l] < pos[a.l] : pos[l] & 1 ? r<a.r : r>a.r;
}
};
Query query[maxn];
int n, q, a[maxn];
int ans, cnt[maxn];
int ans0[maxn];
void solve()
{
scanf("%d", &n);
int len = n / sqrt(n * 2 / 3);
for (int i = 1; i <= n; i++)
a[i] = read(), pos[i] = (i - 1) / len + 1;
scanf("%d", &q);
for (int i = 1; i <= q; i++)
query[i].l = read(), query[i].r = read(), query[i].id = i;
sort(query + 1, query + 1 + q);
int l = 1, r = 0;
for (int i = 1; i <= q; i++)
{
int L = query[i].l, R = query[i].r;
while (l < L) if (cnt[a[l++]]-- == 1) ans--;
while (l > L)if (cnt[a[--l]]++ == 0) ans++;
while (r < R) if (cnt[a[++r]]++ == 0) ans++;
while (r > R) if (cnt[a[r--]]-- == 1) ans--;
ans0[query[i].id] = ans;
}
for (int i = 1; i <= q; i++)
write(ans0[i]), putchar('\n');
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}
#include
#include
using namespace std;
const int maxn = 1e6 + 10;
struct Query
{
int l, r, id;
bool operator < (const Query& a)const
{
return r < a.r;
}
};Query query[maxn];
struct Node
{
int l, r, sum;
}; Node tree[maxn * 4];
void build(int o, int l, int r)
{
tree[o].l = l, tree[o].r = r;
if (l == r)
return;
int mid = (l + r) / 2;
build(o * 2, l, mid);
build(o * 2 + 1, mid + 1, r);
}
void change(int o, int x, int type)
{
if (tree[o].l == tree[o].r)
{
tree[o].sum = type;
return;
}
int mid = (tree[o].l + tree[o].r) / 2;
if (x <= mid)
change(o * 2, x, type);
else
change(o * 2 + 1, x, type);
tree[o].sum = tree[o * 2].sum + tree[o * 2 + 1].sum;
}
int ask(int o, int l, int r)
{
if (tree[o].l == l && tree[o].r == r)
return tree[o].sum;
int mid = (tree[o].l + tree[o].r) / 2;
if (r <= mid)
return ask(o * 2, l, r);
else if (l > mid)
return ask(o * 2 + 1, l, r);
return ask(o * 2, l, mid) + ask(o * 2 + 1, mid + 1, r);
}
int n, q, a[maxn];
int ans0[maxn];
int las[maxn], pos[maxn];
void solve()
{
build(1, 1, maxn - 10);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), las[i] = pos[a[i]], pos[a[i]] = i;
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", &query[i].l, &query[i].r), query[i].id = i;
sort(query + 1, query + 1 + q);
int las_r = 1;
for (int i = 1; i <= q; i++)
{
int l = query[i].l, r = query[i].r;
for (int i = las_r; i <= r; i++)
{
if (las[i] != 0)
change(1, las[i], 0);
change(1, i, 1);
}
ans0[query[i].id] = ask(1, l, r);
las_r = r + 1;
}
for (int i = 1; i <= q; i++)
printf("%d\n", ans0[i]);
}
int main()
{
freopen("Testin.txt", "r", stdin);
solve();
return 0;
}