• Buy Tickets 【POJ


    题目链接

      有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val。

      这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而改变位置,所以我们可以尝试着先放后面的数,再处理前面的数,最后一个数一定是放在(pos[N] + 1)这个位置上的,而再后面的数呢,如果本来是放在pos[N]前面的,可能还是在pos[N]的前面,跟什么有关呢?可以多举几组例子,不难发现,假如这个数原先要放在第pos(i)的位置后面,现在呢在pos(i)及其前面已经没有那么多的位置了,说明了这些位置被其他的人给占有了,也相对应的,它的位置也是要相对后移,因为原本在它之后的操作如果有在它之前位置上的话,也就是会让它的位置向后挪动的道理,而在它之前操作的是不影响的。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <limits>
     8 #include <vector>
     9 #include <stack>
    10 #include <queue>
    11 #include <set>
    12 #include <map>
    13 #define lowbit(x) ( x&(-x) )
    14 #define pi 3.141592653589793
    15 #define e 2.718281828459045
    16 #define INF 0x3f3f3f3f
    17 #define HalF (l + r)>>1
    18 #define lsn rt<<1
    19 #define rsn rt<<1|1
    20 #define Lson lsn, l, mid
    21 #define Rson rsn, mid+1, r
    22 #define QL Lson, ql, qr
    23 #define QR Rson, ql, qr
    24 #define myself rt, l, r
    25 using namespace std;
    26 typedef unsigned long long ull;
    27 typedef long long ll;
    28 const int maxN = 2e5 + 7;
    29 int tree[maxN<<2], N, pos[maxN], val[maxN], ans[maxN];
    30 inline void pushup(int rt) { tree[rt] = tree[lsn] + tree[rsn]; }
    31 inline void buildTree(int rt, int l, int r)
    32 {
    33     if(l == r) { tree[rt] = 1; return; }
    34     int mid = HalF;
    35     buildTree(Lson);
    36     buildTree(Rson);
    37     pushup(rt);
    38 }
    39 inline void update(int rt, int l, int r, int qx, int num)
    40 {
    41     if(l == r)
    42     {
    43         tree[rt] = 0;
    44         ans[l] = num;
    45         return;
    46     }
    47     int mid = HalF;
    48     if(qx <= tree[lsn]) update(Lson, qx, num);
    49     else update(Rson, qx - tree[lsn], num);
    50     pushup(rt);
    51 }
    52 int main()
    53 {
    54     while(scanf("%d", &N) != EOF)
    55     {
    56         buildTree(1, 1, N);
    57         for(int i=1; i<=N; i++) scanf("%d%d", &pos[i], &val[i]);
    58         for(int i=N; i>=1; i--) update(1, 1, N, pos[i] + 1, val[i]);
    59         for(int i=1; i<=N; i++) printf("%d%c", ans[i], i == N ? '
    ' : ' ');
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    VS2010-MFC(对话框:模态对话框及其弹出过程)
    VS2010-MFC(对话框:设置对话框控件的Tab顺序)
    VS2010-MFC(对话框:为控件添加消息处理函数)
    VS2010-MFC(对话框:创建对话框类和添加控件变量)
    manacher算法模板
    [洛谷P1017] 进制转换
    [洛谷P1126] 机器人搬重物
    [洛谷P1032] 字串变换
    [POI2005]SAM-Toy Cars
    [洛谷P1528] 切蛋糕
  • 原文地址:https://www.cnblogs.com/WuliWuliiii/p/10834501.html
Copyright © 2020-2023  润新知