首页 > 代码库 > BZOJ 2038 2009国家集训队 小Z的袜子 莫队算法

BZOJ 2038 2009国家集训队 小Z的袜子 莫队算法

题目大意:给出一些袜子的排列顺序,每次问一段区间中有多少相同颜色的袜子对。


思路:莫队算法真是一个神奇的算法。首先,暴力枚举是O(n^2)的时间复杂度,这肯定是不行的。假如区间是保证不重合的,那么就可以将总的时间转移的复杂度降到O(n)。很遗憾,题目中没有这个保证。于是乎,神秘的莫队就发明了一种神奇的算法。

对于每一个询问,我们将它看成一个平面上的点(x1,y1),同样的也就会有其他的点分布在平面中。假如还有一个点(x2,y2),那么我们从第一个区间转移到第二个区间需要改变的元素总数为|x1 - x2| + |y1 - y2|,也就是两点之间的曼哈顿距离。所以我们把所有的询问抽象成平面上的点,然后做曼哈顿距离最小生成树,之后便利这棵树,顺便就统计出了所有的答案了。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 50010
#define INF 0x3f3f3f3f
using namespace std;

struct Edge{
    int x,y,length;
    
    Edge(int _,int __,int ___):x(_),y(__),length(___) {}
    Edge() {}
    bool operator <(const Edge &a)const {
        return length < a.length;
    }
}edge[MAX << 3];
struct Point{
    int x,y,_id;
    
    bool operator <(const Point &a)const {
        if(x == a.x)	return y < a.y;
        return x < a.x;
    }
    void Read() {
        scanf("%d%d",&x,&y);
    }
}point[MAX];
struct Ask{
    int x,y;
}ask[MAX];

int t,points,edges;
int src[MAX];

pair<int,int *> xx[MAX];
int y_x[MAX];

int fenwick[MAX];
int father[MAX];

int head[MAX],total;
int _next[MAX << 1],aim[MAX << 1];

unsigned int cnt[MAX],now;
unsigned int ans[MAX],down[MAX];

inline void Add(int x,int y)
{
    _next[++total] = head[x];
    aim[total] = y;
    head[x] = total;
}

inline int CalcM(const Point &a,const Point &b)
{
    return abs(a.x - b.x) + abs(a.y - b.y);
}

inline int GetPos(int x)
{
    int re = 0;
    for(; x <= points; x += x&-x)
        if(point[fenwick[x]].x + point[fenwick[x]].y < point[re].x + point[re].y)
            re = fenwick[x];
    return re;
}

inline void Fix(int x,int pos)
{
    for(; x; x -= x&-x)
        if(point[fenwick[x]].x + point[fenwick[x]].y > point[pos].x + point[pos].y)
            fenwick[x] = pos;
}

void MakeGraph()
{
    for(int dir = 1; dir <= 4; ++dir) {
        if(dir != 1)
            for(int i = 1; i <= points; ++i)
                point[i].y *= -1;
        if(dir == 3)
            for(int i = 1; i <= points; ++i)
                swap(point[i].x,point[i].y);
        
        memset(fenwick,0,sizeof(fenwick));
        for(int i = 1; i <= points; ++i) {
            xx[i].first = point[i].y - point[i].x;
            xx[i].second = &y_x[i];
        }
        sort(xx + 1,xx + points + 1);
        int t = 0;
        xx[0].first = INF;
        for(int i = 1;i <= points; ++i) {
            if(xx[i].first != xx[i - 1].first)
                ++t;
            *xx[i].second = t;
        }
        sort(point + 1,point + points + 1);
        for(int i = points; i; --i) {
            int temp = GetPos(y_x[i]);
            if(temp)
                edge[++edges] = Edge(point[i]._id,point[temp]._id,CalcM(point[i],point[temp]));
            Fix(y_x[i],i);
        }
    }
}

int Find(int x)
{
    if(father[x] == x)	return x;
    return father[x] = Find(father[x]);
}

void MST()
{
    sort(edge + 1,edge + edges + 1);
    for(int i = 1; i <= edges; ++i) {
        int fx = Find(edge[i].x);
        int fy = Find(edge[i].y);
        if(fx != fy) {
            father[fx] = fy;
            Add(edge[i].x,edge[i].y);
            Add(edge[i].y,edge[i].x);
        }
    }
}

void DFS(int x,int last)
{
    static int l = 1,r = 0;
    while(r < ask[x].y)	now += cnt[src[++r]]++;
    while(l > ask[x].x)	now += cnt[src[--l]]++;
    while(r > ask[x].y)	now -= --cnt[src[r--]];
    while(l < ask[x].x)	now -= --cnt[src[l++]];
    
    down[x] = (unsigned int)(r - l + 1) * (r - l) >> 1;
    ans[x] = now;
    
    for(int i = head[x]; i; i = _next[i]) {
        if(aim[i] == last)	continue;
        DFS(aim[i],x);
        
        while(r < ask[x].y)	now += cnt[src[++r]]++;
        while(l > ask[x].x)	now += cnt[src[--l]]++;
        while(r > ask[x].y)	now -= --cnt[src[r--]];
        while(l < ask[x].x)	now -= --cnt[src[l++]];
    }
}

int main()
{
    cin >> t >> points;
    for(int i = 1; i <= t; ++i)
        scanf("%d",&src[i]);
    point[0].x = point[0].y = INF;
    for(int i = 1; i <= points; ++i) {
        point[i].Read();
        ask[i].x = point[i].x;
        ask[i].y = point[i].y;
        point[i]._id = i;
        father[i] = i;
    }
    MakeGraph();
    MST();
    DFS(1,0);
    for(int i = 1; i <= points; ++i) {
        unsigned int u = ans[i],d = down[i];
        if(!u)	puts("0/1");
        else {
            unsigned int gcd = __gcd(u,d);
            printf("%u/%u\n",u / gcd,d / gcd);
        }
    }
    return 0;
}


BZOJ 2038 2009国家集训队 小Z的袜子 莫队算法