#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> #include <unordered_set> #include <unordered_map> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T> void maxi(T &a, const T &b) { a = max(a,b) ; } template<class T> void mini(T &a, const T &b) { a = min(a,b) ; } template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 1 #endif //////////////////////////////////////////////////////////////////////////////// const int Q=1000000021, P=1000000097 ; struct Node { int v ; // wartosc oryginalna (w lisciu) lub hash calej podtablicy Node *l, *r ; Node(int vv) : v(vv), l(NULL), r(NULL) {} Node(Node *ll, Node *rr, LL B) : l(ll), r(rr) { v = (l->v*B + r->v)%P ; } bool isLeaf() { assert((l==NULL)==(r==NULL)) ; return l==NULL && r==NULL ; } void show() { if(isLeaf()) cout << v << " " ; else { l->show() ; r->show() ; } } } ; const int MAXN=(1<<19)+100 ; int t[MAXN] ; // Buduje tablice w formie drzewa, kazdy wierzcholek hashuje swoje poddrzewo Node* buildArray(int n) { vector<Node*> nodes, tmp ; for(int i=1 ; i<=n ; i++) nodes.PB(new Node(t[i])) ; for(LL B=Q ; nodes.size()>1 ; B=(B*B)%P) { assert(!(nodes.size()&1)) ; tmp=nodes ; nodes.clear() ; for(int i=0 ; i+1<tmp.size() ; i+=2) nodes.PB(new Node(tmp[i], tmp[i+1], B)) ; } return nodes[0] ; } // Aktualizuje tablice bez niszczenia jej, tworzac nowa sciezke do korzenia pair<Node*, LL> updateArray(Node *node, int l, int r, int i, int val) { assert(l<=i && i<=r) ; if(l==r) return MP(new Node(val), Q) ; else { int sr=(l+r)/2 ; if(i<=sr) { auto tmp = updateArray(node->l, l, sr, i, val) ; Node *ret = new Node(tmp.FI, node->r, tmp.SE) ; return MP(ret, (tmp.SE*tmp.SE)%P) ; } else { auto tmp = updateArray(node->r, sr+1, r, i, val) ; Node *ret = new Node(node->l, tmp.FI, tmp.SE) ; return MP(ret, (tmp.SE*tmp.SE)%P) ; } } } Node *roots[MAXN] ; // Korzystajac z hashy wyszukuje pierwszy indeks, na ktorym tablice sie roznia struct sortByArrays { bool operator()(const int &A, const int &B) const { Node *a=roots[A], *b=roots[B] ; assert(a->isLeaf()==b->isLeaf()) ; while(!a->isLeaf()) { assert(a->isLeaf()==b->isLeaf()) ; if(a->l->v != b->l->v) { a = a->l ; b = b->l ; } else { a = a->r ; b = b->r ; } } assert(a->isLeaf()==b->isLeaf()) ; if(a->v != b->v) return a->v < b->v ; else return A<B ; } } ; int ret[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i, p, x ; cin >> n >> m ; //n = 5e5 ; m=5e5 ; for(i=1 ; i<=n ; i++) { cin >> t[i] ; //t[i]=i ; } int N=1 ; while(N<=n) N*=2 ; assert(n<=N && N<=MAXN) ; n=N ; roots[1] = buildArray(n) ; DEBUG("builded") ; for(i=2 ; i<=m ; i++) { cin >> p >> x ; //p = m+1-i ; x = m-i/2 ; roots[i] = updateArray(roots[i-1], 1,n, p, x).FI ; } DEBUG("updated") ; REP(i, m) ret[i]=i+1 ; sort(ret, ret+m, sortByArrays()) ; DEBUG("sorted") ; REP(i, m) cout << ret[i] << " " ; cout << endl ; }
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 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> #include <unordered_set> #include <unordered_map> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T> void maxi(T &a, const T &b) { a = max(a,b) ; } template<class T> void mini(T &a, const T &b) { a = min(a,b) ; } template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 1 #endif //////////////////////////////////////////////////////////////////////////////// const int Q=1000000021, P=1000000097 ; struct Node { int v ; // wartosc oryginalna (w lisciu) lub hash calej podtablicy Node *l, *r ; Node(int vv) : v(vv), l(NULL), r(NULL) {} Node(Node *ll, Node *rr, LL B) : l(ll), r(rr) { v = (l->v*B + r->v)%P ; } bool isLeaf() { assert((l==NULL)==(r==NULL)) ; return l==NULL && r==NULL ; } void show() { if(isLeaf()) cout << v << " " ; else { l->show() ; r->show() ; } } } ; const int MAXN=(1<<19)+100 ; int t[MAXN] ; // Buduje tablice w formie drzewa, kazdy wierzcholek hashuje swoje poddrzewo Node* buildArray(int n) { vector<Node*> nodes, tmp ; for(int i=1 ; i<=n ; i++) nodes.PB(new Node(t[i])) ; for(LL B=Q ; nodes.size()>1 ; B=(B*B)%P) { assert(!(nodes.size()&1)) ; tmp=nodes ; nodes.clear() ; for(int i=0 ; i+1<tmp.size() ; i+=2) nodes.PB(new Node(tmp[i], tmp[i+1], B)) ; } return nodes[0] ; } // Aktualizuje tablice bez niszczenia jej, tworzac nowa sciezke do korzenia pair<Node*, LL> updateArray(Node *node, int l, int r, int i, int val) { assert(l<=i && i<=r) ; if(l==r) return MP(new Node(val), Q) ; else { int sr=(l+r)/2 ; if(i<=sr) { auto tmp = updateArray(node->l, l, sr, i, val) ; Node *ret = new Node(tmp.FI, node->r, tmp.SE) ; return MP(ret, (tmp.SE*tmp.SE)%P) ; } else { auto tmp = updateArray(node->r, sr+1, r, i, val) ; Node *ret = new Node(node->l, tmp.FI, tmp.SE) ; return MP(ret, (tmp.SE*tmp.SE)%P) ; } } } Node *roots[MAXN] ; // Korzystajac z hashy wyszukuje pierwszy indeks, na ktorym tablice sie roznia struct sortByArrays { bool operator()(const int &A, const int &B) const { Node *a=roots[A], *b=roots[B] ; assert(a->isLeaf()==b->isLeaf()) ; while(!a->isLeaf()) { assert(a->isLeaf()==b->isLeaf()) ; if(a->l->v != b->l->v) { a = a->l ; b = b->l ; } else { a = a->r ; b = b->r ; } } assert(a->isLeaf()==b->isLeaf()) ; if(a->v != b->v) return a->v < b->v ; else return A<B ; } } ; int ret[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i, p, x ; cin >> n >> m ; //n = 5e5 ; m=5e5 ; for(i=1 ; i<=n ; i++) { cin >> t[i] ; //t[i]=i ; } int N=1 ; while(N<=n) N*=2 ; assert(n<=N && N<=MAXN) ; n=N ; roots[1] = buildArray(n) ; DEBUG("builded") ; for(i=2 ; i<=m ; i++) { cin >> p >> x ; //p = m+1-i ; x = m-i/2 ; roots[i] = updateArray(roots[i-1], 1,n, p, x).FI ; } DEBUG("updated") ; REP(i, m) ret[i]=i+1 ; sort(ret, ret+m, sortByArrays()) ; DEBUG("sorted") ; REP(i, m) cout << ret[i] << " " ; cout << endl ; } |