#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; //https://ideone.com/8EKuui const int BASE = 1<<19; const int RAZ = 50; int seg[RAZ * BASE]; int L[RAZ * BASE], R[RAZ * BASE]; int NEW = 2; LL mod = 2000007161; void build(int id = 1, int l = 1, int r = BASE) { seg[id] = 0; if (l == r)return; int mid = (l + r) >> 1; L[id] = NEW++; R[id] = NEW++; build(L[id], l, mid); build(R[id], mid + 1, r); } int update(int x , LL z, int id, int l = 1, int r = BASE) { if (l > x || r < x) return id; int ID = NEW++;// assert(NEW <= RAZ * BASE); if (l == r) { seg[ID] = ((r - l + 1) * z + seg[id]) % mod; L[ID] = L[id]; R[ID] = R[id]; return ID; } int mid = (l + r) >> 1; L[ID] = update(x, z, L[id], l, mid); R[ID] = update(x, z, R[id], mid + 1, r); seg[ID] = (seg[L[ID]] + seg[R[ID]]) % mod; return ID; } LL invpot[BASE]; int query(int id1, int id2, int l = 1, int r = BASE) { if (l == r) { LL v1 = ((seg[id1] * invpot[l] % mod + mod) % mod); LL v2 = ((seg[id2] * invpot[l] % mod + mod) % mod); return v1 - v2; } int mid = (l + r) >> 1; if(seg[L[id1]] == seg[L[id2]]) return query(R[id1], R[id2], mid+1, r); else return query(L[id1], L[id2], l, mid); } bool cmp(PII a, PII b) { int x = query(a.f, b.f); if(x != 0)return x < 0; return a.s < b.s; } LL root[BASE]; LL p = 1e9 + 7; LL pot[BASE]; int act[BASE]; LL inv(LL a, LL m); int main() { pot[0] = 1; FOR(i, 1, BASE-1)pot[i] = pot[i-1] * p % mod; //invpot[BASE-1] = inv(pot[BASE-1], mod); invpot[BASE-1] = 568391732; assert(invpot[BASE-1] * pot[BASE-1] % mod == 1); FORD(i, BASE-2, 0) invpot[i] = invpot[i+1] * p % mod; assert(invpot[BASE-100] * pot[BASE-100] % mod == 1); // FOR(i, 0, BASE-1)pot[i] = invpot[i] = 1; int n, m; // n = m = 5e5; scanf("%d%d", &n, &m); build(); root[1] = 1; FOR(i, 1, n) { int a; // a = rand() % 1000000000; scanf("%d", &a); act[i] = a; root[1] = update(i, +act[i] * pot[i] % mod, root[1]); } VPII pom = {MP(root[1], 1)}; FOR(i, 2, m) { int a, b; // a = rand() % n + 1; // b = rand() % 1000000000; scanf("%d%d", &a, &b); root[i] = update(a, (b-act[a]) * pot[a] % mod, root[i-1]); act[a] = b; pom.PB(MP(root[i], i)); } sort(ALL(pom), cmp); for(auto i : pom) { printf("%d ", i.s); } puts(""); // cerr<<NEW/BASE<<endl; } LL inv(LL a, LL m) { return a < 2 ? a : ((1 - m * 1ll * inv(m % a, a)) / a % m + m) % m; }
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 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; //https://ideone.com/8EKuui const int BASE = 1<<19; const int RAZ = 50; int seg[RAZ * BASE]; int L[RAZ * BASE], R[RAZ * BASE]; int NEW = 2; LL mod = 2000007161; void build(int id = 1, int l = 1, int r = BASE) { seg[id] = 0; if (l == r)return; int mid = (l + r) >> 1; L[id] = NEW++; R[id] = NEW++; build(L[id], l, mid); build(R[id], mid + 1, r); } int update(int x , LL z, int id, int l = 1, int r = BASE) { if (l > x || r < x) return id; int ID = NEW++;// assert(NEW <= RAZ * BASE); if (l == r) { seg[ID] = ((r - l + 1) * z + seg[id]) % mod; L[ID] = L[id]; R[ID] = R[id]; return ID; } int mid = (l + r) >> 1; L[ID] = update(x, z, L[id], l, mid); R[ID] = update(x, z, R[id], mid + 1, r); seg[ID] = (seg[L[ID]] + seg[R[ID]]) % mod; return ID; } LL invpot[BASE]; int query(int id1, int id2, int l = 1, int r = BASE) { if (l == r) { LL v1 = ((seg[id1] * invpot[l] % mod + mod) % mod); LL v2 = ((seg[id2] * invpot[l] % mod + mod) % mod); return v1 - v2; } int mid = (l + r) >> 1; if(seg[L[id1]] == seg[L[id2]]) return query(R[id1], R[id2], mid+1, r); else return query(L[id1], L[id2], l, mid); } bool cmp(PII a, PII b) { int x = query(a.f, b.f); if(x != 0)return x < 0; return a.s < b.s; } LL root[BASE]; LL p = 1e9 + 7; LL pot[BASE]; int act[BASE]; LL inv(LL a, LL m); int main() { pot[0] = 1; FOR(i, 1, BASE-1)pot[i] = pot[i-1] * p % mod; //invpot[BASE-1] = inv(pot[BASE-1], mod); invpot[BASE-1] = 568391732; assert(invpot[BASE-1] * pot[BASE-1] % mod == 1); FORD(i, BASE-2, 0) invpot[i] = invpot[i+1] * p % mod; assert(invpot[BASE-100] * pot[BASE-100] % mod == 1); // FOR(i, 0, BASE-1)pot[i] = invpot[i] = 1; int n, m; // n = m = 5e5; scanf("%d%d", &n, &m); build(); root[1] = 1; FOR(i, 1, n) { int a; // a = rand() % 1000000000; scanf("%d", &a); act[i] = a; root[1] = update(i, +act[i] * pot[i] % mod, root[1]); } VPII pom = {MP(root[1], 1)}; FOR(i, 2, m) { int a, b; // a = rand() % n + 1; // b = rand() % 1000000000; scanf("%d%d", &a, &b); root[i] = update(a, (b-act[a]) * pot[a] % mod, root[i-1]); act[a] = b; pom.PB(MP(root[i], i)); } sort(ALL(pom), cmp); for(auto i : pom) { printf("%d ", i.s); } puts(""); // cerr<<NEW/BASE<<endl; } LL inv(LL a, LL m) { return a < 2 ? a : ((1 - m * 1ll * inv(m % a, a)) / a % m + m) % m; } |