Codeforces 371D (Union-find)
来源:互联网 发布:c语言数组定义和输出 编辑:程序博客网 时间:2024/06/12 23:35
并查集做,满了的水池合并为为最后一个为父节点
#include <cstdio>#include <algorithm>#include <cstring>#include <cstdlib>#define maxn 200010int n;int next[maxn];int v[maxn];int lft[maxn];/*union-findsomewhere change to your need*/void GxdUnionFindInit(int* gnum,int glen){ for(int i=0;i<glen;i++) gnum[i]=i;}int GxdUnionFindSearch(int* gnum,int glen,int gkey){ int key = gkey; while(key!=next[key]) key = next[key]; int fa = key; //path compress key = gkey; while(key!=fa) { next[key] = fa; key = next[key]; } return fa;}void GxdUnionFindAdd(int* gnum,int glen,int ga,int gb){ int faa = GxdUnionFindSearch(gnum,glen,ga); int fab = GxdUnionFindSearch(gnum,glen,gb); if(faa==fab) return ; //here is a change to your need //the father is always bigger than the child here. next[std::min(faa,fab)] = std::max(faa,fab);}int main(){ memset(lft,0,sizeof(lft[0])); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&v[i]); lft[i] = v[i]; } GxdUnionFindInit(next,n+1); int m; scanf("%d",&m); while(m--) { int t; scanf("%d",&t); if(t==1) { int p,x; scanf("%d%d",&x,&p); int last = x; x=next[x]; while(p>=lft[x]&&x!=n) { p-=lft[x]; lft[x] = 0; GxdUnionFindAdd(next,n+1,x,x+1); x = next[x]; } lft[x]=std::max(lft[x]-p,0); GxdUnionFindAdd(next,n+1,last,x); }else { int k; scanf("%d",&k); if(lft[k]==0) printf("%d\n",v[k]); else printf("%d\n",v[k]-lft[k]); } } return 0;}
阅读全文
0 0
- Codeforces 371D (Union-find)
- CodeForces 612 D. The Union of k-Segments(水~)
- Almost Union-Find ()
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- codeforces 612D The Union of k-Segments (前缀和处理区间)
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- Union Find
- Union-Find
- Union-find
- Union-Find
- union find
- union-find
- codeforces D. The Union of k-Segments 排序
- Codeforces 612D The Union of k-Segments 【思维】
- Codeforces 612D The Union of k-Segments
- codeforces 612D The Union of k-Segments (sorting)
- Educational Codeforces Round 4 D. The Union of k-Segments
- CodeForces 612D The Union of k-Segments
- 快学Scala-单词计数程序、并行计算
- JXNU_Weekly_competition3题解
- 康托展开与逆康托展开(模板)
- Javaoop3
- Java读写文件
- Codeforces 371D (Union-find)
- TCP/IP协议族体系结构以及主要协议
- 持久化
- 选择某种Map集合保存学号从1到15的学员的学号(键)和姓名(值),学号用字符串表示,输入的时候要以学号乱序的方式存入Map集合,然后按照学号从大到小的顺序将Map集合中的元素输出打印。需要自定义Ma
- 实现用户在ListView上进行选择时出现向上或向下获取更多提示
- MYSQL——存储引擎
- SAP-MM-移动类型解析之收货01-收货冻结
- iOS 自动化单元测试--XCTest
- block和delegate的区别使用