Destroying Array CodeForces
来源:互联网 发布:最近网络疯传的名校 编辑:程序博客网 时间:2024/05/31 19:13
点击打开链接
逆序考虑 这样问题就转换为每插入一个数 查询当前所有连续区间的最大值
插入时看左右是否有相邻的其他区间 有就合并
#include <bits/stdc++.h>using namespace std;#define ll long longll num[100010],sum[100010],ans[100010];int pre[100010],f[100010];int n;int getf(int p){ if(f[p]==p) return p; else { f[p]=getf(f[p]); return f[p]; }}int main(){ ll maxx; int i,p,fp; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%lld",&num[i]); } for(i=1;i<=n;i++) { scanf("%d",&pre[i]); } memset(f,-1,sizeof(f)); memset(sum,0,sizeof(sum)); maxx=0; for(i=n;i>=2;i--) { p=pre[i]; f[p]=p; sum[p]=num[p]; if(f[p-1]!=-1) { fp=getf(p-1); f[fp]=p; sum[p]+=sum[fp]; } if(f[p+1]!=-1) { fp=getf(p+1); f[fp]=p; sum[p]+=sum[fp]; } maxx=max(maxx,sum[p]); ans[i]=maxx; } for(i=2;i<=n;i++) { printf("%lld\n",ans[i]); } printf("0\n"); } return 0;}
阅读全文
0 0
- Destroying Array CodeForces
- codeforces 722C Destroying Array
- codeforces(722c)-Destroying Array
- codeforces-772C-Destroying Array
- Codeforces 722C. Destroying Array
- codeforces 722C. Destroying Array
- 【37.38%】【codeforces 722C】Destroying Array
- codeforces Destroying Array 线段树维护区间
- codeforces 722C Destroying Array 【线段树好题】
- codeforces 722C. Destroying Array map+multiset
- Codeforces 722C - Destroying Array By Assassin
- CodeForces - 722C Destroying Array (set)
- Codeforces #722C: Destroying Array 题解
- codeforces 722C Destroying Array 并查集带权
- Destroying Array
- Codeforces Round 722C:Destroying Array(离线)
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- [codeforces]C. Destroying Array——并查集
- 进程的创建-Process子类(python 版)
- 在vue项目中 如何定义全局变量 全局函数
- form表单
- 1024程序员节
- 复制单链表成为新的链表,然后return the number of items in the new list
- Destroying Array CodeForces
- React Native 模仿网易云音乐手机客户端,兼容安卓和IOS两个平台
- form表单样式
- Android lanunmode
- Tableau&BDP,哪个才是最适合中国用户的数据可视化分析工具?
- 从SVN上检出Maven项目报红叉错误,package错误 does not match the expected package "main.java.com.*
- ubuntu下C++程序的编译和运行
- Java中的String
- POJ系列——java