Advertisement
jonathanasdf

Permutacja (SPOJ AE5B2)

Jan 10th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int MAXN = 262144;
  6. // stores binary tree in [1..2*MAXN-1], the leaves are [MAXN..2*MAXN-1]
  7. // assumes MAXN is a power of 2. functions act on [x..y]
  8. const int INF = 0x3f3f3f3f;
  9. int a[2*MAXN], b[2*MAXN];
  10. inline int update(int y, int v, int i=1, int l=0, int r=MAXN-1) {
  11.     if (y<l) return a[i];
  12.     if (l==r) return a[i] += v;
  13.     if (r <= y) {
  14.         b[i] += v;
  15.         return a[i]+b[i];
  16.     }
  17.     int m = (l+r)/2;
  18.     if (b[i]) {
  19.         b[2*i] += b[i];
  20.         b[2*i+1] += b[i];
  21.         b[i] = 0;
  22.     }
  23.     if(y < m) update(y,v,2*i,l,m);
  24.     else {
  25.         b[2*i] += v;
  26.         update(y,v,2*i+1,m+1,r);
  27.     }
  28.     a[i] = min(a[2*i]+b[2*i], a[2*i+1]+b[2*i+1]);
  29. }
  30. int main() {
  31.     int n; scanf("%d", &n);
  32.     memset(a, INF, sizeof a); memset(b, 0, sizeof b);
  33.     for (int i=0; i < n; i++) a[MAXN+i] = i-n;
  34.     for (int i=MAXN-1; i >= 1; i--) a[i] = min(a[2*i], a[2*i+1]);
  35.        
  36.     int z[n+1];
  37.     for (int i=1; i <= n; i++) {
  38.         scanf("%d", z+i);
  39.         update(z[i]-1, 1);
  40.     }
  41.  
  42.     printf("%s\n", a[1] < 0 ? "NIE" : "TAK");
  43.     int m; scanf("%d", &m);
  44.     while(m--) {
  45.         int j, w; scanf("%d %d", &j, &w);
  46.         update(z[j]-1, -1);
  47.         update(w-1, 1);
  48.         z[j] = w;
  49.         printf("%s\n", a[1] < 0 ? "NIE" : "TAK");
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement