#include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M = 1024 * 1024; vector<pair<LL, int>> S[2][M]; int n, m; int T[2 * M], A[M], P[M], last[2 * M]; inline void add(LL x, int ind) { //debug("%lld %lld %d\n", x / M, x % M, ind); S[0][x % M].PB(MP(x, ind)); } void solve() { int st = M / 2; while (st >= 1) { //debug("\n%d %d\n", st, 2 * st - 1); for (int j = st; j < 2 * st; j++) { add((LL)T[2 * j] * M + T[2 * j + 1], j); } for (int j = 2; j <= m; j++) { LL ad = 0; if (P[j] % 2 == 0) { if (last[P[j] + 1] == 0) { ad = (LL)A[j] * M + T[P[j] + 1]; } else { ad = (LL)A[j] * M + A[last[P[j] + 1]]; } } else { if (last[P[j] - 1] == 0) { ad = (LL)T[P[j] - 1] * M + A[j]; } else { ad = (LL)A[last[P[j] - 1]] * M + A[j]; } } last[P[j]] = j; add(ad, M + j); P[j] /= 2; } for (int i = 0; i < M; i++) { for (auto x : S[0][i]) { S[1][x.F / M].PB(x); } S[0][i].clear(); } int tmp = -1; LL pre = -1; for (int i = 0; i < M; i++) { for (auto x : S[1][i]) { if (x.F != pre) { pre = x.F; tmp++; } if (x.S <= M) { T[x.S] = tmp; } else { A[x.S - M] = tmp; } } S[1][i].clear(); } st /= 2; } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); //n = 5e5; //m = 5e5; vector<PII> V; //srand(1); for (int i = 0; i < n; i++) { int a; scanf("%d", &a); //a = rand() % 1000; V.PB(MP(a, i)); } for (int i = 2; i <= m; i++) { int pos, val; scanf("%d%d", &pos, &val); //pos = rand() % n + 1; //val = rand() % 1000; pos--; P[i] = M + pos; V.PB(MP(val, M + i)); } sort(ALL(V)); int tmp = 0, pre = -1; for (auto x : V) { if (x.F != pre) { tmp++; pre = x.F; } if (x.S <= M) { T[x.S + M] = tmp; } else { A[x.S - M] = tmp; } } solve(); vector<PII> X; X.PB(MP(T[1], 1)); for (int i = 2; i <= m; i++) { X.PB(MP(A[i], i)); //debug("%d %d\n", A[i], i); } sort(ALL(X)); for (auto x : X) { printf("%d ", x.S); } printf("\n"); return 0; }
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 | #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M = 1024 * 1024; vector<pair<LL, int>> S[2][M]; int n, m; int T[2 * M], A[M], P[M], last[2 * M]; inline void add(LL x, int ind) { //debug("%lld %lld %d\n", x / M, x % M, ind); S[0][x % M].PB(MP(x, ind)); } void solve() { int st = M / 2; while (st >= 1) { //debug("\n%d %d\n", st, 2 * st - 1); for (int j = st; j < 2 * st; j++) { add((LL)T[2 * j] * M + T[2 * j + 1], j); } for (int j = 2; j <= m; j++) { LL ad = 0; if (P[j] % 2 == 0) { if (last[P[j] + 1] == 0) { ad = (LL)A[j] * M + T[P[j] + 1]; } else { ad = (LL)A[j] * M + A[last[P[j] + 1]]; } } else { if (last[P[j] - 1] == 0) { ad = (LL)T[P[j] - 1] * M + A[j]; } else { ad = (LL)A[last[P[j] - 1]] * M + A[j]; } } last[P[j]] = j; add(ad, M + j); P[j] /= 2; } for (int i = 0; i < M; i++) { for (auto x : S[0][i]) { S[1][x.F / M].PB(x); } S[0][i].clear(); } int tmp = -1; LL pre = -1; for (int i = 0; i < M; i++) { for (auto x : S[1][i]) { if (x.F != pre) { pre = x.F; tmp++; } if (x.S <= M) { T[x.S] = tmp; } else { A[x.S - M] = tmp; } } S[1][i].clear(); } st /= 2; } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); //n = 5e5; //m = 5e5; vector<PII> V; //srand(1); for (int i = 0; i < n; i++) { int a; scanf("%d", &a); //a = rand() % 1000; V.PB(MP(a, i)); } for (int i = 2; i <= m; i++) { int pos, val; scanf("%d%d", &pos, &val); //pos = rand() % n + 1; //val = rand() % 1000; pos--; P[i] = M + pos; V.PB(MP(val, M + i)); } sort(ALL(V)); int tmp = 0, pre = -1; for (auto x : V) { if (x.F != pre) { tmp++; pre = x.F; } if (x.S <= M) { T[x.S + M] = tmp; } else { A[x.S - M] = tmp; } } solve(); vector<PII> X; X.PB(MP(T[1], 1)); for (int i = 2; i <= m; i++) { X.PB(MP(A[i], i)); //debug("%d %d\n", A[i], i); } sort(ALL(X)); for (auto x : X) { printf("%d ", x.S); } printf("\n"); return 0; } |