1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include <vector>
#include <stdexcept>
#include <list>
#include <set>
#include <map>
#include <cassert>

#define ll long long
using namespace std;

int n, m;
vector<int> T;
vector<int> P;
vector<int> X;


void solveBrut() {
    vector<vector<int> > groups;
    vector<int> start;
    for(int i=0;i<m;i++){
        start.push_back(i);
    }
    groups.push_back(start);
    for(int day =0;day < n; day++) {
        int dt[m];
        dt[0]=T[day];
        for(int s=1;s<m;s++){
            int x;
            if(P[s-1]==day)x=X[s-1];
            else x=dt[s-1];
            dt[s]=x;
        }
        vector<vector<int> > newGroups;
        for(vector<int> group: groups){
            vector<pair<int, int>> temps;
            for(int x: group) {
                temps.push_back(make_pair(dt[x],x));
            }
            sort(temps.begin(), temps.end());
            vector<int>current;
            current.push_back(temps[0].second);
            for(int i=1;i<temps.size();i++){
                if(temps[i-1].first==temps[i].first)current.push_back(temps[i].second);
                else {
                    newGroups.push_back(current);
                    current.clear();
                    current.push_back(temps[i].second);
                }
            }
            newGroups.push_back(current);
        }
        groups = newGroups;
     /*    for(vector<int> group:groups){
            cout << "(";
            for(int x:group)cout <<x+1<<" ";
            cout << ") ";
        }
        cout << endl;*/

    }
    for(vector<int> group:groups){
        for(int x:group)cout <<x+1<<" ";
    }
    cout <<endl;
}

class Range {
    public:
    vector<int>from;
    vector<int>to;
    int val;

    Range(int f, int t, int v) {
        from.push_back(f);
        to.push_back(t);
        val=v;
    }
    string to_string(){
        string res = "|"+std::to_string(val) +":";
        for(int i=0;i<from.size();i++) {
                res += " [" + std::to_string(from[i])+","+std::to_string(to[i])+"]";
        }
        res +="|";
        return res;
    }

    void merge(Range r2) {
        for(int i=0;i<r2.to.size();i++){
            to.push_back(r2.to[i]);
            from.push_back(r2.from[i]);
        }
    }

    bool operator< (const Range& rhs) const
    { return val < rhs.val;}

};


struct Elem {
    int val;
    Elem* next;
    Elem * branch;
    Elem(int x){
        val = x;
        next = nullptr;
        branch = nullptr;
    }
};

struct List {
    Elem * first=nullptr;
    int size =0;
};

vector<List> Tree;
int tn=0;
void createTree(){
    tn =1;
    while(tn < m)tn*=2;
    Tree.resize(tn*2);

}

int elemsCreated =0;

void prepend(int p, List* list) {
    Elem* el = new Elem(-1);
    el->branch = list->first;
    el->next = Tree[p].first;
    Tree[p].first = el;
    Tree[p].size += list->size;
}

void prepend(int p, int val) {
    elemsCreated++;
    Elem* el = new Elem(val);
    el->next = Tree[p].first;
    Tree[p].first = el;
    Tree[p].size ++;
}


void pushDown(int p){
    if(Tree[p].first==nullptr)return;
    int ch1 = 2*p;
    int ch2 = 2*p+1;
    prepend(ch1, &(Tree[p]));
    prepend(ch2, &(Tree[p]));
    Tree[p].first = nullptr;
    Tree[p].size=0;
}

void pushAll(int p){
    if(p<tn) {
        pushDown(p);
        pushAll(2*p);
        pushAll(2*p+1);
    }
}

void fillRange(int p, int pfrom, int pto, int rfrom, int rto, int value) {
    if(pfrom==rfrom && pto==rto){
        prepend(p, value);
    } else {
        pushDown(p);
        int firstRight =(pfrom + pto)/2;
        if(rfrom < firstRight) {
            fillRange(2*p, pfrom, firstRight, rfrom, min(rto, firstRight), value );
        }
        if(rto > firstRight) {
            fillRange(2*p+1, firstRight, pto, max(firstRight, rfrom), rto, value);
        }
    }
}

void fillFromList(Elem *e, vector<int> &res){

    while(e!=nullptr){
        if(e->val!=-1){
                res.push_back(e->val);
        } else {
            fillFromList(e->branch, res);
        }
        e = e->next;
    }
}

void print_list(Elem*e, bool topLevel){
    if(topLevel)cerr<<"[";
    while(e!=nullptr){
        if(e->val!=-1)
            cerr<<e->val;
        else
            print_list(e->branch, false);
        e = e->next;
    }
    if(topLevel)cerr<<"]";
}

void printTree() {
    for(int node = 1;node<tn+m;node++){
        cerr <<node<<": ";
        print_list(Tree[node].first, true);
        cerr<<"("<<Tree[node].size<<")"<<endl;
    }
    cerr<<"-------------------------------------"<<endl;
}




void updateRange(Range range){
    for(int i=0;i<range.from.size();i++){
        fillRange(1, 0, tn, range.from[i], range.to[i], range.val);
    }
}


int compare( Elem*e1,  Elem*e2) {
    vector<int>a;vector<int>b;
    fillFromList(e1,a);fillFromList(e2,b);
    if(a<b)return -1;
    if(a>b)return 1;
    return 0;
}


