当前位置:网站首页>[luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
[luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
2022-06-23 01:23:00 【Lupin123123】
Minimum line segment tree template question .
Update intervals in order , Maintenance minimum . Minimum less than 0 I found the contradiction when I was . Complexity O ( m l o g n ) O(mlogn) O(mlogn)
Complete code :
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e6+5;
using namespace std;
int n,m;
ll a[maxn];
struct seg_tree
{
int l,r;
ll minn,lazy;
}tree[maxn<<2];
void pushup(int rt)
{
tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);
}
void build(int l, int r, int rt)
{
tree[rt].l=l, tree[rt].r=r;
if (l==r)
{
tree[rt].minn=a[l];
return;
}
int mid=(l+r)/2;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void pushdown(int rt)
{
if (tree[rt].lazy!=0)
{
tree[rt<<1].lazy+=tree[rt].lazy;
tree[rt<<1|1].lazy+=tree[rt].lazy;
tree[rt<<1].minn+=tree[rt].lazy;
tree[rt<<1|1].minn+=tree[rt].lazy;
tree[rt].lazy=0;
}
}
void add(int L, int R, ll val, int l, int r, int rt)
{
if (L<=l && R>=r)
{
tree[rt].minn+=val;
tree[rt].lazy+=val;
return;
}
int mid=(l+r)/2;
pushdown(rt);
if (L<=mid) add(L,R,val,l,mid,rt<<1);
if (R>mid) add(L,R,val,mid+1,r,rt<<1|1);
pushup(rt);
}
ll query(int L, int R, int l, int r, int rt)
{
if (L<=l && R>=r)
{
return tree[rt].minn;
}
pushdown(rt);
int mid=(l+r)/2;
ll ans=INF;
if (L<=mid) ans=min(ans,query(L,R,l,mid,rt<<1));
if (R>mid) ans=min(ans,query(L,R,mid+1,r,rt<<1|1));
return ans;
}
int main()
{
FAST;
int flag=1;
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>a[i];
build(1,n,1);
for (int i=1; i<=m; i++)
{
int x,l,r,ans=INF;
cin>>x>>l>>r;
add(l,r,-x,1,n,1);
ans=query(l,r,1,n,1);
if (ans<0)
{
flag=0;
cout<<-1<<endl;
cout<<i<<endl;
break;
}
}
if (flag==1) cout<<0<<endl;
return 0;
}
边栏推荐
- Flink synchronizes MySQL data to es
- cadence SPB17.4 - 中文UI设置
- 关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
- Overview of visual object detection technology based on deep learning
- Development status of full color LED display
- [sliding window] leetcode992 Subarrays with K Different Integers
- It's still like this
- SQL programming task02 job - basic query and sorting
- Get the direction of mouse movement
- Explain the startup process of opengauss multithreading architecture in detail
猜你喜欢
![[initial launch] there are too many requests at once, and the database is in danger](/img/c1/807575e1340b8f8fe54197720ef575.png)
[initial launch] there are too many requests at once, and the database is in danger

OSPF comprehensive experiment

E-R diagram

Have you stepped on these pits? Use caution when creating indexes on time type columns

Explain the startup process of opengauss multithreading architecture in detail

62. different paths

SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications

MGRE环境下的OSPF实验

Installation record of ros1noetic in Win 11

Local deployment and problem solving of IIS in ArcGIS JS 4.23
随机推荐
Charles garbled code problem solving
graphite statsd接口数据格式说明
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Vscade personalization: let a cute girl knock the code with you
LINQ 查询
Cadence spb17.4 - Allegro - optimiser la spécification de l'angle de connexion de la polyligne pour une seule ligne électrique - polyligne à arc
Quelle est la structure et la façon dont les données sont stockées dans la base de données?
The road of architects starts from "storage selection"
BGP federal comprehensive experiment
Sélecteur de hiérarchie
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
cadence SPB17.4 - allegro - 优化指定单条电气线折线连接角度 - 折线转圆弧
Psychological analysis of the safest spot Silver
Hierarchy selector
Pat a - 1010 radical (thinking + two points)
62. different paths
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
OSPF综合实验
What is the storage structure and mode of data in the database?