当前位置:网站首页>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
边栏推荐
- How to apply smart contracts more wisely in 2022?
- Common - Hero Minesweeper
- Is it safe for Anxin securities to open an account online?
- 安信证券在网上开户安全吗?
- 完爆面试官,一线互联网企业高级Android工程师面试题大全
- Android interview classic, 2022 Android interview written examination summary
- Flume series: interceptor filtering data
- Debezium series: PostgreSQL loads the correct last submission LSN from the offset
- Four methods of random number generation | random | math | threadlocalrandom | securityrandom
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
猜你喜欢
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer

【无标题】

leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)

leetcode刷题:二叉树12(二叉树的所有路径)

Go language | 02 for loop and the use of common functions

How about testing outsourcing companies?

Let's talk about threadlocalinsecurerandom

力扣 1200. 最小绝对差

Recommended collection, my Tencent Android interview experience sharing

S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
随机推荐
【c语言】快速排序的三种实现以及优化细节
leetcode刷题:二叉树13(相同的树)
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
leetcode刷题:二叉树12(二叉树的所有路径)
中金财富在网上开户安全吗?
建立自己的网站(16)
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
手机股票开户安全吗?靠不靠谱啊?
BZOJ 3747 POI2015 Kinoman 段树
leetcode刷题:二叉树15(找树左下角的值)
【obs】libobs-winrt :CreateDispatcherQueueController
Android interview classic, 2022 Android interview written examination summary
Multi branch structure
That's awesome. It's enough to read this article
95后阿里P7晒出工资单:狠补了这个,真香...
openh264解码数据流向分析
Analysis of openh264 decoded data flow
leetcode刷题:二叉树10(完全二叉树的节点个数)
SecureRandom那些事|真伪随机数