Submission #1185208

#TimeUsernameProblemLanguageResultExecution timeMemory
1185208Zbyszek99Gift Exchange (JOI24_ho_t4)C++20
100 / 100
1008 ms35056 KiB
#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 query { int l,r,ind; bool operator<(const query& other) const { return r < other.r; } }; const int tree_siz = 1024*2048-1; int drzewo1[tree_siz+1]; int get_max1(int akt, int p1, int p2, int s1, int s2) { if(p2 < s1 || p1 > s2) return -1e9; if(p1 >= s1 && p2 <= s2) return drzewo1[akt]; return max(get_max1(akt*2,p1,(p1+p2)/2,s1,s2),get_max1(akt*2+1,(p1+p2)/2+1,p2,s1,s2)); } void upd1(int v) { drzewo1[v] = max(drzewo1[v*2],drzewo1[v*2+1]); if(v != 1) upd1(v/2); } void change1(int ind, int v) { drzewo1[tree_siz/2+ind+1] = v; upd1((tree_siz/2+1+ind)/2); } int drzewo2[tree_siz+1]; void set_max2(int akt, int p1, int p2, int s1, int s2, int w) { if(p2 < s1 || p1 > s2) return; if(p1 >= s1 && p2 <= s2) { drzewo2[akt] = max(drzewo2[akt],w); return; } set_max2(akt*2,p1,(p1+p2)/2,s1,s2,w); set_max2(akt*2+1,(p1+p2)/2+1,p2,s1,s2,w); } int get_max2(int v) { return max(drzewo2[v],(v != 1 ? get_max2(v/2) : 0)); } int A[500'001]; int B[500'001]; int nxt_ok[500'001][2]; void calc_nxt(int n, int type) { rep2(i,1,tree_siz) { drzewo1[i] = 0; drzewo2[i] = 0; } rep2(i,1,n) { int prv = max(get_max2(tree_siz/2+1+A[i]),get_max1(1,0,tree_siz/2,B[i],A[i])); if(type == 0) nxt_ok[i][0] = prv; else nxt_ok[n-i+1][1] = n-prv+1; change1(A[i],i); set_max2(1,0,tree_siz/2,B[i],A[i],i); } } bool query_ans[200'001]; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random_start(); int n; cin >> n; rep2(i,1,n) cin >> A[i]; rep2(i,1,n) cin >> B[i]; calc_nxt(n,0); reverse(A+1,A+n+1); reverse(B+1,B+n+1); calc_nxt(n,1); reverse(A+1,A+n+1); reverse(B+1,B+n+1); rep2(i,1,tree_siz) drzewo1[i] = 0; vector<pii> events; rep2(i,1,n) { events.pb({nxt_ok[i][1],i}); } sort(all(events)); rep2(i,1,n) change1(i,-nxt_ok[i][0]); vector<query> queries; int q; cin >> q; rep(qq,q) { int l,r; cin >> l >> r; queries.pb({l,r,qq}); } sort(all(queries)); int cur_e = 0; forall(it,queries) { while(cur_e < siz(events) && events[cur_e].ff <= it.r) { change1(events[cur_e].ss,-1e9); cur_e++; } query_ans[it.ind] = (-get_max1(1,0,tree_siz/2,it.l,it.r) >= it.l); } rep(qq,q) { if(query_ans[qq]) cout << "Yes\n"; else cout << "No\n"; } }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...