POJ 2827:Auto-Calculation Machine
来源:互联网 发布:成都铁路局网络考试 编辑:程序博客网 时间:2024/06/06 14:06
题型很普通,
写起来有一些细节要注意,
于是记录一下。
题目链接:http://poj.org/problem?id=2827
题意:
给定一个序列,
给出一些形如(i,j,sum)的询问,
代表元素i到元素j之间的闭区间元素的和为sum。
若此询问与之前的正确询问矛盾,
则此询问错误,输出正确值。
否则此询问正确。
算法:
对于属于同一集合的元素,
根节点是该集合中最小的元素。
d[x]维护的是(p[x],x]区间的元素和。
合并时需注意要保持根节点始终是最小值
trick:i可能大于j
代码如下:
#include<cstdio>#include<iostream>#include<algorithm>#include<sstream>#include<cstdlib>#include<cstring>#include<string>#include<climits>#include<cmath>#include<queue>#include<vector>#include<stack>#include<set>#include<map>#define INF 0x3f3f3f3f#define eps 1e-8using namespace std;const int MAXN=110000;int a[MAXN],b[MAXN],p[MAXN];long long c[MAXN],d[MAXN];vector<int> hash;long long findp(int x) { if(p[x]==-1) { return x; } int ret=findp(p[x]); d[x]+=d[p[x]]; return p[x]=ret;}int main() { int m; while(scanf("%d",&m)==1) { hash.clear(); memset(p,-1,sizeof(p)); memset(d,0,sizeof(d)); for(int i=0; i<m; i++) { scanf("%d%d%lld",&a[i],&b[i],&c[i]); if(a[i]>b[i]) { swap(a[i],b[i]); } hash.push_back(a[i]-1); hash.push_back(b[i]); } sort(hash.begin(),hash.end()); hash.erase(unique(hash.begin(),hash.end()),hash.end()); for(int i=0; i<m; i++) { a[i]=lower_bound(hash.begin(),hash.end(),a[i]-1)-hash.begin(); b[i]=lower_bound(hash.begin(),hash.end(),b[i])-hash.begin(); int px=findp(a[i]); int py=findp(b[i]); if(px==py) { if(d[b[i]]-d[a[i]]==c[i]) { puts("Accept"); } else { printf("Bug Detected %lld\n",d[b[i]]-d[a[i]]); } } else { if(px>py) { swap(a[i],b[i]); swap(px,py); c[i]=-c[i]; } d[py]=d[a[i]]+c[i]-d[b[i]]; p[py]=px; puts("Accept"); } } } return 0;}
- POJ 2827:Auto-Calculation Machine
- Calculation
- poj 1276 Cash Machine
- poj 1276 Cash Machine
- poj 1276Cash Machine
- POJ 1325 Machine Schedule
- POJ 1276 Cash Machine
- poj 1276 Cash machine
- POJ 1325 Machine Schedule
- poj 1276 Cash Machine
- poj 1276 Cash Machine
- poj Secret Milking Machine
- POJ 1325 Machine Schedule
- poj 1325 Machine Schedule
- Poj 1325 Machine Schedule
- POJ 1325-Machine Schedule
- POJ 1325 Machine Schedule
- POJ 1276 Cash Machine
- Prezi实战 ------ 一款颠覆性的做presentation的软件
- Java项目经验——程序员成长的关键
- JAVA--I/O流
- php+apache+mysql之间的关系
- 推荐近年来印象最深的几本书
- POJ 2827:Auto-Calculation Machine
- 数据结构算法总结-内排序
- NVGRE标准:在云中实现更多VLAN和独立租赁
- ubuntu 下安装的软件
- 从几个sample来学习Java堆,方法区,Java栈和本地方法栈
- Android中Webview使用自定义的javascript进行回调
- HBase 官方文档中文版
- 使用zend studio 配置 xdebug教程
- Mono--发布程序