洛谷 P4113 [HEOI2012]采花
-----
可将询问按L从小到大排序,每次将序列中花朵第二次出现的位置置为1,用树状数组维护;
--树状数组
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stdio.h>
using namespace std;
#define ls rt<<1
#define rs rt<<1|1
#define LL long long
#define PI acos(-1.0)
#define eps 1e-8
#define Pair pair<double,double>
// notice
#define mod 998244353
#define MAXN 2e6
#define MS 2000009
LL n,m,k;
LL a[MS];
LL fr[MS]; // 记录第一次出现的位置
LL aft[MS]; // 记录后一个相同值出现的位置
LL v[MS];
LL p[MS];
struct node{
int l,r;
int id;
}ask[MS];
LL ac[MS];
bool cmp(node t1,node t2){
return t1.l < t2.l;
}
LL lowbit(LL x){
return x&(-x);
}
void add(int pos,LL val){
for(;pos<=n;pos+=lowbit(pos)) p[pos] += val;
}
LL query(int pos){
LL ans = 0;
for(;pos;pos-=lowbit(pos)) ans += p[pos];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
for(int i=1;i<=n;i++){
cin >> a[i];
if(!fr[a[i]]) fr[a[i]] = i;
}
for(int i=1;i<=m;i++){
v[i] = n+1;
}
for(int i=n;i>=1;i--){
aft[i] = v[a[i]];
v[a[i]] = i;
}
aft[n+1] = n+1;
for(int i=1;i<=m;i++){
if(fr[i]){
add(aft[fr[i]],1);
}
}
for(int i=1;i<=k;i++){
int l,r;
cin >> l >> r;
ask[i] = {l,r,i};
}
sort(ask+1,ask+k+1,cmp);
for(int i=1,L=1;i<=k;i++){
while(L < ask[i].l){
add(aft[L],-1);
add(aft[aft[L]],1);
L++;
}
LL ans = query(ask[i].r);
ac[ask[i].id] = ans;
}
for(int i=1;i<=k;i++){
cout << ac[i] << "\n";
}
cout << "\n";
return 0;
}
动态开点线段树
额...复习一下;吸氧过
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stdio.h>
using namespace std;
#define ls rt<<1
#define rs rt<<1|1
#define LL int
#define PI acos(-1.0)
#define eps 1e-8
#define Pair pair<double,double>
// notice
#define mod 998244353
#define MAXN 2e6
#define MS 2000009
LL n,m,k;
LL a[MS];
LL fr[MS];
LL aft[MS];
LL v[MS];
struct node{
int l,r;
int id;
}ask[MS];
struct nod{
int l,r;
LL val;
}p[MS<<5];
int tot;
int root;
LL ac[MS];
bool cmp(node t1,node t2){
return t1.l < t2.l;
}
void push_up(int &rt){
if(!p[rt].l && !p[rt].r){
rt = 0;
}
else if(!p[rt].l){
p[rt].val = p[p[rt].r].val;
}
else if(!p[rt].r){
p[rt].val = p[p[rt].l].val;
}
else{
p[rt].val = p[p[rt].l].val + p[p[rt].r].val;
}
}
void update(int &rt,int pos,int l,int r,LL val){
if(!rt) rt = ++tot;
if(l == r){
p[rt].val = val;
if(!p[rt].val) rt = 0;
return;
}
int m = l+r>>1;
if(m >= pos) update(p[rt].l,pos,l,m,val);
else update(p[rt].r,pos,m+1,r,val);
push_up(rt);
}
LL query(int rt,int L,int R,int l,int r){
if(!rt) return 0;
if(L <= l && r <= R){
return p[rt].val;
}
int m = l+r>>1;
LL ans = 0;
if(m >= L) ans += query(p[rt].l,L,R,l,m);
if(m < R) ans += query(p[rt].r,L,R,m+1,r);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
for(int i=1;i<=n;i++){
cin >> a[i];
v[i] = n+1;
if(!fr[a[i]]) fr[a[i]] = i;
}
for(int i=n;i>=1;i--){
aft[i] = v[a[i]];
v[a[i]] = i;
}
aft[n+1] = n+1;
for(int i=1;i<=m;i++){
if(fr[i] && aft[fr[i]] != n+1){
update(root,aft[fr[i]],1,MAXN,1);
}
}
for(int i=1;i<=k;i++){
int l,r;
cin >> l >> r;
ask[i] = {l,r,i};
}
sort(ask+1,ask+k+1,cmp);
for(int i=1,L=1;i<=k;i++){
while(L < ask[i].l){
if(aft[L] != n+1){
update(root,aft[L],1,MAXN,0);
if(aft[aft[L]] != n+1){
update(root,aft[aft[L]],1,MAXN,1);
}
}
L++;
}
LL ans = query(root,ask[i].l,ask[i].r,1,MAXN);
ac[ask[i].id] = ans;
}
for(int i=1;i<=k;i++){
cout << ac[i] << "\n";
}
cout << "\n";
return 0;
}