当前位置:网站首页>Bzoj 3747 poi2015 kinoman segment tree
Bzoj 3747 poi2015 kinoman segment tree
2022-07-05 19:58:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
Title Effect : Yes m spot , Each point has a weight . Now we have this m Is the length of the point n This sequence , Seek the interval , It only appears once in the correct point interval and the maximum
I thought for a long time. , Even God's title , Ben said it was a water problem …… I'm drunk
Enumerate left points For each request, right click Tree maintenance maximum
Consider the contribution of each number to the answer Record an array next Indicates the position of the next occurrence of the point on this position Then the scope of contribution of this point is [i,next[i]-1] Suppose there is no next Namely [i,n]
So we first add the contribution of all the first numbers to the answer to the line segment tree Then scan it from left to right After counting the answers every time i The contribution to the answer is removed And then put next[i] The contribution to the answer increases the segment tree
I'm also drunk with this constant …… Second to last in speed The positive solution must not be like this ……
Besides POI2015 I'm wearing the wrong age ?
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 1001001
using namespace std;
struct Segtree{
Segtree *ls,*rs;
long long num,mark;
void Build_Tree(int x,int y);
void Update(int x,int y,int l,int r,long long val);
long long Get_Ans(int x,int y,int l,int r);
}*root=new Segtree,mempool[M<<1],*C=mempool;
int n,m;
int a[M],w[M],next[M],last[M];
bool v[M];
long long ans;
void Segtree :: Build_Tree(int x,int y)
{
int mid=x+y>>1;
num=0;mark=0;
if(x==y) return ;
ls=C++;rs=C++;
ls->Build_Tree(x,mid);
rs->Build_Tree(mid+1,y);
}
void Segtree :: Update(int x,int y,int l,int r,long long val)
{
int mid=x+y>>1;
if(x==l&&y==r)
{
num+=val;
mark+=val;
return ;
}
if(mark)
{
ls->num+=mark;
rs->num+=mark;
ls->mark+=mark;
rs->mark+=mark;
mark=0;
}
if(r<=mid) ls->Update(x,mid,l,r,val);
else if(l>mid) rs->Update(mid+1,y,l,r,val);
else ls->Update(x,mid,l,mid,val),rs->Update(mid+1,y,mid+1,r,val);
num=max(ls->num,rs->num);
}
long long Segtree :: Get_Ans(int x,int y,int l,int r)
{
int mid=x+y>>1;
if(x==l&&y==r)
return num;
if(mark)
{
ls->num+=mark;
rs->num+=mark;
ls->mark+=mark;
rs->mark+=mark;
mark=0;
}
if(r<=mid) return ls->Get_Ans(x,mid,l,r);
if(l> mid) return rs->Get_Ans(mid+1,y,l,r);
return max( ls->Get_Ans(x,mid,l,mid) , rs->Get_Ans(mid+1,y,mid+1,r) );
}
int main()
{
int i;
cin>>n>>m;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=m;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
{
if(last[a[i]])
next[last[a[i]]]=i;
else
v[i]=1;
last[a[i]]=i;
}
root->Build_Tree(1,n);
for(i=1;i<=n;i++)
if(v[i])
root->Update(1,n,i,next[i]?next[i]-1:n,w[a[i]]);
for(i=1;i<=n;i++)
{
ans=max(ans, root->Get_Ans(1,n,i,n) );
root->Update(1,n,i,next[i]?next[i]-1:n,-w[a[i]]);
if(next[i])
root->Update(1,n,next[i],next[next[i]]?next[next[i]]-1:n,w[a[next[i]]]);
}
cout<<ans<<endl;
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117735.html Link to the original text :https://javaforall.cn
边栏推荐
- Four methods of random number generation | random | math | threadlocalrandom | securityrandom
- sun.misc.BASE64Encoder报错解决方法[通俗易懂]
- Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
- 线程池参数及合理设置
- leetcode刷题:二叉树18(最大二叉树)
- 秋招字节面试官问你还有什么问题?其实你已经踩雷了
- Redis cluster simulated message queue
- 使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
- C#应用程序界面开发基础——窗体控制(5)——分组类控件
- 如何安全快速地从 Centos迁移到openEuler
猜你喜欢

leetcode刷题:二叉树18(最大二叉树)

Common - Hero Minesweeper

IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times

leetcode刷题:二叉树14(左叶子之和)

No matter how busy you are, you can't forget safety
Android interview classic, 2022 Android interview written examination summary
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer

C#应用程序界面开发基础——窗体控制(5)——分组类控件

Base du réseau neuronal de convolution d'apprentissage profond (CNN)

Webuploader file upload drag upload progress monitoring type control upload result monitoring control
随机推荐
Debezium series: parsing the default value character set
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Relationship between floating elements and parent and brother boxes
股票开户哪里好?网上客户经理开户安全吗
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
城链科技数字化创新战略峰会圆满召开
c——顺序结构
leetcode刷题:二叉树11(平衡二叉树)
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
中金财富在网上开户安全吗?
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
DP:树DP
Wildcard selector
C#应用程序界面开发基础——窗体控制(5)——分组类控件
1:引文;
Thread pool parameters and reasonable settings
[untitled]