# BZOJ 3236 AHOI 2013 作业 莫队算法

84sAC。。。这题卡的。。貌似莫队并非正解。

CODE(84816AC)：

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1100010
using namespace std;

int x,y,_id;
int block;
int a,b;

bool operator <(const Ask &a)const {
if(block == a.block)	return y < a.y;
return block < a.block;
}
scanf("%d%d%d%d",&x,&y,&a,&b);
_id = p;
}

pair<int,int *> xx[MAX << 1];
int src[MAX];

int t;
int fenwick[MAX << 1],_fenwick[MAX];
int num[MAX];
pair<int,int> ans[MAX];

inline int GetSum(int fenwick[],int x)
{
int re = 0;
for(; x; x -= x&-x)
re += fenwick[x];
return re;
}

inline void Fix(int fenwick[],int x,int c)
{
for(; x <= t; x += x&-x)
fenwick[x] += c;
}

int main()
{
int temp = 0;
for(int i = 1; i <= cnt; ++i)
scanf("%d",&xx[++temp].first),xx[temp].second = &src[i];
for(int i = 1; i <= asks; ++i) {
}
sort(xx + 1,xx + temp + 1);
for(int i = 1; i <= temp; ++i) {
if(!t || xx[i].first != xx[i - 1].first)
++t;
*xx[i].second = t;
}
int block = static_cast<int>(sqrt(cnt));
for(int i = 1; i <= asks; ++i)
int l = 1,r = 0;
for(int x = 1; x <= asks; ++x) {
++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1)	Fix(_fenwick,src[r],1);
}
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1)	Fix(_fenwick,src[l],1);
}
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]])	Fix(_fenwick,src[r],-1);
--r;
}
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]])	Fix(_fenwick,src[l],-1);
++l;
}
}
for(int i = 1; i <= asks; ++i)
printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}

CODE(TLE)：

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

inline int GetInt()
{
int res=0;
char t=getchar();
for (;t>'9'||t<'0';t=getchar());
for (;t>='0'&&t<='9';t=getchar()) res=res*10+t-'0';
return res;
}

int x,y,_id;
int a,b;

bool operator <(const Ask &a)const {
if(x == a.x)    return y < a.y;
return x < a.x;
}
x = GetInt();
y = GetInt();
a = GetInt();
b = GetInt();
}

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];

int src[MAX];

int y_x[MAX],t,gt,edges;
pair<int,int *> xx[MAX << 1];
int fenwick[MAX << 1],_fenwick[MAX];

int father[MAX];

int next[MAX << 1],aim[MAX << 1];

int num[MAX];
pair<int,int> ans[MAX];

{
return abs(a.x - b.x) + abs(a.y - b.y);
}

inline int GetPos(int x)
{
int re = 0;
for(; x <= t; x += x&-x)
re = fenwick[x];
return re;
}

inline void Fix(int x,int pos)
{
for(; x; x -= x&-x)
fenwick[x] = pos;
}

void MakeGraph()
{
for(int dir = 1; dir <= 4; ++dir) {
if(dir == 2 || dir == 4)
for(int i = 1; i <= asks; ++i)
if(dir == 3)
for(int i = 1; i <= asks; ++i)
for(int i = 1; i <= asks; ++i)
sort(xx + 1,xx + asks + 1);
t = 0;
for(int i = 1; i <= asks; ++i) {
if(!t || xx[i].first != xx[i - 1].first)
++t;
*xx[i].second = t;
}
memset(fenwick,0,sizeof(fenwick));
for(int i = asks; i; --i) {
int temp = GetPos(y_x[i]);
if(temp)
Fix(y_x[i],i);
}
}
for(int i = 1; i <= asks; ++i)
}

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

{
aim[total] = y;
}

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;
}
}
}

inline int GetSum(int fenwick[],int x)
{
int re = 0;
for(; x; x -= x&-x)
re += fenwick[x];
return re;
}

inline void Fix(int fenwick[],int x,int c)
{
for(; x <= gt; x += x&-x)
fenwick[x] += c;
}

void DFS(int x,int last)
{
static int l = 1,r = 0;

++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1)    Fix(_fenwick,src[r],1);
}
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1)    Fix(_fenwick,src[l],1);
}
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]])    Fix(_fenwick,src[r],-1);
--r;
}
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]])    Fix(_fenwick,src[l],-1);
++l;
}

for(int i = head[x]; i; i = next[i]) {
if(aim[i] == last)  continue;
DFS(aim[i],x);

++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1)    Fix(_fenwick,src[r],1);
}
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1)    Fix(_fenwick,src[l],1);
}
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]])    Fix(_fenwick,src[r],-1);
--r;
}
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]])    Fix(_fenwick,src[l],-1);
++l;
}
}
}

int main()
{
cnt = GetInt();
int temp = 0;
for(int i = 1; i <= cnt; ++i) {
xx[++temp].first = GetInt();
xx[temp].second = &src[i];
}
for(int i = 1; i <= asks; ++i) {
father[i] = i;
}
sort(xx + 1,xx + temp + 1);
for(int i = 1; i <= temp; ++i) {
if(!gt || xx[i].first != xx[i - 1].first)
++gt;
*xx[i].second = gt;
}
MakeGraph();
MST();
memset(fenwick,0,sizeof(fenwick));
DFS(1,0);
for(int i = 1; i <= asks; ++i)
printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}

posted @ 2016-01-07 09:33  mengfanrong  阅读(162)  评论(0编辑  收藏  举报