int compCalls =0;
ll comploops =0;
int compareSmart( Elem*e1,  Elem*e2) {
    compCalls++;
    vector<Elem*>S1;
    vector<Elem*>S2;
    S1.push_back(e1);
    S2.push_back(e2);
    while(!S1.empty() && !S2.empty()) {
        Elem*a = S1.back();
        Elem*b = S2.back();
        comploops++;
        if(a==b){
                S1.pop_back();S2.pop_back();
                continue;
        }
        if(a->val==-1){
            S1.pop_back();
            if(a->next !=nullptr)S1.push_back(a->next);
            S1.push_back(a->branch);
        } else if(b->val==-1){
            S2.pop_back();
            if(b->next !=nullptr)S2.push_back(b->next);
            S2.push_back(b->branch);
        } else {
            S1.pop_back();S2.pop_back();
            //cerr << "comparing "<<a->val<<" "<<b->val<<endl;
            if(a->val < b->val)return -1;
            if(b->val < a->val)return 1;
            if(a->next!=nullptr)S1.push_back(a->next);
            if(b->next!=nullptr)S2.push_back(b->next);
        }
    }
    return 0;
}


void merge(list<int> &v1, list<int> &v2, list<int> &res){


        auto p1=v1.begin();auto p2=v2.begin();
        while(p1!=v1.end()||p2!=v2.end()) {
            if(p1==v1.end()){
                res.push_back(*p2);
                p2++;
            }else if(p2==v2.end()){
                res.push_back(*p1);
                p1++;
            }else {
                int c = compareSmart(Tree[*p1].first, Tree[*p2].first);
                if(c==0){
                    if(*p1<*p2)c=-1;
                    else c = 1;
                }
                if(c==-1){
                    res.push_back(*p1);
                    p1++;
                }else {
                    res.push_back(*p2);
                    p2++;
                }
            }
        }
}

list<int>* mergeSort(int node) {
    list<int>*res=new list<int>();;
    if(node>=tn) {
        if(node-tn<m)res->push_back(node);

    } else {
        int ch1 = node*2;
        int ch2 = node*2+1;
        list<int>* v1 = mergeSort(ch1);

        list<int>* v2 = mergeSort(ch2);


        if(Tree[ch1].first!=nullptr && Tree[ch2].first!=nullptr) {
            int c = compareSmart(Tree[ch1].first, Tree[ch2].first);
            if(c==-1){
               // res->splice(res->begin(), *v1);
                //res->splice(res->end(), *v2);
                v1->splice(v1->end(),*v2);
                res = v1;
            }
            if(c==1){
                 v2->splice(v2->end(),*v1);
                res = v2;
            }
            if(c==0){
                pushAll(ch1);
                pushAll(ch2);
                merge(*v1,*v2,*res);
            }
        } else {
            pushAll(ch1);
            pushAll(ch2);
            merge(*v1,*v2,*res);
        }
    }
    return res;
}


void solve(){
  createTree();
  vector<vector<int>> dayChanges(n+1);
  for(int s =1;s<m;s++){
    int d = P[s-1];
    dayChanges[d].push_back(s);
  }
  vector<vector<Range>> dayRanges;
  for(int day =0;day < n; day++) {
      vector<Range> ranges;
      int prevS =0;
      int prevVal = T[day];
      for(int s:dayChanges[day]){
        if(P[s-1]==day){
            Range r(prevS, s, prevVal);
            ranges.push_back(r);
            prevS = s;
            prevVal = X[s-1];
        }
      }
      Range r(prevS, m, prevVal);
      ranges.push_back(r);
      sort(ranges.begin(), ranges.end());
      vector<Range> newRanges;
      Range current = ranges[0];
      for(int i=1;i<ranges.size();i++){
        if(ranges[i].val==current.val) {
                current.merge(ranges[i]);
        }
        else {
            newRanges.push_back(current);
            current = ranges[i];
        }
      }
      newRanges.push_back(current);
      if(newRanges.size()>1)dayRanges.push_back(newRanges);


  }
  for(int j = dayRanges.size()-1;j>=0;j--) {
      vector<Range> ranges = dayRanges[j];
      for(int i=0;i<ranges.size();i++){
           // cerr<< newRanges[i].to_string()<<" ";
            updateRange(ranges[i]);
      }

  }
 /*
 // printTree();
  pushAll(1);
 // printTree();

  vector<pair<List, int>> V;
  for(int i=0;i<m;i++){
    V.push_back(make_pair(Tree[tn+i], i));
  }
  cerr<<"starting sort\n";
  sort(V.begin(),V.end(),
       [](const pair<List, int> & a, const pair<List, int> & b) -> bool
    {
        int r = compareSmart(a.first.first, b.first.first);
        if(r!=0)return r==-1;
        return a.second<b.second;
    });
  cerr << compCalls<<" "<<comploops<<endl;
  for(int i=0;i<m;i++)cout << V[i].second+1<<" ";
    */
  cerr <<"starting merge\n";
  list<int>*res = mergeSort(1);
  for(int x:*res)cout << x-tn+1<<" ";
  cout<<endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    istream& my_in = cin;
    //ifstream my_in; my_in.open ("3Atesty/3.in");
    my_in >> n >>m;
    for(int i=0;i<n;i++){
        int t;
        my_in >> t;
        T.push_back(t);
    }
    for(int i=0;i<m-1;i++) {
        int p, x;
        my_in >> p >> x;
        P.push_back(p-1);
        X.push_back(x);
    }
    solve();
    return 0;
}