Advertisement
juaniisuar

P1Selectivo2015 (Segmentarbol pajero)

Oct 15th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cstring>
  5. #define forn(i,n) for(int i=0; i<n; i++)
  6.  
  7. using namespace std;
  8.  
  9. const int maxN = 1e6;
  10. const int magia = (1<<(20+1));
  11. int L, total;
  12. queue<int> pos[256];
  13. char orig[maxN], modif[maxN];
  14. int ST[magia], lazy[magia];
  15.  
  16. void build(){
  17. memset(ST, 0, sizeof ST);
  18. memset(lazy, 0, sizeof lazy);
  19. }
  20.  
  21. void update(int index, int s, int e, int qs, int qe, int v){
  22. if(s>e) return;
  23.  
  24. if(lazy[index]){
  25. ST[index] += lazy[index] * (s-e+1);
  26. if(s!=e){
  27. lazy[index*2] += lazy[index];
  28. lazy[index*2+1] += lazy[index];
  29. }
  30. lazy[index] = 0;
  31. }
  32.  
  33. if(s>qe || e<qs) return;
  34. if(s>=qs && e<=qe){
  35. ST[index] += v * (s-e+1);
  36. if(s!=e){
  37. lazy[index*2] += v;
  38. lazy[index*2+1] += v;
  39. }
  40. return;
  41. }
  42.  
  43. int mid = (s+e)/2;
  44. update(index*2, s, mid, qs, qe, v);
  45. update(index*2+1, mid+1, e, qs, qe, v);
  46. }
  47.  
  48. int query(int index, int s, int e, int qs, int qe){
  49. if(s>e) return 0;
  50. if(lazy[index]){
  51. ST[index] += lazy[index] * (s-e+1);
  52. if(s!=e){
  53. lazy[index*2] += lazy[index];
  54. lazy[index*2+1] += lazy[index];
  55. }
  56. lazy[index] = 0;
  57. }
  58. if(s>qe || e<qs) return 0;
  59. if(s>=qs && e<=qe){
  60. return ST[index];
  61. }
  62.  
  63. int mid = (s+e)/2;
  64. return query(index*2, s, mid, qs, qe) + query(index*2+1, mid+1, e, qs, qe);
  65. }
  66.  
  67. int main(){
  68. cin.getline(orig,maxN);
  69. cin.getline(modif,maxN);
  70. L = strlen(orig);
  71. forn(i,L)
  72. pos[ int(orig[i]) ].push(i);
  73. build();
  74. forn(i,L){
  75. int j = pos[ int(modif[i]) ].front();
  76. pos[ int(modif[i]) ].pop();
  77. total += j - i + query(1, 0, L-1, j, j);
  78. update(1, 0, L-1, 0, j, 1);
  79. }
  80. cout << total << endl;
  81.  
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement