Submission #1202384

#TimeUsernameProblemLanguageResultExecution timeMemory
1202384Zbyszek99Distributing Candies (IOI21_candies)C++20
100 / 100
1772 ms32084 KiB
#include "candies.h" #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; struct node { ll max_ = 0; ll min_ = 0; ll spych = 0; node operator+(const node& other) { node res; res.max_ = max(max_,other.max_); res.min_ = min(min_,other.min_); return res; } void add(ll x) { max_ += x; min_ += x; spych += x; } }; const int tree_siz = 1024*512-1; node drzewo[tree_siz+1]; void spych(int v) { drzewo[v*2].add(drzewo[v].spych); drzewo[v*2+1].add(drzewo[v].spych); drzewo[v].spych = 0; } void add_seg(int akt, int l, int r, int l2, int r2, ll x) { if(l > r2 || r < l2) return; if(l >= l2 && r <= r2) { drzewo[akt].add(x); return; } spych(akt); add_seg(akt*2,l,(l+r)/2,l2,r2,x); add_seg(akt*2+1,(l+r)/2+1,r,l2,r2,x); drzewo[akt] = drzewo[akt*2] + drzewo[akt*2+1]; } node get_seg(int akt, int l, int r, int l2, int r2) { if(l > r2 || r < l2) return {(ll)-1e18,(ll)1e18,0LL}; if(l >= l2 && r <= r2) { return drzewo[akt]; } spych(akt); return get_seg(akt*2,l,(l+r)/2,l2,r2) + get_seg(akt*2+1,(l+r)/2+1,r,l2,r2); } vector<pll> events[200'001]; vi distribute_candies(vi c, vi l, vi r, vi v) { int n = c.size(); vi ansT(n); events[0].pb({0,1e9}); events[0].pb({1,-1e9}); rep(i,siz(l)) { events[l[i]].pb({i+2,v[i]}); events[r[i]+1].pb({i+2,-v[i]}); } rep(i,n) { forall(it,events[i]) { add_seg(1,0,tree_siz/2,it.ff,siz(l)+2,it.ss); } int L = 0; int R = siz(l)+2; int ans = -1; while(L <= R) { int mid = (L+R)/2; node inf = get_seg(1,0,tree_siz/2,mid+1,siz(l)+2); if(inf.max_ - inf.min_ <= c[i]) { ans = mid; R = mid-1; } else { L = mid+1; } } ll end_val = get_seg(1,0,tree_siz/2,siz(l)+2,siz(l)+2).max_; ll my_val = get_seg(1,0,tree_siz/2,ans,ans).max_; node inf = get_seg(1,0,tree_siz/2,ans,siz(l)+2); if(my_val <= inf.min_) { ansT[i] = end_val - (inf.max_ - c[i]); } else { ansT[i] = end_val - inf.min_; } } return ansT; }
#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...