-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraq_rmq.cpp
More file actions
160 lines (147 loc) · 5.32 KB
/
Copy pathraq_rmq.cpp
File metadata and controls
160 lines (147 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// https://atcoder.jp/contests/dp/submissions/5033754
#ifdef DEBUG_IS_VALID
#define DEB 1
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back
#pragma GCC optimize ("-O3")
using namespace std;
#define DUMPOUT cout
#define dump(...) if(DEB) DUMPOUT<<" "<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl<<" "; if(DEB) dump_func(__VA_ARGS__)
template<typename T1,typename T2>ostream& operator << (ostream& os, pair<T1,T2> p){cout << "(" << p.first << ", " << p.second << ")"; return os;}
template<typename T>ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; }
template<typename T>ostream& operator << (ostream& os, set<T>& st){cout << "{"; for(auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr)!=st.end() ? ", " : ""); cout << "}"; return os;}
template<typename T1,typename T2>ostream& operator << (ostream& os, map<T1,T2> mp){cout << "{"; for(auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr)!=mp.end() ? "," : ""); cout << "}"; return os; }
void dump_func(){DUMPOUT << endl;}
template <class Head, class... Tail>void dump_func(Head&& head, Tail&&... tail){ DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...);}
template<class T> inline bool chmax(T& a,T const& b){if(a>=b) return false; a=b; return true;}
template<class T> inline bool chmin(T& a,T const& b){if(a<=b) return false; a=b; return true;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
const int INF = 1<<29;
const long long LINF=1LL<<59;
template <typename T, typename E>
struct SegmentTreeLazy {
using F = function<T(T,T)>;
using G = function<T(T,E)>;
using H = function<E(E,E)>;
using P = function<E(E,int)>;
F f; G g; H h; P p; T d1; E d0;
int n;
vector<T> dat;
vector<E> lazy;
SegmentTreeLazy(){}
SegmentTreeLazy(int n_, F f_, G g_, H h_,
T d1_, E d0_, P p_=[](E a, int b){return a;})
: f(f_), g(g_), h(h_), p(p_), d1(d1_), d0(d0_)
{
n = 1; while(n < n_) n *= 2;
dat.assign(n*2-1, d1);
lazy.assign(n*2-1, d0);
}
void build(vector<T> v) {
rep(i, v.size()) dat[i+n-1] = v[i];
for(int i=n-2; i>=0; --i) dat[i] = f(dat[i*2+1], dat[i*2+2]);
}
// 区間の幅がlenの節点kについて遅延評価
inline void eval(int len, int k) {
if(lazy[k] == d0) return;
if(k*2+1 < n*2-1) {
lazy[2*k+1] = h(lazy[k*2+1], lazy[k]);
lazy[2*k+2] = h(lazy[k*2+2], lazy[k]);
}
dat[k] = g(dat[k],p(lazy[k],len));
lazy[k] = d0;
}
// [a, b)
T update(int a, int b, E x, int k, int l, int r) {
eval(r-l, k);
if(b <= l || r <= a) return dat[k];
if(a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
return g(dat[k], p(lazy[k],r-l));
}
return dat[k] = f(update(a, b, x, 2*k+1, l, (l+r)/2),
update(a, b, x, 2*k+2, (l+r)/2, r));
}
T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
// [a, b)
T query(int a, int b, int k, int l, int r) {
eval(r-l, k);
if(a <= l && r <= b) return dat[k];
bool left = !((l+r)/2 <= a || b <= l), right = !(r <= 1 || b <= (l+r)/2);
if(left&&right) return f(query(a, b, 2*k+1, l, (l+r)/2), query(a, b, 2*k+2, (l+r)/2, r));
if(left) return query(a, b, 2*k+1, l, (l+r)/2);
return query(a, b, 2*k+2, (l+r)/2, r);
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
// デバッグ出力
void debug() {
cout << "---------------------" << endl;
int cnt = 0;
for(int i=1; i<=n; i*=2) {
rep(j, i) {
cout << "(" << dat[cnt] << "," << lazy[cnt] << ") ";
cnt++;
}
cout << endl;
}
cout << "---------------------" << endl;
}
};
/**
* 区間更新区間max d1=d0=INT_MAX f=max(a,b) g=h=(b==INT_MAX?a:b)\n
* 区間加算区間和 d1=d0=0 f=g=h=a+b p=a*b\n
* 区間加算区間min d1=d0=0 f=min(a,b) g=h=a+b\n
* 区間更新区間和 d1=d0=0 f=a+b g=h=(b==0?a:b) p=a*b\n
* 区間xor区間和 d1=d0=0 f=a+b g=(b>=1?b-a:a) h=a^b p=a*b
*/
void solve(long long N, long long M, std::vector<long long> l, std::vector<long long> r, std::vector<long long> a){
auto f = [](ll lhs,ll rhs){return max(lhs,rhs);};
auto g = [](ll lhs,ll rhs){return lhs+rhs;};
SegmentTreeLazy<ll,ll> dp(N+2,f,g,g,0LL,0LL);
vector<vector<ll>> lr(N);
rep(i,M){
lr[r[i]-1].pb(i);
}
rep(i,N){
ll ma = 0;
if(i) chmax(ma,dp.query(0,i));
dp.update(i,i+1,ma);
for(auto j:lr[i]){
dp.update(l[j]-1,i+1,a[j]);
}
}
cout << max(dp.query(0,N+1),0LL) << endl;
}
int main(){
long long N;
scanf("%lld",&N);
long long M;
scanf("%lld",&M);
std::vector<long long> l(M);
std::vector<long long> r(M);
std::vector<long long> a(M);
for(int i = 0 ; i < M ; i++){
scanf("%lld",&l[i]);
scanf("%lld",&r[i]);
scanf("%lld",&a[i]);
}
solve(N, M, std::move(l), std::move(r), std::move(a));
return 0;
}