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
#include <bits/stdc++.h>

using namespace std;

#ifdef _WIN32
inline int getchar_unlocked() {
    return getchar();
}

inline void putchar_unlocked(int a) {
    putchar(a);
}
#endif // getchar_unlocked

void readint(int &a) {
    char c;
    a = 0;
    do {
        c = getchar_unlocked();
    } while(c < '0' || c > '9');
    do {
        a = a * 10 + c - '0';
        c = getchar_unlocked();
    } while(c >= '0' && c <= '9');
}

void writeint(int a) {
    int x = 1;
    while(a / x / 10)
        x *= 10;
    while(x) {
        putchar_unlocked('0' + a / x);
        a %= x;
        x /= 10;
    }
}

typedef long long ll;

const ll MOD1 = 1e9 + 7;
const ll MOD2 = 1e9 + 9;
const ll POT1 = 1145689;
const ll POT2 = 1157179;

struct node {
    node *l, *r;
    ll h1, h2;

    node() {
        l = nullptr;
        r = nullptr;
    };

    node(ll v) {
        l = nullptr;
        r = nullptr;
        h1 = h2 = v;
    }
};

ll pom1[1100007], pom2[1100007];
int n, m, ile;
int t[500007], p[500007], x[500007];
int pom[1000007], ind;
map<int, int> M;
node* roots[500007];
int res[500007];

node* make_tree(int aa, int bb) {
    if(aa == bb) {
        node *a;
        if(aa < n)
            a = new node(t[aa]);
        else
            a = new node(0);
        return a;
    }
    int mid = (aa + bb) >> 1;
    node *a = make_tree(aa, mid);
    node *b = make_tree(mid + 1, bb);
    node *res = new node();
    res->l = a;
    res->r = b;
    res->h1 = (a->h1 + b->h1 * pom1[mid - aa + 1]) % MOD1;
    res->h2 = (a->h2 + b->h2 * pom2[mid - aa + 1]) % MOD2;
    return res;
}

void insert(node *n, int a, int b, int c, int v) {
    if(a == b) {
        n->l = nullptr;
        n->r = nullptr;
        n->h1 = v;
        n->h2 = v;
        return ;
    }

    int mid = (a + b) >> 1;
    if(c <= mid) {
        n->l = new node(*(n->l));
        insert(n->l, a, mid, c, v);
        n->h1 = (n->l->h1 + n->r->h1 * pom1[mid - a + 1]) % MOD1;
        n->h2 = (n->l->h2 + n->r->h2 * pom2[mid - a + 1]) % MOD2;
    } else {
        n->r = new node(*(n->r));
        insert(n->r, mid + 1, b, c, v);
        n->h1 = (n->l->h1 + n->r->h1 * pom1[mid - a + 1]) % MOD1;
        n->h2 = (n->l->h2 + n->r->h2 * pom2[mid - a + 1]) % MOD2;
    }
}

int comp(node *n1, node *n2, int a, int b) {
    while(a != b) {
        if(n1->l->h1 != n2->l->h1 || n1->l->h2 != n2->l->h2) {
            n1 = n1->l;
            n2 = n2->l;
            b = (a + b) >> 1;
        } else {
            n1 = n1->r;
            n2 = n2->r;
            a = ((a + b) >> 1) + 1;
        }
    }

    if(n1->h1 != n2->h1)
        return n1->h1 < n2->h1 ? -1 : 1;
    return 0;
}

inline bool cmp(int a, int b) {
    int res = comp(roots[a], roots[b], 0, ile - 1);

    if(res == 0)
        return a < b;
    if(res == -1)
        return true;
    return false;
}

int main() {
    srand(time(NULL));
    pom1[0] = pom2[0] = 1;
    for(int i = 1 ; i <= 1100000 ; i++) {
        pom1[i] = (pom1[i - 1] * POT1) % MOD1;
        pom2[i] = (pom2[i - 1] * POT2) % MOD2;
    }

    readint(n);
  // n = m = 500000;
    readint(m);
    for(int i = 0 ; i < n ; i++) {
       readint(t[i]);
    //t[i] = abs(rand() * rand());
        pom[ind++] = t[i];
    }
    for(int i = 1 ; i < m ; i++) {
        readint(p[i]);
        readint(x[i]);
    //p[i] = rand() % n + 1;
   // x[i] = abs(rand() * rand());
        pom[ind++] = x[i];
    }

    sort(pom, pom + ind);
    int act = -1;
    for(int i = 0 ; i < ind ; i++) {
        if(i == 0 || pom[i] != pom[i - 1]) {
            act++;
            M[pom[i]] = act;
        }
    }

    for(int i = 0 ; i < n ; i++)
        t[i] = M[t[i]];
    for(int i = 1 ; i < m ; i++)
        x[i] = M[x[i]];

    ile = 2;
    while(ile < n)
        ile *= 2;

    roots[0] = make_tree(0, ile - 1);

    for(int i = 1 ; i < m ; i++) {
        roots[i] = new node(*roots[i - 1]);
        insert(roots[i], 0, ile - 1, p[i] - 1, x[i]);
    }

    for(int i = 0 ; i < m ; i++)
        res[i] = i;

    sort(res, res + m, cmp);
    for(int i = 0 ; i < m ; i++) {
        writeint(res[i] + 1);
        putchar_unlocked(' ');
    }
    putchar_unlocked('\n');

    return 0;
}