[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=3362

[算法]

带权并查集

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = ; struct Que
{
int f1 , f2 , t;
int id;
} que[MAXN]; int n , m;
int f[MAXN] , dist[MAXN] , x[MAXN] , y[MAXN] , X[MAXN] , Y[MAXN] , d[MAXN];
int ans[MAXN];
char dir[MAXN][]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool cmp(Que a,Que b)
{
return a.t < b.t;
}
inline int get_root(int x)
{
if (f[x] == x) return x;
int fa = get_root(f[x]);
X[x] += X[f[x]];
Y[x] += Y[f[x]];
return f[x] = fa;
} int main()
{ scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++) f[i] = i;
for (int i = ; i <= m; i++) scanf("%d%d%d%s",&x[i],&y[i],&d[i],&dir[i]);
int q;
read(q);
for (int i = ; i <= q; i++)
{
scanf("%d%d%d",&que[i].f1 , &que[i].f2 , &que[i].t);
que[i].id = i;
}
sort(que + ,que + q + ,cmp);
int cur = ;
for (int i = ; i <= q; i++)
{
while (cur <= que[i].t)
{
int sx = get_root(x[cur]) , sy = get_root(y[cur]);
if (sx == sy) continue;
f[sx] = sy;
if (dir[cur][] == 'E')
{
X[sx] = -X[x[cur]] + X[y[cur]] - d[cur];
Y[sx] = -Y[x[cur]] + Y[y[cur]];
}
if ( dir[cur][] == 'W' )
{
X[sx] = -X[x[cur]] + X[y[cur]] + d[cur];
Y[sx] = -Y[x[cur]] + Y[y[cur]];
}
if (dir[cur][] == 'N')
{
Y[sx] = -Y[x[cur]] + Y[y[cur]] - d[cur];
X[sx] = -X[x[cur]] + X[y[cur]] ;
}
if (dir[cur][] == 'S')
{
Y[sx] = -Y[x[cur]] + Y[y[cur]] + d[cur];
X[sx] = -X[x[cur]] + X[y[cur]];
}
++cur;
}
if (get_root(que[i].f1) != get_root(que[i].f2)) ans[que[i].id] = -;
else ans[que[i].id] = abs(X[que[i].f1] - X[que[i].f2]) + abs(Y[que[i].f1] - Y[que[i].f2]);
}
for (int i = ; i <= q; i++) printf("%d\n",ans[i]); return ; }

最新文章

  1. bzoj3208--记忆化搜索
  2. jsp打印页面 js代码
  3. How to control printer orientation(Landscape / Portrait) for an AX report in X++
  4. about JNI
  5. angular $apply()以及$digest()讲解1
  6. 【leetcode】260. Single Number III
  7. UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-25: ordinal not in range(128)
  8. NAND Flash底层原理,SLC MLC TLC比较【转】
  9. 【python接口自动化-requests库】【一】requests库安装
  10. iOS app签名原理
  11. 配置vCenter Server Appliance 6.7
  12. [Android] 基于 Linux 命令行构建 Android 应用(一):关于 Android 项目
  13. 4. mysql 1449 : The user specified as a definer (&#39;test&#39;@&#39;%&#39;) does not exist 解决方法
  14. PTA (Advanced Level) 1009 Product of Polynomials
  15. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
  16. POJ2228 Naptime
  17. linux引导系统
  18. RAC安装重新运行root.sh
  19. boost 中文编码转换
  20. 【数据库_Mysql】JAVA-数据库Date格式在前台JSP页面的获取

热门文章

  1. 嵌套在ScrollView中的TextView控件可以自由滚动
  2. dispatching(bzoj 2008)
  3. 有向图欧拉回路个数 BEST定理
  4. canvas跟随页面滑动后准确定位到真实坐标
  5. curl post请求方式
  6. Node.js开发Web后台服务(转载)
  7. 【转载】Unix设计哲学 &amp; 回车换行八卦 &amp; EOF八卦 &amp; UNIX目录结构八卦
  8. 如何扩展ArcGIS中的元数据编辑器
  9. [经典面试题]在O(1)时间删除链表结点
  10. MaterialImageView