#include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++) #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define SIZE(X) int(X.size()) #define CONTAINS(A,X) (A.find(X) != A.end()) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); } typedef signed long long slong; typedef long double ldouble; typedef pair<int,int> pii; const slong INF = 1000000100; const ldouble EPS = 1e-9; template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #ifdef LOCALs #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif static const int M = (1<<19); typedef pair<int,int> payload; slong K[2] = {1000000007, 1000000097}; slong P[2] = {1000001447, 1000001449}; inline slong fpow(slong a, slong x, slong p) { a %= p; slong result = 1; while(x != 0) { if(x % 2 == 1) result = (result * a) % p; a = (a * a) % p; x /= 2; } return result; } slong pw[M+42][2]; void prep_hash() { pw[0][0] = pw[0][1] = 1; FORW(i,1,M) { FORW(j,0,2) { pw[i][j] = (pw[i-1][j] * K[j]) % P[j]; } } } inline slong make_hash(slong val, int pos, int j) { return (val * pw[pos][j]) % P[j]; } inline payload make_hash(slong val, int pos) { return {make_hash(val,pos,0), make_hash(val,pos,1)}; } inline slong unhash(payload h, int pos) { //return 0; return (h.X * fpow(K[0], P[0]-1-pos, P[0])) % P[0]; } inline int sum_mod(int a, int b, int p) { int ret = a + b; if(ret >= p) ret -= p; return ret; } //int cmp_cnt = 0; struct tree { const payload ZERO = {0, 0}; int n; vector<int> L, R; vector<payload> T; tree(int size) : n(0), L(size, -1), R(size, -1), T(size, ZERO) {} inline payload merge(const payload &x, const payload &y) { return {sum_mod(x.X, y.X, P[0]), sum_mod(x.Y, y.Y, P[1])}; } inline payload eval(int v) { if(v < 0) return ZERO; return T[v]; } payload query(int a, int l, int r, int v) { if(v < 0 or a < l or a > r) { assert(false); /// fail? return ZERO; } if(l == r) { return T[v]; } int mid = (l + r) / 2; if(a <= mid) { return query(a, l, mid, L[v]); } return query(a, mid+1, r, R[v]); } int insert(int a, int l, int r, int v, payload val, slong real_value) { if(a < l or a > r) return v; int u = v; v = ++n; if(u >= 0) L[v] = L[u], R[v] = R[u], T[v] = T[u]; if(l == r) { T[v] = val; L[v] = - real_value; /// super hack return v; } int mid = (l + r) / 2; L[v] = insert(a, l, mid, L[v], val, real_value); R[v] = insert(a, mid+1, r, R[v], val, real_value); T[v] = merge(eval(L[v]), eval(R[v])); return v; } inline payload query(int a, int id) { return query(a, 0, M-1, id); } inline int insert(int a, payload val, int id, slong real_value) { return insert(a, 0, M-1, id, val, real_value); } pair<int, payload> first_diff(int l, int r, int v, int w) { //debug(l, r, v, w); //assert(v >= 0 and w >= 0); if(l == r) { return {l, {-L[v], -L[w]}}; } int mid = (l + r) / 2; //cmp_cnt++; if(T[L[v]] != T[L[w]]) { return first_diff(l, mid, L[v], L[w]); } return first_diff(mid + 1, r, R[v], R[w]); } inline pair<int, payload> first_diff(int id, int id2) { //cmp_cnt++; if(T[id] == T[id2]) return {-1, ZERO}; return first_diff(0, M-1, id, id2); } }; typedef vector<int> vi; const int MAXV = M * 42; /// uwaga na pamiec tree T(MAXV); int root[M+42]; bool cmp(int i, int j) { auto res = T.first_diff(root[i], root[j]); //cerr << "porownuje "; //debug(i,j); if(res.X == -1) { return (i < j); } slong x = res.Y.X; //unhash(res.Y.X, res.X); slong y = res.Y.Y; //unhash(res.Y.Y, res.X); //debug(res.X, x, y); return (x < y); } void solve() { prep_hash(); srand(42); const int AUTO_GEN = 0, MX = 1000000000; int n, m; if(!AUTO_GEN) cin >> n >> m; else { n = m = 500000; //n -= 200000; } vi v(n+42); FOR(i,1,n) { if(!AUTO_GEN) cin >> v[i]; else v[i] = rand() % (MX+1); root[1] = T.insert(i, make_hash(v[i]+1, i), root[1], v[i]+1); } FOR(i,2,m) { int p, x; if(!AUTO_GEN) cin >> p >> x; else { p = 1 + rand() % n; x = rand() % (MX+1); } root[i] = T.insert(p, make_hash(x+1, p), root[i-1], x+1); } //return; cerr << "prep " << endl; vi ans(m); FORW(i,0,m) ans[i] = i + 1; sort(ALL(ans), cmp); FORW(i,0,m) cout << ans[i] << " "; cout << "\n"; //debug(cmp_cnt); //while(42); } int main() { ios_base::sync_with_stdio(0); solve(); }
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 | #include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++) #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define SIZE(X) int(X.size()) #define CONTAINS(A,X) (A.find(X) != A.end()) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); } typedef signed long long slong; typedef long double ldouble; typedef pair<int,int> pii; const slong INF = 1000000100; const ldouble EPS = 1e-9; template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #ifdef LOCALs #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif static const int M = (1<<19); typedef pair<int,int> payload; slong K[2] = {1000000007, 1000000097}; slong P[2] = {1000001447, 1000001449}; inline slong fpow(slong a, slong x, slong p) { a %= p; slong result = 1; while(x != 0) { if(x % 2 == 1) result = (result * a) % p; a = (a * a) % p; x /= 2; } return result; } slong pw[M+42][2]; void prep_hash() { pw[0][0] = pw[0][1] = 1; FORW(i,1,M) { FORW(j,0,2) { pw[i][j] = (pw[i-1][j] * K[j]) % P[j]; } } } inline slong make_hash(slong val, int pos, int j) { return (val * pw[pos][j]) % P[j]; } inline payload make_hash(slong val, int pos) { return {make_hash(val,pos,0), make_hash(val,pos,1)}; } inline slong unhash(payload h, int pos) { //return 0; return (h.X * fpow(K[0], P[0]-1-pos, P[0])) % P[0]; } inline int sum_mod(int a, int b, int p) { int ret = a + b; if(ret >= p) ret -= p; return ret; } //int cmp_cnt = 0; struct tree { const payload ZERO = {0, 0}; int n; vector<int> L, R; vector<payload> T; tree(int size) : n(0), L(size, -1), R(size, -1), T(size, ZERO) {} inline payload merge(const payload &x, const payload &y) { return {sum_mod(x.X, y.X, P[0]), sum_mod(x.Y, y.Y, P[1])}; } inline payload eval(int v) { if(v < 0) return ZERO; return T[v]; } payload query(int a, int l, int r, int v) { if(v < 0 or a < l or a > r) { assert(false); /// fail? return ZERO; } if(l == r) { return T[v]; } int mid = (l + r) / 2; if(a <= mid) { return query(a, l, mid, L[v]); } return query(a, mid+1, r, R[v]); } int insert(int a, int l, int r, int v, payload val, slong real_value) { if(a < l or a > r) return v; int u = v; v = ++n; if(u >= 0) L[v] = L[u], R[v] = R[u], T[v] = T[u]; if(l == r) { T[v] = val; L[v] = - real_value; /// super hack return v; } int mid = (l + r) / 2; L[v] = insert(a, l, mid, L[v], val, real_value); R[v] = insert(a, mid+1, r, R[v], val, real_value); T[v] = merge(eval(L[v]), eval(R[v])); return v; } inline payload query(int a, int id) { return query(a, 0, M-1, id); } inline int insert(int a, payload val, int id, slong real_value) { return insert(a, 0, M-1, id, val, real_value); } pair<int, payload> first_diff(int l, int r, int v, int w) { //debug(l, r, v, w); //assert(v >= 0 and w >= 0); if(l == r) { return {l, {-L[v], -L[w]}}; } int mid = (l + r) / 2; //cmp_cnt++; if(T[L[v]] != T[L[w]]) { return first_diff(l, mid, L[v], L[w]); } return first_diff(mid + 1, r, R[v], R[w]); } inline pair<int, payload> first_diff(int id, int id2) { //cmp_cnt++; if(T[id] == T[id2]) return {-1, ZERO}; return first_diff(0, M-1, id, id2); } }; typedef vector<int> vi; const int MAXV = M * 42; /// uwaga na pamiec tree T(MAXV); int root[M+42]; bool cmp(int i, int j) { auto res = T.first_diff(root[i], root[j]); //cerr << "porownuje "; //debug(i,j); if(res.X == -1) { return (i < j); } slong x = res.Y.X; //unhash(res.Y.X, res.X); slong y = res.Y.Y; //unhash(res.Y.Y, res.X); //debug(res.X, x, y); return (x < y); } void solve() { prep_hash(); srand(42); const int AUTO_GEN = 0, MX = 1000000000; int n, m; if(!AUTO_GEN) cin >> n >> m; else { n = m = 500000; //n -= 200000; } vi v(n+42); FOR(i,1,n) { if(!AUTO_GEN) cin >> v[i]; else v[i] = rand() % (MX+1); root[1] = T.insert(i, make_hash(v[i]+1, i), root[1], v[i]+1); } FOR(i,2,m) { int p, x; if(!AUTO_GEN) cin >> p >> x; else { p = 1 + rand() % n; x = rand() % (MX+1); } root[i] = T.insert(p, make_hash(x+1, p), root[i-1], x+1); } //return; cerr << "prep " << endl; vi ans(m); FORW(i,0,m) ans[i] = i + 1; sort(ALL(ans), cmp); FORW(i,0,m) cout << ans[i] << " "; cout << "\n"; //debug(cmp_cnt); //while(42); } int main() { ios_base::sync_with_stdio(0); solve(); } |