// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define py cout << "YES" << endl;
#define pn cout << "NO" << endl;
#define pb push_back
#define int long long
typedef long double lld;
#define double lld
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 2e5;
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
// Check for queue, priorioty_queue, stack, ordered_set solutions
// stack => LIFO (whatever goes in last comes out last)
// queue => FIFO (whatever goes in first comes out first)
// priority_queue => Dynamic queries of minimum/maximum
// ordered_set => set in vector form
//[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element]
// To comment multiple lines : ctrl + /
// To find and replace : ctrl+H
void judge(){
srand(time(NULL));
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl;
#else
#define debug(x...)
#endif
}
void usaco(string s) {
freopen((s + ".in").c_str(),"r",stdin);
freopen((s + ".out").c_str(),"w",stdout);
}
int n,d,k;
set<int>s;
vector<int>dp;
vector<bool>vis;
vector<int>a,vals;
vector<int>adj,extra, prefix;
int curr_sum = 0;
int summation(int l, int r){
if(l>r){
return 0;
}
if(l==0){
return prefix[r];
}
return prefix[r] - prefix[l-1];
}
void build_adj(){
adj.resize(n+1), extra.resize(n);
vector<int>pref(n); pref[0] = 0;
for(int i = 1;i<n;i++) pref[i] = pref[i-1] + a[i-1];
for(int i = 1;i<n;i++){
auto it = upper_bound(pref.begin(),pref.end(),pref[i-1]+k);
adj[i-1] = it - pref.begin();
}
extra[0] = 0;
for(int i = 1;i<n;i++){
auto it = lower_bound(pref.begin(),pref.end(),pref[i]-k);
int idx = it - pref.begin();
extra[i] += summation(idx,i-1);
// for(int j = idx;j<i;j++){
// extra[i] += vals[j];
// }
}
adj[n-1] = n, adj[n] = n;
prefix.resize(n);
prefix[0] = vals[0];
for(int i = 1;i<n;i++) prefix[i] = prefix[i-1] + vals[i];
}
struct bin_node{
int vertex;
int dist = 0;
// The other stuff like max
bool operator==(const bin_node &b){
return vertex==b.vertex;
}
};
bin_node unite(bin_node a, bin_node b){ // Order is very imp
bin_node ans;
ans.vertex = b.vertex;
ans.dist = a.dist + b.dist;
return ans;
}
// Root is 0
class BinaryJump{
vector<vector<bin_node>>up; vector<int>depth; int n;
vector<int>par;
private:
vector<vector<bin_node>>build(vector<int>&par){
int n = par.size();
int mx = log2(n) + 2;
vector<vector<bin_node>>up(n,vector<bin_node>(mx));
//Base Case :
for(int i = 0;i<mx;i++){
up[n-1][i].vertex = -1;
}
for(int i = 0;i<n-1;i++){
up[i][0].vertex = par[i];
up[i][0].dist = summation(i,par[i]-1);
}
// Jumps
for(int j = 1;j<mx;j++){
for(int i = 1;i<n;i++){
bin_node parent = up[i][j-1];
if(parent.vertex==-1){
up[i][j].vertex = -1;
}
else{
up[i][j] = unite(parent,up[parent.vertex][j-1]);
}
}
}
return up;
}
bin_node kth(int node, int k){
bin_node ans;
ans.vertex = node;
while(true){
if(k==0 or ans.vertex==-1){
return ans;
}
int p = log2(k);
ans = unite(ans,up[ans.vertex][p]);
k -= (1<<p);
}
}
public:
BinaryJump(vector<int>&parent){
par = parent;
n = par.size();
up = build(par);
}
bin_node kthAncestor(int node, int k){
return kth(node,k);
}
};
void final_answer(){
int best = 0;
for(int i = 1;i<n;i++){
if(dp[i]>dp[best]){
best = i;
}
}
vector<int>answer;
while(d--){
if(best==n){
break;
}
answer.pb(best);
best = adj[best];
}
cout << answer.size() << endl;
for(auto x : answer){
cout << x + 1 << ' ';
}
}
signed main(){
fastio; //judge();
cin >> n >> d >> k;
a.resize(n-1), vals.resize(n+1), vis.resize(n,false), dp.resize(n,0);
extra.resize(n);
for(auto &x : a) cin >> x;
for(auto &x : vals) cin >> x;
build_adj();
BinaryJump binjump(adj);
for(int i=0;i<n;i++){
dp[i] = binjump.kthAncestor(i,d).dist + extra[i];
}
final_answer();
}
컴파일 시 표준 에러 (stderr) 메시지
SolarStorm.cpp: In function 'void judge()':
SolarStorm.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("1.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("2.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen("Error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp: In function 'void usaco(std::string)':
SolarStorm.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen((s + ".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen((s + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |