当前位置:网站首页>[luogu] p1083 [noip2012 improvement group] classroom borrowing (difference)
[luogu] p1083 [noip2012 improvement group] classroom borrowing (difference)
2022-06-23 01:23:00 【Lupin123123】
I started with a line segment tree , Later I heard that I could score two points + Difference ?! Line segment tree solution
Speaking of difference is really annoying , Today, I spent a lot of time on the problem of string counting for score checking . About difference
Define differential array dif[],dif[i]=a[i]-a[i-1]. If you want to [L,R] Add... To each number k And you need a single point of inquiry , as long as dif[L]+=k,dif[R+1]-=k. Single point query pair dif Array summation implementation , Time complexity O ( n ) O(n) O(n)
To make a long story short , Difference can be achieved O ( 1 ) O(1) O(1) Interval addition and subtraction , O ( n ) O(n) O(n) Single point query .
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,l,r,ans;
int a[maxn],dif[maxn],s[maxn],e[maxn],d[maxn];
int check(int x)
{
memset(dif,0,sizeof(dif));
for (int i=1; i<=n; i++) dif[i]=a[i]-a[i-1];
for (int i=1; i<=x; i++)
{
dif[s[i]]-=d[i];
dif[e[i]+1]+=d[i];
}
int sum=0;
for (int i=1; i<=n; i++)
{
sum+=dif[i];
if (sum<0) return 1;
}
return 0;
}
int main()
{
FAST;
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=m; i++) cin>>d[i]>>s[i]>>e[i];
l=0, r=n+1;
int flag=1;
while(l<=r)
{
int mid=(l+r)/2;
if (check(mid))
{
flag=0;
ans=mid;
r=mid-1;
}
else l=mid+1;
}
if (flag==0)
{
cout<<-1<<endl;
cout<<ans<<endl;
}
else cout<<0<<endl;
return 0;
}
边栏推荐
- JS to determine whether the browser has opened the console
- C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
- LINQ query
- MySQL-Seconds_ behind_ Master accuracy error
- Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
- 最安全的现货白银的心理分析
- Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
- I've been outsourcing for four years, but I feel it's useless
- MGRE环境下的OSPF实验
- The longest child sequence of the 2019 Blue Bridge Cup
猜你喜欢

JMeter associated login 302 type interface

Psychological analysis of the safest spot Silver

Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times

How to set the power-off auto start of easycvr hardware box

Cadence spb17.4 - Allegro - optimiser la spécification de l'angle de connexion de la polyligne pour une seule ligne électrique - polyligne à arc

SQL programming task05 job -sql advanced processing

How to solve the problem that easycvr does not display the interface when RTMP streaming is used?

Autumn move script C

LeetCode 206. 反转链表(迭代+递归)

贵金属现货白银如何呢?
随机推荐
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
New progress in the construction of meituan's Flink based real-time data warehouse platform
Flink synchronizes MySQL data to es
07 project cost management
How about precious metal spot silver?
Does qiniu school belong to a securities company? Is it safe to open an account?
[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION
Is it safe for CICC securities to open an account? What is its relationship with CICC?
MGRE环境下的OSPF实验
How to refine permissions to buttons?
You can also do NLP (classification)
Random decoding NLP
Pat class A - 1013 battle over cities
three. JS simulated driving tour art exhibition hall - creating super camera controller
Phantomjs Usage Summary
Local deployment and problem solving of IIS in ArcGIS JS 4.23
SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application
Vector 1 (classes and objects)
Unit of RMB in words
How to calculate the position of gold ETF