This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* WHY NOT ? */
// #pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define endl '\n'
#define mn first
#define mnc second
#define int long long
const int inf = -1e18;
const int N = 200005;
int arr[N]; /* 1 - based indexing */
int seg[4*N],lazy[4*N];
void build(int node,int ss,int se){
if(ss == se){
seg[node] = arr[ss];
return;
}
int mid = (ss + se) / 2;
build(2 * node,ss,mid);
build(2 * node + 1,mid + 1,se);
seg[node] = max(seg[2*node],seg[2*node + 1]);
}
void update(int node,int ss,int se,int l,int r,int val){
if(lazy[node] != 0){
int dx = lazy[node];
lazy[node] = 0;
seg[node] += dx;
if(se != ss){
lazy[2*node] += dx;
lazy[2*node + 1] += dx;
}
}
if(ss > r || se < l){
return;
}
if(ss >= l && se <= r){
seg[node] += val;
if(ss != se){
lazy[2*node] += val;
lazy[2*node + 1] += val;
}
return;
}
int mid = (ss + se) / 2;
update(2*node,ss,mid,l,r,val);
update(2*node+1,mid+1,se,l,r,val);
seg[node] = max(seg[2*node],seg[2*node + 1]);
}
int query_max(int node,int ss,int se,int l,int r){
if(lazy[node] != 0){
int dx = lazy[node];
lazy[node] = 0;
seg[node] += dx;
if(se != ss){
lazy[2*node] += dx;
lazy[2*node + 1] += dx;
}
}
if(ss > r || se < l) return inf;
if(ss >= l && se <= r) return seg[node];
int mid = (ss + se) / 2;
int lft = query_max(2*node,ss,mid,l,r);
int rgt = query_max(2*node + 1,mid+1,se,l,r);
return max(lft, rgt);
}
void solve(){
int n,m;
cin>>n>>m;
build(1,1,N);
vector<pair<int,int>> p(m);
vector<int> c(m,0);
for(int i=0;i<m;i++){
cin>>p[i].first>>p[i].second;
cin>>c[i];
update(1,1,N,p[i].first,p[i].second-1,c[i]);
}
vector<vector<int>> start(n + 1),end(n + 1);
for(int i=0;i<m;i++){
start[p[i].first].push_back(i);
end[p[i].second].push_back(i);
}
int ans = query_max(1,1,N,1,N);
for(int i=0;i<n;i++){
for(auto j : start[i]){
update(1,1,N,p[j].first,p[j].second-1,-c[j]);
update(1,1,N,0,p[j].first - 1,c[j]);
update(1,1,N,p[j].second,n-1,c[j]);
}
for(auto j:end[i]){
update(1,1,N,p[j].first,p[j].second-1,c[j]);
update(1,1,N,0,p[j].first-1,-c[j]);
update(1,1,N,p[j].second,n-1,-c[j]);
}
ans = max(ans,query_max(1,1,N,1,N));
}
cout<<ans<<endl;
}
signed main(){
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-6 << " miliseconds.\n";
return 0;
}
# | 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... |