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>
#include<unordered_map>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//const long long inf = 1e15;
using namespace std;
/**
* Author: Simon Lindholm
* Date: 2016-10-08
* License: CC0
* Source: me
* Description: Segment tree with ability to add or set values of large intervals, and compute max of intervals.
* Can be changed to other things.
* Use with a bump allocator for better performance, and SmallPtr or implicit indices to save memory.
* Time: O(\log N).
* Usage: Node* tr = new Node(v, 0, sz(v));
* Status: stress-tested a bit
*/
const int inf = 1e9;
struct Node {
Node* l = 0, * r = 0;
ull lo, hi, mset = inf, madd = 1, val = 0, val0 = 0;
Node(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Node(vector<ull>& v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Node(v, lo, mid); r = new Node(v, mid, hi);
val = l->val + r->val;
val0 = l->val + r->val;
}
else val = val0 = v[lo];
}
ull query(int L, int R) {
if (R <= lo || hi <= L) return 0;
if (L <= lo && hi <= R) return val;
push();
return l->query(L, R) + r->query(L, R);
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val = x, madd = 1;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = l->val + r->val;
}
}
void add(int L, int R, ull x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
madd = 1;
val = val0;
if (mset != inf) mset += x;
else madd *= x;
val *= x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = l->val + r->val;
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Node(lo, mid); r = new Node(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd != 1)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 1;
}
};
int main() {
cin.sync_with_stdio(false);
int n,d;
cin >> n >> d;
vector<pair<ll, int>> v(n);
rep(i, 0, n) {
cin >> v[i].first;
v[i].second = -i;
}
sort(all(v));
vector<ll> dp(n,-inf);
rep(i, 0, n) {
dp[-v[i].second] = 1;
rrep(j, -v[i].second - 1, max(-1, -v[i].second - 1 - d)) {
dp[-v[i].second] = max(dp[-v[i].second], dp[j] + 1);
}
}
ll ans = 1;
trav(a, dp)ans = max(ans, a);
cout << ans << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In member function 'ull Node::query(int, int)':
Main.cpp:46:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
46 | if (R <= lo || hi <= L) return 0;
| ~~^~~~~
Main.cpp:46:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
46 | if (R <= lo || hi <= L) return 0;
| ~~~^~~~
Main.cpp:47:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
47 | if (L <= lo && hi <= R) return val;
| ~~^~~~~
Main.cpp:47:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
47 | if (L <= lo && hi <= R) return val;
| ~~~^~~~
Main.cpp: In member function 'void Node::set(int, int, int)':
Main.cpp:52:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
52 | if (R <= lo || hi <= L) return;
| ~~^~~~~
Main.cpp:52:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
52 | if (R <= lo || hi <= L) return;
| ~~~^~~~
Main.cpp:53:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
53 | if (L <= lo && hi <= R) mset = val = x, madd = 1;
| ~~^~~~~
Main.cpp:53:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
53 | if (L <= lo && hi <= R) mset = val = x, madd = 1;
| ~~~^~~~
Main.cpp: In member function 'void Node::add(int, int, ull)':
Main.cpp:61:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
61 | if (R <= lo || hi <= L) return;
| ~~^~~~~
Main.cpp:61:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
61 | if (R <= lo || hi <= L) return;
| ~~~^~~~
Main.cpp:62:9: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
62 | if (L <= lo && hi <= R) {
| ~~^~~~~
Main.cpp:62:21: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
62 | if (L <= lo && hi <= R) {
| ~~~^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |