Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- void cvp(vector <pii> a){
- for (auto p: a){
- cout<<p.first<<' '<<p.second<<"\n";
- }
- cout<<"\n";
- }
- bool sh=1;
- int n,N,k;
- vector <int> a,a1;
- const int inf = 1e9;
- int up2(int x){
- int y = log2(x);
- if (pow(2, y) < x){
- y++;
- }
- y = pow(2, y);
- return y;
- }
- void seep(pii p){
- cout<<"("<<p.first<<","<<p.second<<") ";
- }
- struct tree{
- vector <pii> MN; //{mn, id}
- vector <pii> fr0, fr1; //макс мин на отрезках длины 2 {max mn, id}
- void showMN(){
- //cout<<"SHOW\n";
- cout<<"MN\n";
- int id=-1;
- for (int lg = 0; lg <= log2(N); ++lg){
- for (int i = 0; i < pow(2, lg); ++i){
- id++;
- seep(MN[id]);
- }cout<<"\n";
- }cout<<"\n";
- }
- void showfr0(){
- cout<<"fr0\n";
- int id=-1;
- for (int lg = 0; lg <= log2(N) - 1; ++lg){
- for (int i = 0; i < pow(2, lg); ++i){
- id++;
- seep(fr0[id]);
- }cout<<"\n";
- }cout<<"\n";
- }
- void showfr1(){
- cout<<"fr1\n";
- int id = -1;
- for (int lg = 0; lg <= log2(N) - 1; ++lg){
- for (int i = 0; i < pow(2, lg); ++i){
- id++;
- seep(fr1[id]);
- }cout<<"\n";
- }cout<<"\n";
- }
- void bld(){
- for (int i = 1; i < n; ++i){
- if (n % 2 == 0 && i == n - 1) continue;
- a1.push_back(a[i]);
- }
- //while (a1.size() < a.size()) a1.push_back(inf);
- N = up2(n);
- MN.assign(2 * N - 1, {inf,-1});
- for (int i = N - 1; i < N - 1 + n; ++i){
- int id = i - ( N - 1);
- MN[i] = {a[id], id};
- }
- for (int i = N - 2;i >= 0; --i){
- if (MN[2*i + 1] < MN[2 * i + 2]){
- MN[i] = MN[2*i + 1];
- }
- else{
- MN[i] = MN[2 * i + 2];
- }
- }
- fr0.assign(N - 1, {0,-1});
- fr1.assign(N - 1, {0,-1});
- for (int i = 0; i + 1 < n; i += 2){
- int mn = a[i], id = i;
- if (a[i] > a[i+1]){
- mn = a[i+1];
- id++;
- }
- fr0[i/2 + N/2 - 1] = {mn, id};
- }
- for (int i = 0; i + 1 < n; i += 2){
- if (n % 2 == 0 && i + 1 == n - 1) continue;
- int mn = a1[i], id = i;
- if (a1[i] > a1[i + 1]){
- mn = a1[i + 1];
- id++;
- }
- fr1[i/2 + N/2 - 1] = {mn, id+1};
- }
- for (int i = N/2 - 2; i >= 0; --i){
- /*fr0[i] = max(fr0[i*2 + 1], fr0[i * 2 + 2]);
- fr1[i] = max(fr1[i*2 + 1], fr1[i * 2 + 2]);*/
- pii p = fr0[i*2 + 1];
- if (fr0[i*2 + 2].first > fr0[i*2 + 1].first){
- p = fr0[i*2 + 2];
- }
- fr0[i] = p;
- }
- for (int i = N/2 - 2; i >= 0; --i){
- pii p = fr1[i*2 + 1];
- if (fr1[i*2 + 2].first > fr1[i*2 + 1].first){
- p = fr1[i*2 + 2];
- }
- fr1[i] = p;
- }
- if (sh){
- //showMN();
- //showfr0();
- }
- }
- void updMN(int id){
- //MN
- id += N - 1;
- MN[id].first = inf;
- while (id > 0){
- id--;
- id /= 2;
- pii p = MN[2 * id + 1];
- if (MN[2 * id + 1].first > MN[2 * id + 2].first){
- p = MN[2 * id + 2];
- }
- MN[id] = p;
- }
- }
- void updfr0(int id){
- //!!!
- a[id] = inf;
- //if id % 2 == 0
- if (sh){
- cout<<"id = "<<id<<"\n";
- }
- //соседи по паре в fr0 и fr1
- if (n % 2 == 1 && id == n - 1){
- if (sh){
- cout<<"C\n";
- }
- return;
- }
- int to;
- if (id % 2 == 0){
- if (sh){
- cout<<"D\n";
- }
- to = id+1;
- }
- else if (id % 2 == 1){
- if (sh){
- cout<<"E\n";
- }
- to = id-1;
- }
- //fr0
- if (sh){
- cout<<"F\n";
- cout<<"to = "<<to<<"\n";
- }
- fr0[(id + N - 1 - 1) / 2] = {a[id], id};
- if (a[to] < a[id]){
- fr0[(id + N - 1 - 1) / 2] = {a[to], to};
- }
- if (sh){
- cout<<"(id + N - 1 - 1) / 2 = "<<((id + N - 1 - 1) / 2)<<"\n";
- cout<<"fr0[(id + N - 1 - 1) / 2] = "; seep(fr0[(id + N - 1 - 1) / 2]); cout<<"\n";
- }
- id = (id + N - 1);
- if (sh){
- cout<<"id = "<<id<<"\n";
- }
- id--;
- id /= 2;
- if (sh){
- cout<<"id = "<<id<<"\n";
- }
- while (id > 0){
- id--;
- id /= 2;
- if (sh){
- cout<<"id = "<<id<<"\n";
- }
- pii p = fr0[id * 2 + 1];
- if (fr0[id * 2 + 2].first > fr0[id * 2 + 1].first){
- p = fr0[id * 2 + 2];
- }
- if (sh){
- cout<<"p = "; seep(p); cout<<"\n";
- }
- fr0[id] = p;
- }
- /*if (sh){
- cout<<"a\n"; cv(a);
- cout<<"upd0 upd1 = "<<upd0<<" "<<upd1<<"\n";
- cout<<"to0 to1 = "<<to0<<" "<<to1<<"\n";
- cout<<"id0 id1 = "<<id0<<" "<<id1<<"\n";
- if (upd0){
- cout<<"fr0[id0] = "; seep(fr0[id0]);
- }
- if (upd1){
- cout<<"fr1[id1] = "; seep(fr1[id1]);
- }
- cout<<"\n";
- }*/
- }
- void updfr1(int id){
- if (id == 0){
- if (sh){
- cout<<"A\n";
- }
- return;
- }
- else if (n % 2 == 0 && id == n - 1){
- if (sh){
- cout<<"B\n";
- }
- return;
- }
- int to;
- if (id % 2 == 0){
- if (sh){
- cout<<"D\n";
- }
- to = id-1;
- }
- else if (id % 2 == 1){
- if (sh){
- cout<<"E\n";
- }
- to = id+1;
- }
- id--;
- a1[id] = inf;
- //fr1
- if (sh){
- cout<<"G\n";
- }
- fr1[(id + N - 1 - 1) / 2] = {a1[id], id};
- if (a1[to] < a1[id]){
- fr1[(id + N - 1 - 1) / 2] = {a1[to], to};
- }
- id = (id + N - 1);
- id--;
- id /= 2;
- while (id > 0){
- id--;
- id /= 2;
- pii p = fr1[id * 2 + 1];
- if (fr1[id * 2 + 2].first > fr1[id * 2 + 1].first){
- p = fr1[id * 2 + 2];
- }
- fr1[id] = p;
- }
- }
- void upd(int id){
- updMN(id);
- updfr0(id);
- updfr1(id);
- }
- };
- void slv(){
- tree T;
- T.bld();
- if (sh){
- cout<<"a\n"; cv(a);
- T.showMN();
- T.showfr0();
- T.showfr1();
- }
- if (sh){
- int q; cin>>q;
- for (int i = 0; i < q; ++i){
- int id; cin>>id;
- T.upd(id);
- if (sh){
- cout<<"a\n"; cv(a);
- T.showMN();
- T.showfr0();
- T.showfr1();
- }
- //T.showfr1();
- }
- }
- }
- int main()
- {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- int t=1; if (!sh) cin>>t;
- for (int go = 0; go < t; ++go){
- cin>>n>>k; a.resize(n); for (int &i: a) cin>>i;
- slv();
- }
- }
- /*
- 20 20
- 1 4 3 6 3
- 1 4 7 4 7
- 1 4 3 6 4
- 1 4 3 2 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement