zoukankan      html  css  js  c++  java
  • BZOJ1588 [HNOI2002] 营业额统计

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588

    又是一道Treap模版题……总做模版题不好……

    另外牢记:BZOJ上用srand(time(0))会RE!

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <ctime>
     6 #define rep(i,l,r) for(int i=l; i<=r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 typedef long long ll;
     9 typedef unsigned long long ull;
    10 using namespace std;
    11 const int INF = 0x3f3f3f3f;
    12 const int maxn = 50010;
    13 struct node{
    14     int v,w,l,r,rnd,size;
    15 }t[maxn];
    16 int n,x,t1,t2,tot=0,root=0,ans=0;
    17 inline int read(){
    18     int ans = 0, f = 1;
    19     char c = getchar();
    20     while (!isdigit(c)){
    21         if (c == '-') f = -1;
    22         c = getchar();
    23     }
    24     while (isdigit(c)){
    25         ans = ans * 10 + c - '0';
    26         c = getchar();
    27     }
    28     return ans * f;
    29 }
    30 inline void update(int w){
    31     t[w].size = t[t[w].l].size + t[t[w].r].size + t[w].w;
    32 }
    33 void rotl(int &w){
    34     int k = t[w].r; t[w].r = t[k].l; t[k].l = w;
    35     update(w); update(k); w = k;
    36 }
    37 void rotr(int &w){
    38     int k = t[w].l; t[w].l = t[k].r; t[k].r = w;
    39     update(w); update(k); w = k;
    40 }
    41 void insert(int x,int &w){
    42     if (!w){
    43         w = ++tot; t[w].v = x; t[w].size = t[w].w = 1;
    44         t[w].rnd = rand(); t[w].l = t[w].r = 0; return;
    45     }
    46     t[w].size++; if (t[w].v == x) t[w].w++;
    47     else if (x < t[w].v){
    48         insert(x,t[w].l);
    49         if (t[t[w].l].rnd < t[w].rnd) rotr(w);
    50     }
    51     else{
    52         insert(x,t[w].r);
    53         if (t[t[w].r].rnd < t[w].rnd) rotl(w);
    54     }
    55 }
    56 void bef(int x,int w){
    57     if (!w) return;
    58     if (t[w].v <= x){
    59         t1 = t[w].v;
    60         bef(x,t[w].r);
    61     }
    62     else bef(x,t[w].l);
    63 }
    64 void aft(int x,int w){
    65     if (!w) return;
    66     if (t[w].v >= x){
    67         t2 = t[w].v;
    68         aft(x,t[w].l);
    69     }
    70     else aft(x,t[w].r);
    71 }
    72 int main(){
    73     n = read();
    74     rep(i,1,n){
    75         if (scanf("%d",&x) == EOF) x = 0;
    76         t1 = -INF; t2 = INF;
    77         bef(x,root); aft(x,root);
    78         if (i == 1) ans += x;
    79         else ans += min(x-t1,t2-x);
    80         insert(x,root);
    81     }
    82     printf("%d
    ",ans);
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    WPF 模拟UI 键盘录入
    rabbitmq使用dead letter机制来进行retry
    工厂设计模式
    python 内置速度最快算法(堆排)
    简单工厂设计模式
    杂类
    MorkDown 常用语法总结
    使用python列表推导式进行99乘法表
    linux 命令free -m 命令结果分析
    理解记忆三种常见字符编码:ASCII, Unicode,UTF-8
  • 原文地址:https://www.cnblogs.com/jimzeng/p/bzoj1588.html
Copyright © 2011-2022 走看看