Bzoj 3362 navigation nightmare with the right and check collection __bzoj

Source: Internet
Author: User
Tags abs

To find the distance between two points, given the position relationship between the points

This problem tyrants, but POJ also have a same problem, you can brush

Don't be subject to the problem, this problem does not force online, the query offline processing can be

And then there's the question of the right and the collection. Set weights to directional vectors, overload + and-, follow normal weights and check the set.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 40400
using namespace Std;
	struct abcd{int x,y;
	ABCD () {} abcd (int x,int Y): X (x), Y (y) {} ABCD operator + (const ABCD &y) const {return ABCD (x+y.x, Y+Y.Y);
	ABCD operator-(const ABCD &y) const {return ABCD (x-y.x, Y-Y.Y);
}}f[m];
	struct operation{int x,y;
ABCD temp;
}OPERATIONS[M];
	struct query{int x,y,z,pos;
	BOOL operator < (const query &y) Const {return Z < y.z;
}}queries[10100];
int n,m,q,fa[m],ans[10100]; int Distance (ABCD x) {return abs (x.x) +abs (x.y);} int find (int x) {if (!fa[x]| |
	FA[X]==X) return fa[x]=x;
	int y=fa[x];
	Fa[x]=find (Fa[x]);
	F[X]=F[Y]+F[X];
return fa[x];
	int main () {int i,j,x,y,z;
	Char p[10];
	cin>>n>>m;
		for (i=1;i<=m;i++) {scanf ("%d%d%d%s", &operations[i].x,&operations[i].y,&z,p);
			Switch (p[0]) {case ' E ': OPERATIONS[I].TEMP=ABCD (z,0); Case ' W ': OperatIONS[I].TEMP=ABCD (-z,0);
			Case ' N ': OPERATIONS[I].TEMP=ABCD (0,z);
		Case ' S ': OPERATIONS[I].TEMP=ABCD (0,-z);
	}} cin>>q;
	for (i=1;i<=q;i++) scanf ("%d%d%d", &queries[i].x,&queries[i].y,&queries[i].z), queries[i].pos=i;
	Sort (queries+1,queries+q+1);
			for (i=1,j=1;i<=q;i++) {for (; j<=queries[i].z;j++) {int x=operations[j].x;
			int y=operations[j].y;
			int Fx=find (x), Fy=find (y);
			FA[FY]=FX;
		F[fy]=f[x]-f[y]+operations[j].temp;
		int x=queries[i].x;
		int y=queries[i].y;
		if (find (x)!=find (y)) ans[queries[i].pos]=-1;
	else Ans[queries[i].pos]=distance (F[x]-f[y]);	
for (i=1;i<=q;i++) printf ("%d\n", Ans[i]);
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.