#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 5e5 + 7; const int MOD = 1e9 + 7; const LL p = 1000033; const int MAX = 1 << 19; LL po[MAX + 7]; LL init; struct node { LL val; node *left = nullptr, *right = nullptr; int l, r; node(int L, int R, LL v) : val(v), l(L), r(R) {} }; vector < node* > roots; void check(node *root) { int m = (root -> l + root -> r) / 2; if(root -> left == nullptr) root -> left = new node(root -> l, m, init); if(root -> right == nullptr) root -> right = new node(m + 1, root -> r, init); } node *add(node *root, int id, LL v) { node *res = new node(*root); if(res -> l == res -> r) { res -> val = v; return res; } int m = (res -> l + res -> r) / 2; check(res); if(id <= m) res -> left = add(res -> left, id, v); else res -> right = add(res -> right, id, v); res->val = (res->left->val + res->right->val * po[res->r - m]) % MOD; return res; } void update(node *res, int id, LL v) { if(res -> l == res -> r) { res -> val = v; return; } int m = (res -> l + res -> r) / 2; check(res); if(id <= m) update(res -> left, id, v); else update(res -> right, id, v); res->val = (res->left->val + res->right->val * po[res->r - m]) % MOD; } bool compare(node *A, node *B, const int &x, const int &y) { if(A->val == B->val) return x < y; if(A->l == A->r) return A->val < B->val; if(A->left->val != B->left->val) return compare(A->left, B->left, x, y); else return compare(A->right, B->right, x, y); } bool cmp(const int &A, const int &B) { return compare(roots[A], roots[B], A, B); } int n, m, T[N]; vector < PII > Q, mp; vector < int > ans; int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { scanf("%d", &T[i]); mp.push_back({T[i], i}); } for(int i = 1; i < m; i++) { int pos, x; scanf("%d%d", &pos, &x); mp.push_back({x, n + i}); Q.push_back({pos, x}); } roots.push_back(new node(0, MAX - 1, 0)); sort(all(mp)); int cnt = 0; for(int i = 0; i < sz(mp); i++) { if(i == 0 || mp[i].first != mp[i - 1].first) ++cnt; if(mp[i].second <= n) T[mp[i].second] = cnt; else Q[mp[i].second - 1 - n].second = cnt; } po[1] = p; for(int i = 2; i <= MAX; i *= 2) po[i] = po[i / 2] * po[i / 2] % MOD; for(int i = 1; i <= n; i++) { update(roots[0], i - 1, T[i]); } ans.push_back(0); roots.resize(m + 1); for(int i = 0; i < m - 1; i++) { roots[i + 1] = add(roots[i], Q[i].first - 1, Q[i].second); ans.push_back(i + 1); } sort(all(ans), cmp); for(int i = 0; i < m; i++) printf("%d ", ans[i] + 1); 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 5e5 + 7; const int MOD = 1e9 + 7; const LL p = 1000033; const int MAX = 1 << 19; LL po[MAX + 7]; LL init; struct node { LL val; node *left = nullptr, *right = nullptr; int l, r; node(int L, int R, LL v) : val(v), l(L), r(R) {} }; vector < node* > roots; void check(node *root) { int m = (root -> l + root -> r) / 2; if(root -> left == nullptr) root -> left = new node(root -> l, m, init); if(root -> right == nullptr) root -> right = new node(m + 1, root -> r, init); } node *add(node *root, int id, LL v) { node *res = new node(*root); if(res -> l == res -> r) { res -> val = v; return res; } int m = (res -> l + res -> r) / 2; check(res); if(id <= m) res -> left = add(res -> left, id, v); else res -> right = add(res -> right, id, v); res->val = (res->left->val + res->right->val * po[res->r - m]) % MOD; return res; } void update(node *res, int id, LL v) { if(res -> l == res -> r) { res -> val = v; return; } int m = (res -> l + res -> r) / 2; check(res); if(id <= m) update(res -> left, id, v); else update(res -> right, id, v); res->val = (res->left->val + res->right->val * po[res->r - m]) % MOD; } bool compare(node *A, node *B, const int &x, const int &y) { if(A->val == B->val) return x < y; if(A->l == A->r) return A->val < B->val; if(A->left->val != B->left->val) return compare(A->left, B->left, x, y); else return compare(A->right, B->right, x, y); } bool cmp(const int &A, const int &B) { return compare(roots[A], roots[B], A, B); } int n, m, T[N]; vector < PII > Q, mp; vector < int > ans; int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { scanf("%d", &T[i]); mp.push_back({T[i], i}); } for(int i = 1; i < m; i++) { int pos, x; scanf("%d%d", &pos, &x); mp.push_back({x, n + i}); Q.push_back({pos, x}); } roots.push_back(new node(0, MAX - 1, 0)); sort(all(mp)); int cnt = 0; for(int i = 0; i < sz(mp); i++) { if(i == 0 || mp[i].first != mp[i - 1].first) ++cnt; if(mp[i].second <= n) T[mp[i].second] = cnt; else Q[mp[i].second - 1 - n].second = cnt; } po[1] = p; for(int i = 2; i <= MAX; i *= 2) po[i] = po[i / 2] * po[i / 2] % MOD; for(int i = 1; i <= n; i++) { update(roots[0], i - 1, T[i]); } ans.push_back(0); roots.resize(m + 1); for(int i = 0; i < m - 1; i++) { roots[i + 1] = add(roots[i], Q[i].first - 1, Q[i].second); ans.push_back(i + 1); } sort(all(ans), cmp); for(int i = 0; i < m; i++) printf("%d ", ans[i] + 1); return 0; } |