BZOJ-2743: [HEOI2012]采花 前缀和 树状数组

BZOJ-2743

LUOGU:https://www.luogu.org/problemnew/show/P4113

题意:

  给一个n长度的序列,m次询问区间,问区间中出现两次及以上的数字的个数。n,m,c为2e6。

思路:

  一开始用莫队写,但是tle了,莫队的复杂度是n的1.5次这道题n是2e6,复杂度逼近1e9。所以要用更加巧妙的方法,利用前缀和和树状数组维护即可。这道题不是简单的维护前缀和。因为题意说区间中要满足这个种类的物品大于两个的时候,才能加一。所以这一次离开区间的物品,影响的是最接近这个物品的同类物品(在区间中减去1),和次接近这个物品的同类物品(在区间中加1)。因此要预处理每个下标对应的下一个下标。离线处理每一个询问,按区间左端点从小到大排序。然后用一个id值从小到大遍历。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <iomanip>
#include   <cstdlib>
#include    <cstdio>
#include    <string>
#include    <vector>
#include    <bitset>
#include    <cctype>
#include     <queue>
#include     <cmath>
#include      <list>
#include       <map>
#include       <set>
//#include <unordered_map>
//#include <unordered_set>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
//__gnu_pbds::cc_hash_table<int,int>ret[11];    //这是很快的hash_map
#define fi first
#define se second
//#define endl '
'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18

const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

/*-----------------------showtime----------------------*/
            const int maxn = 2e6+9;
            int a[maxn],sum[maxn],nx[maxn],x[maxn],ans[maxn],cnt[maxn];
            int n,c,m;
            struct node
            {
                int l,r,id;                
            }q[maxn];
            bool cmp(node a,node b){
                if(a.l == b.l)
                    return a.r < b.r;
                return a.l < b.l;
            }
            int lowbit(int x){
                return x & (-x);
            }
            void add(int x,int c){
                while(x <= n){
                    sum[x] += c;
                    x+=lowbit(x);
                }
            }
            int query(int x){
                int res = 0;
                while(x > 0){
                    res += sum[x];
                    x -= lowbit(x);
                }
                return res;
            }
int main(){
            scanf("%d%d%d", &n, &c, &m);
            for(int i=1; i<=n; i++) scanf("%d", &a[i]);

            for(int i=1; i<=n; i++){
                cnt[a[i]]++;
                if(cnt[a[i]] == 2)add(i,1);
            }
            for(int i=1; i<=m; i++){
                scanf("%d%d", &q[i].l, &q[i].r);
                q[i].id = i;
            }

            for(int i=n; i>=1; i--){
                nx[i] = x[a[i]];
                x[a[i]] = i;
            }

            sort(q+1,q+1+m,cmp);
            int le = 1;
            for(int i=1; i<=m; i++){
                while(le < q[i].l){
                    if(nx[le] > 0) add(nx[le],-1);
                    if(nx[nx[le]] > 0)add (nx[nx[le]], 1);
                    le++;    
                }
                ans[q[i].id] = query(q[i].r) - query(q[i].l-1);
            }
            for(int i=1; i<=m; i++){
                printf("%d
", ans[i]);
            }
            return 0;
}
BZOJ-2743
原文地址:https://www.cnblogs.com/ckxkexing/p/9538947.html