/*
.:-=++***#***+=:.
.:-==+**##**%########***#####*=:.
.-+#%%#*****############**#******#####+=:
.-*%#####********###########*********#######*+-:
:+#######**********########*****+********##########+-.
:+##########**###******##********************###########+:
:+##########*****#*******###**********************###***####*:
.=###########***###*******#***************************#****#####*:
:*#################******************************************######=
-###################***************+****************************#####*:
.=###################******************************************#****#####+.
:*###################*******************************+************#*****#####:
=#############**######*****#*******************++***++++++***************#####=
.*###########******####*****#************+++*+**+++***++++++++***#**********#####+
-############********###*****%*+****+++++++++++++++++***++++++++***#*****#****#####*
=############********###+++++##++++++++++++++++*++++++****++++******##*+++*#****#####*.
.+#*##########*********###+++++%*++==++++++++++++*+++++*#************#*##*+++##****#####*.
.*#=-##########********###*++++*%*+++++++++++++*****+******#***********#*##*+++####*######*.
.:-: .*#: *#########*********###+++++##****++*******************#*#**********##*#*****####*#%###%*.
.--:++=+. .**. ##########********##*#++***%#****+*************=#*****#*#**********##*##*##*####**#%###%*:
:++===== .**. -###########******##*##+****%#*****#************-********#**********#***%####*###***#%######=:
.-=++= =*. +############****###*##*****%#*****%####*####**#:=#***#**#**********#=##*###**####***#%########+:
. .#. .####################*##*****%#####*%#****##****#.:#***#*#**********#%:##*+##***%###***############+:
-- -###################**#*****####****##****##*****..#*****#**********#*.*#+ +##**#####***##############+-.
-=+++-. = +###################**#*****###**********#*#***#: .***#*#***********#:.*#- :**#*######***#######%%%####%%#=:
.++====++ = ####################**##*****###***#+****#=#**** .+*###************+ ## +***######****##########+--+#%%%%+:
.---:=+======+: +. ##################%#***#*****####****#**#+-####. .+###***##*******# .#: :***########***##%%%####%#=. .:=*%%
---==++=======+= -= .%#################%****#*****######-#*##*.=%#*. +=---:::.......: =: =**#########***#%%%%%%%%%%%=. .-
:-:===========+= .#. :%################%%****#*****#####= :-+***#*=. .*#########%%#**#*=%%%%%%%%%%=.
.---=========+= :*. -#################%%****#*****%#+-.-*#%%%@###*= +##########%%%***:+-#%%%#####*:
.:=++++====+- =*.=###%############%@%****#*****#. =###%%%@@@+ ###########%%%%#*#: -*########=
..:::::-. +*+###%####%######%%+******#****#=##= -@@@@@@@+ =*###########%%%%##+ :*#######+
=%##+%####%######%+.******#*****%%#-:+@@@@%-=* +..###########%##%%%*. -#######
%##=#####%###%%#%:=******#*****%*.**##*+=: . -+**####*=: - .=###############%%*: .+#####
.%###%########%%%%:.#*****##*****= .+: . :-::.:--=++- : .-*##################+: :####
:##.*###########%@# #************+ .:.-==-- . +**###+*##############*: .*##
-%- ############%@@-#*###*#=##***#. :. =*++*#**++*##############*- .##
=# ############@@@######*#:###*##= :..+*++++***+++++*##############=.+#
+: ###########%@%%%%######.=###### .=+#+++++++***+++++****##############
.+ *##########%%%%%%######::######= =+++#*+++++++*#+++++******###########
+: =%#%######%%%##%@######:-+######. .:------== :*++++**+++++++*#*+++++********#######
-* :%%%%#####%%%##%%%#####***######* -=--------=: -*+++++++*++++++++*#**+++*=:=******+==*
.*+ +%%%%###%%####%%%#####**+#######= -........:=: -++++++++++*#**##***+******++- .-******=.
-*= *%%%%##%%%%%%%#%#####**++#######. :.........- .=#%%%######%##########%#+*******. .-++++*+
-*+ .*%%%%#%%#######%#####*++++######*: ......... :=#%@@%%%%%%#################*++*****=. .-++++
:**. :###%%%#%%########%####*++++*#######=-:.. -+#%%%###########################++++****#=. .-++
-*+ .######%%%%#########%###@@@%%%%######+...:----:. :*%################################%*+++++*+###- .+
:*+.=########%%%%###########%%%%%%%%######-........:------::.+%%%%%###############%#############%*+++++*.:*##+. .
-+####****##%%%%%%%%###%########%%@%#####:.......:::::::...=@@%%%@###############%############%*+++++*. =##+
+#*##*****####%%%%##%%%%#########%@%#####:.....-:....:..::=*=++++############################%#*+++++ -##.
=%**********#######%%###############%*#####=...=. . :-=-: .###############%#******####%#####+= +*
*#************#############%%#######**+*%####-.- .-:=##############%*********##%#######=: .+.
*#****************##**######%%%###*: ::*#####: -:: .-+####%%%####%*********#%##########*-:
=#***********************######%*- .--:=:. =. :--: .-=*###%###*********#######*#######+-
.#**************************###= .:. ::... .=. +***#%##*******#########***#######+:
=##**************************: :-:--. .. .+..... + =******#%#####%##########***#####**#
####**********************+. . :#########+-=:.... -: +***********##%%#***######****##****
.#%####******************- *##########*:....... ...:. #**************##*********###*##****
:#%@%%%###************* =#*******####+....... :-. *#***************%************#%#****
.#**###%%%%%######****#. =#***********#=....:-. :*#****************##*************####*
*#**#****#####%%%%##### =#*************+=: .=-::..:-*#*****************##*****************##
-#***#*******######%%%%= :=*#**********: ... ##*##%##*******************#********************
.%*****#*********######%. ....:-=*- :++: =#****##%####*************###********************
:. +#*****#**********####%+ ..-*#***#+=##*******##%%###********####*********************
.:-:. .%#*****#**********#%%%* :=+**##########******###********#%%%%%%##****###***********************
-- =%#******#*********@#%%-. :#%%%%%%%%%%%%%%###*****###*****#%%##%%%#%@#####*************************
=-. .=. ##*******#********#%#**###*+=-:.: -######%##%###%####%%%%%##########%####%%%%##%*************####************
==== .= :%#*******#********%%***************++#%%#%#######%###%#######%%%%%%%########%%%%####%#************####***********
==== -: *%#*******#********@#********************#%#######%###%###########%%%%%#####%%%#######%%#**********#####**********
:+=+. = .%###*******#******#%#*******************#%#############%##############%%%##%######***####%%#*******######*********
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "routers.h"
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using mll = map<ll, ll>;
using sll = set<ll>;
using sc = set<char>;
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
cheese;
#define printv(v) {for(auto &elem:v){cout<<elem<<" ";}cout<<endl;}
#define printg(g) {for(auto &row:g){printv(row)};}
#define printm(m) {for(auto &pa:m){cout << pa.first << "," << pa.second << " ";};cout<<endl;}
ll min(int a,ll b) { if (a<b) return a; return b; }
ll min(ll a,int b) { if (a<b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
string to_upper(string a) { for (ll i=0;i<(ll)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (ll i=0;i<(ll)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool is_prime(ll a)
{
if(a==1) return false;
for(ll i=2;i*i<=a;i++)
{
if (a%i==0) return false;
}
return true;
}
ll binpow(ll a, ll b, ll m)
{
a %= m;
ll res = 1;
while (b > 0)
{
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll moddiv(ll numerator,ll denominator, ll mod)
{
numerator %= mod;
denominator %= mod;
return numerator * binpow(denominator,mod-2,mod) % mod;
}
//DSU https://cp-algorithms.com/data_structures/disjoll_set_union.html
const ll DSUN = 200000;
ll parent[DSUN];
ll set_size[DSUN];
ll find_set(ll v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void make_set(ll v) {
parent[v] = v;
set_size[v] = 1;
}
void union_sets(ll a, ll b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (set_size[a] < set_size[b])
swap(a, b);
parent[b] = a;
set_size[a] += set_size[b];
}
}
//DSU
//https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html
vector<char> segmentedSieve(ll L, ll R) {
// generate all primes up to sqrt(R)
ll lim = sqrt(R);
vector<char> mark(lim + 1, false);
vector<ll> primes;
for (ll i = 2; i <= lim; ++i) {
if (!mark[i]) {
primes.emplace_back(i);
for (ll j = i * i; j <= lim; j += i)
mark[j] = true;
}
}
vector<char> isPrime(R - L + 1, true);
for (ll i : primes)
for (ll j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
isPrime[j - L] = false;
if (L == 1)
isPrime[0] = false;
return isPrime;
}
//segtree
vector<ll> builder(vector<ll> &arr)
{
//min
ll identityelement = INT_MAX;
vector<ll> segtree;
ll n,segtree_size;
n=arr.size();
segtree_size=1;
while(segtree_size<n)
{
segtree_size<<=1;
}
segtree.assign(2*segtree_size,identityelement);
for(ll i=0;i<n;i++)
{
segtree[segtree_size+i]=arr[i];
}
for(ll i=segtree_size-1;i>0;i--)
{
segtree[i]=min(segtree[i*2],segtree[i*2+1]);
}
return segtree;
}
ll query(vector<ll>&segtree,vector<ll>&lazy,ll query_l,ll query_r,ll val,ll node_l,ll node_r,ll node)
{
// min
ll identity_element = INT_MAX;
if(lazy[node]!=0)
{
segtree[node]=lazy[node];
if(node_l!=node_r)
{
lazy[node*2]=lazy[node];
lazy[node*2+1]=lazy[node];
}
lazy[node]=0;
}
if(val!=-1 && query_l<=node_l && node_r<=query_r)
{
segtree[node]=val;
if(node_l!=node_r)
{
lazy[node*2]=val;
lazy[node*2+1]=val;
}
lazy[node]=0;
}
if(query_l<=node_l && node_r<=query_r)
{
return segtree[node];
}
else if(node_r<query_l || query_r<node_l)
{
return identity_element;
}
else
{
ll mid = node_l+(node_r-node_l)/2;
ll sum = min(query(segtree,lazy,query_l,query_r,val,node_l,mid,node*2),query(segtree,lazy,query_l,query_r,val,mid+1,node_r,node*2+1));
segtree[node] = min(segtree[node*2],segtree[node*2+1]);
return sum;
}
return 0ll;
}
//segtree
ll kadane(vll &a)
{
ll ans = a[0], sum = 0;
for (ll r = 0; r < a.size(); r++)
{
sum += a[r];
ans = max(ans, sum);
sum = max(sum, 0);
}
return ans;
}
vector<bool>sieve(ll n)
{
vector<bool>ret(n+1,true);
ret[0]=false;
ret[1]=false;
for(ll i=2;i<=n;i++)
{
if(ret[i])
{
for(ll j=i*i;j<=n;j+=i)
{
ret[j]=false;
}
}
}
return ret;
}
ll mod=998244353;
ll arr[200001];
ll solve(string &s)
{
ll cnt,ret;
ret=0;
cnt=0;
ll pntr=s.size()-1;
for(auto ch:s)
{
if(ch=='1')
{
ret+=(max(arr[pntr]+(cnt%2 ? -1 : 1),0)+mod)%mod;
ret%=mod;
cnt+=1;
}
pntr-=1;
}
return ret;
}
// https://www.geeksforgeeks.org/inversion-count-in-array-using-merge-sort/
ll countAndMerge(vector<ll>& arr, ll l, ll m, ll r) {
// Counts in two subarrays
ll n1 = m - l + 1, n2 = r - m;
// Set up two vectors for left and right halves
vector<ll> left(n1), right(n2);
for (ll i = 0; i < n1; i++)
left[i] = arr[i + l];
for (ll j = 0; j < n2; j++)
right[j] = arr[m + 1 + j];
// Initialize inversion count (or result) and merge two halves
ll res = 0;
ll i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count if left[] has a
// smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
// If right is smaller, then it is smaller than n1-i
// elements because left[] is sorted
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array
ll countInv(vector<ll>& arr, ll l, ll r){
ll res = 0;
if (l < r) {
ll m = (r + l) / 2;
// Recursively count inversions in the left and
// right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
ll inversionCount(vector<ll> &arr) {
ll n = arr.size();
return countInv(arr, 0, n-1);
}
/*
int use_detector(int x)
{
cout << x << endl;
int owo;
cin >> owo;
return owo;
}
*/
int use_detector(int x);
vector<int> find_routers(int l, int n, int q)
{
vector<long long> owo(n,l);
vll min_segtree,lazy;
ll min_segtree_size;
min_segtree=builder(owo);
min_segtree_size=min_segtree.size()/2;
lazy.assign(min_segtree_size*2,0);
vector<int> indices({0});
int gap,left,right,mid,response,ans;
for(int i=1;i<n;i++)
{
left=indices.back()+1;
right=query(min_segtree,lazy,i,n-1,-1,1,min_segtree_size,1);
ans=right;
while(left<=right)
{
mid=left+(right-left)/2;
response=use_detector(mid);
if(response==i)
{
ans=mid;
right=mid-1;
}
else if(response>i)
{
right=mid-1;
}
else
{
left=mid+1;
}
query(min_segtree,lazy,response,response,mid,1,min_segtree_size,1);
}
indices.push_back(2*(ans-1)-indices.back());
}
return indices;
}
/*
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//vll segtree,lazy;
//ll segtree_size;
//segtree=builder(arr);
//segtree_size=segtree.size()/2;
//lazy.assign(segtree_size*2,0);
//update: query_sum(segtree,lazy,a,b,c,1,segtree_size,1);
//query: cout << query_sum(segtree,lazy,a,b,-1,1,segtree_size,1) << endl;
printv(find_routers(6, 3, 10));
}
*/
# | 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... |