Submission #1144110

#TimeUsernameProblemLanguageResultExecution timeMemory
1144110agrim_09Solar Storm (NOI20_solarstorm)C++20
100 / 100
488 ms257780 KiB
// #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,s,k; 
vector<int>a;
vector<int>vals;
vector<int>nxt,extra;
vector<vector<int>>adj;
vector<int>pref,dp;
vector<int>par;

int summation(int l, int r){
    if(r==-1){
        r = n-1;
    }
    r = min(r,n-1);
    if(l>r){return 0;}
    if(l==0){return pref[r];}
    return pref[r] - pref[l-1];
}

void input(){
    cin >> n >> s >> k;
    a.resize(n); vals.resize(n); pref.resize(n); par.resize(n+1);
    par[n] = -1;
    a[0] = 0;
    for(int i = 1,diff;i<n;i++){
        cin >> diff; a[i] = a[i-1] + diff;
    }
    for(auto &x : vals) cin >> x;
    pref[0] = vals[0];
    for(int i = 1;i<n;i++) pref[i] = vals[i] + pref[i-1];
}

void build_adj(){
    nxt.resize(n+1); extra.resize(n); dp.resize(n);
    for(int i = 0;i<n;i++){
        auto it = upper_bound(a.begin(),a.end(),a[i]+k);
        nxt[i] = it - a.begin() - 1;
    }
    for(int i = 0;i<n;i++){
        auto it = lower_bound(a.begin(),a.end(),a[i]-k);
        extra[i] = it - a.begin();
    }

    adj.resize(n+1); nxt[n] = n;
    for(int u = 0;u<n;u++){
        int v = nxt[nxt[u]+1];
        adj[u].pb(v); adj[v].pb(u);
    }
}



struct bin_node {
	int vertex, jump, par, subSize;
    int depth = 0;
};

struct BinaryJump{
	vector<bin_node> info; int root;
 
	BinaryJump(const vector<vector<int>>& adj, int r) {
		info.resize(adj.size());
        root = r;
		build(root, root, adj);
	}
 
	void build(int node, int par, const vector<vector<int>>& adj) {
        if(node==root){
            info[root].vertex = root;
            info[root].depth = 0;
            info[root].par = info[root].jump = root;
            for(auto child : adj[node]) {
			    build(child, node, adj);
			    info[node].subSize += info[child].subSize;
		    }
            return;
        }
		info[node].vertex = node;
		info[node].depth = info[par].depth + 1;
        info[node].par = par;
        info[node].subSize = 1;
        
        int par2 = info[par].jump;
        if(info[par].depth - info[info[par].jump].depth==info[par2].depth - info[info[par2].jump].depth){
            info[node].jump = info[par2].jump;
        }
        else{
            info[node].jump = par;
        }
        
		for(auto child : adj[node]) {
            if(par==child){
                continue;
            }
			build(child, node, adj);
			info[node].subSize += info[child].subSize;
		}
	}
 
	int kthPar(int node, int k) const {
        int sum = 0; // of the dist
		while (true) {
            if(k==0 or node<0 or node==root){
                break;
            }
			if (info[node].depth - info[info[node].jump].depth <= k) {
				k -= info[node].depth - info[info[node].jump].depth;
				node = info[node].jump;
			} 
            else {
				k--;
				node = info[node].par;
			}
		}
		return node;
	}
};

void dfs(int node, int parent){
    for(auto child : adj[node]){
        if(child==parent){
            continue;
        }
        par[child] = node;
        dfs(child,node);
    }
}

void answer(){
    par[n] = -1;
    BinaryJump binjump(adj,n);
    int ans = 0, best = 0;
    for(int i = 0;i<n;i++){
        int v = binjump.kthPar(i,s-1);
        if(v==-1){
            v = n-1;
        }
        int p = summation(extra[i],nxt[v]);
        if(p>ans){
            ans = p, best = i;
        }
    }
    vector<int>q;
    while(s--){
        if(best==n){
            break;
        }
        q.pb(best);
        best = par[best];
    }
    cout << q.size() << endl;
    for(auto x : q){
        cout << x+1 << ' ';
    }
}

signed main(){
    fastio; //judge();
    input();
    build_adj();
    dfs(n,n);
    answer();

}

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...