http://codeforces.com/problemset/problem/371/D

题意:给一个多层的漏斗,当一层满后会流向下一层,最后一层满后漏出;

n(<=2e5)层数,a[]每层大小,m询问次数,op=1表示添加下标为p的层k升水,op=2表示询问p层的水有多少;

思路:并查集,将满了的建边,查询就会快点;

C++:

#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<map>
#include<stack>
#include<string>
using namespace std;#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const ll mod=1e9+7;
const int maxn=2e6+100;int pre[maxn];
int n;
struct A
{int cap;int val;
}a[maxn];int Find(int x)
{int r=x;while(r!=pre[r]){r=pre[r];}int i=x;int j;while(pre[i]!=r){j=pre[i];pre[i]=r;i=j;}return r;
}void join(int x,int y)
{int jx=Find(x);int jy=Find(y);if(jx!=jy){if(jx>jy) pre[jy]=jx;else pre[jx]=jy;}
}void add(int p,int x)
{int r=Find(p);while(r<=n&&x>0){if(a[r].cap>=a[r].val+x){a[r].val+=x;x=0;}else{x-=(a[r].cap-a[r].val);a[r].val=a[r].cap;join(r,r+1);}r=Find(r);}
}
int main()
{sfi(n);for(int i=1;i<maxn;i++) pre[i]=i;//n的话连接n+1不行,会tlefor(int i=1;i<=n;i++){A tmp;sfi(tmp.cap);tmp.val=0;a[i]=tmp;}int m;sfi(m);while(m--){int op;sfi(op);if(op==1){int p;int x;sfi(p);sfi(x);add(p,x);}else{int p;sfi(p);printf("%d\n",a[p].val);}}return 0;
}

python:

虽然写了出来但tle8了,不知道怎么优化了,刚学不久;

类的类似c++数组创建搞炸我了,列表创建会是每个地址相同,每次改都全部改=-=;不定长输入也试了很多波;

总结:多尝试才是王道!

import mathmaxn = 2000009
"""""
def gcd(x,y):if y: return gcd(y,x%y)return xdef P():isp=[True]*(maxn+10)pri=[]for i in range(2,maxn):if isp[i]:pri.append(i)for j in range(i*i,maxn,i):isp[j]=Falsereturn pri
"""""class A:def __init__(self,x,y):self.cap=xself.val=ycnt=1
a={}pre=[0]*(maxn+20)##赋值0,不然无法从指定位置赋值
n=0def Find(x):r=xwhile r!=pre[r]:r=pre[r]i=xj=0while pre[i]!=r:j=pre[i]pre[i]=ri=jreturn int(r)def join(x,y):jx=Find(x)jy=Find(y)if jx!=jy:if jx>jy: pre[jy]=jxelse: pre[jx]=jydef add(p,x):r=Find(p)while r<=n and x>0:if a[r].cap>=a[r].val+x:a[r].val+=xx=0else :x-=(a[r].cap-a[r].val)a[r].val=a[r].capjoin(r,r+1)r=Find(r)n=int(input())for i in range(1,n+9):pre[i]=ik=input()
k=k.split()for i in range(len(k)):a[cnt]=A(int(k[i]),0)cnt+=1##for z in range(1,3):
##        print(z,a[z].cap,a[z].val,end=' ')
##        print()m=int(input())
while m:m-=1op=input()op=op.split()for i in range(len(op)):op[i]=int(op[i])if op[0]==1:p=op[1]x=op[2]add(p,x)else:p=op[1]print(int(a[p].val))

D. Vessels相关推荐

  1. 【CodeForces - 371D】Vessels(思维,元素合并,并查集)

    题干: There is a system of n vessels arranged one above the other as shown in the figure below. Assume ...

  2. 62——A hybrid deep segmentation network for fundus vessels viadeep-learning framework

    Yang L, Wang H, Zeng Q, et al. A hybrid deep segmentation network for fundus vessels via deep-learni ...

  3. CodeForces 371D. Vessels

    暴力+胡乱优化就过了..tags给的东西似乎什么都没用到.....CF的数据是不是有点水啊.....果然是没有营养的题目..... D. Vessels time limit per test 2 s ...

  4. 论文阅读——CcNet:A cross-connected convolutional network for segmenting retinal vessels using multi-scale

    论文阅读:CcNet:A cross-connected convolutional network for segmenting retinal vessels using multi-scale ...

  5. Codeforces I. Vessels(跳转标记)

    题目描述: Vessels time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Vessels CodeForces - 371D

    A - Vessels CodeForces - 371D 题意: 进行两种操作:1代表向x容器中倒入y升水 2代表  查询x容器中 的水为多少 将所有容器从下往上开始标号 1到  n,当要倒水的时候 ...

  7. 论文阅读笔记:CcNet: A cross-connected convolutional network for segmenting retinal vessels using 多尺度特征

    论文链接:CcNet: A cross-connected convolutional network for segmenting retinal vessels using multi-scale ...

  8. CodeForces - 371D. Vessels(并查集)

    题目链接:http://codeforces.com/problemset/problem/371/D点击打开链接 D. Vessels time limit per test 2 seconds m ...

  9. CodeForces - 371D Vessels 【并查集】

    Vessels 题意: 自上而下的n个碗,向某个碗中倒水,如果溢出,则会流向它之下的下一个未满的碗.有两种操作:1 p x表示往第p个碗中导入x的水,2 p表示询问此时第p个碗中的水量. 题解: 未经 ...

  10. USV(Unmanned Surface Vessels)研究概况和发展趋势

    USV研究概况和发展趋势 1. Guidance 优化算法:evolutionary algorithms(EA).genetic algorithm(GA) 缺点:难以实现实时优化计算 启发式算法: ...

最新文章

  1. 计算机应用 含升学方向,对口升学《计算机应用基础》复习资料总汇(含答案))讲述.doc...
  2. 纪念:2006年我在51CTO的第一帖
  3. 命名实体识别_用膨胀卷积进行命名实体识别 NER
  4. hashmap 判断key是否存在
  5. [watevrCTF 2019]Baby RLWE
  6. VAT code VAT NO.
  7. java集合系列之18 spring boot程序员的必修课
  8. 形变立体跟踪-基于稠密运动估计和力学仿真(1)
  9. python黑客攻防入门下载-Python键盘钩取的自我理解(来源于《Python黑客攻防入门》)...
  10. 十、探索性数据分析的图形化探索
  11. 同事用void把我给秀翻了!
  12. android12适配机型,OPPO率先适配安卓12版本
  13. RBM,DBM和DBN之间有什么区别?
  14. linux lnmp安装
  15. Julia: wsl ubuntu下安装、vscode及配置profile错误补正
  16. 阿里38号元老:管理要轻,文化要浓
  17. 阿里专家与你分享:你必须了解的Java多线程技术
  18. [Python嗯~机器学习]---用python3来分析和预测加州房价
  19. Camera2 闪光灯梳理
  20. 7. Mayavi入门

热门文章

  1. mysql,无法修改密码
  2. 前端人员必须了解的各种浏览器
  3. 仿新版支付宝账单页面滑动时月份栏被下一个月给顶上去
  4. viper4android 6.0系统,VIPER4Android最新版本
  5. DevOps运维开发一体化【超详细】
  6. ANSYS apdl命令流笔记8---载荷步与载荷子步的运用
  7. bitmap和canvas实现图层叠加(可实现灰色遮罩)
  8. 远距离大容量毫米波太赫兹通信系统设计—大气窗口选择
  9. 【微信小程序】冒泡事件与非冒泡事件、将文章数据从业务中分离、wxml的模块化
  10. 一键下载网页所有图片(含dataurl格式图片)