This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, M;
LL A[202020], B[202020], ans;
struct Line{
int l, r, c;
bool operator<(const Line &t)const{return l < t.l;}
} P[101010];
priority_queue<pii> PQ;
bool chk(int t, LL m, LL n){
int k=1;
LL x=0;
for (int i=1; i<=N; i++) B[i] = 0;
for (int i=1; i<=t; i++){
LL p=(n+A[i]-m+1)/2-x;
if (i==t) p=n-x;
while (P[k].l == i && k <= M){
if (P[k].r >= t) PQ.push((pii){P[k].r, P[k].c});
k++;
}
if (p>0) x+=p;
while (p>0){
if (PQ.empty()) return false;
pii T = PQ.top();
PQ.pop();
if (T.second > p){
B[T.first] += p;
T.second -= p, PQ.push(T);
break;
}
else{
B[T.first] += T.second;
p -= T.second;
}
}
}
while (k<=M) {
B[P[k].l]+=P[k].c, B[P[k].r]-=P[k].c;
k++;
}
while (!PQ.empty()){
B[t]+=PQ.top().second, B[PQ.top().first]-=PQ.top().second;
PQ.pop();
}
for (int i=t; i<=N; i++){
B[i] += B[i-1];
if (B[i] > m) return false;
}
return true;
}
int main(){
scanf("%d %d", &N, &M);
for (int i=1; i<=M; i++){
scanf("%d %d %d", &P[i].l, &P[i].r, &P[i].c);
if (P[i].l > P[i].r) swap(P[i].l, P[i].r);
A[P[i].l] += P[i].c, A[P[i].r] -= P[i].c;
}
sort(P+1, P+M+1);
LL L=0, H=0;
int l, r;
for (int i=1; i<=N; i++){
A[i] += A[i-1];
if (H < A[i]) l=r=i, H=A[i];
if (H == A[i]) r=i;
}
while (L<=H){
int mid=L+H>>1;
if (chk(l, mid, A[l]-mid) || chk(l, mid, A[l]-mid+1) || chk(r, mid, A[r]-mid) || chk(r, mid, A[r]-mid+1)) H=mid-1, ans=mid;
else L = mid+1;
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
arranging_tickets.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &P[i].l, &P[i].r, &P[i].c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |