码迷,mamicode.com
首页 > 其他好文 > 详细

PPMM

时间:2020-02-20 19:52:12      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:turn   printf   getchar   while   oid   amp   print   ++   pen   

代码

#include<cstdio>
using namespace std;

const int N = 1000000 , INF = 2e9;
int n , m , x , head = 1 , tail , p , s;
char opt[5];

inline int read()
{
    char ch = getchar();
    int res = 0 , f = 1;
    for(; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -f;
    for(; ch >= '0' && ch <= '9'; ch = getchar()) res = (res << 3) + (res << 1) + ch - '0';
    return res * f;
}

inline int my_m(int x , int y , int o)
{
    if (!o) return x > y ? x : y;
    if (o) return x < y ? x : y;
}

struct tree{
    int top;
    int tr[(N << 2) + 5][3];
    inline void change(int l , int r , int k , int x , int v)
    {
        if (l == r && l == x)
        {
            tr[k][0] = tr[k][1] = v;
            return;
        }
        int mid = (l + r) >> 1;
        if (x <= mid) change(l , mid , k << 1 , x , v);
        else change(mid + 1 , r , k << 1 | 1 , x , v);
        tr[k][0] = my_m(tr[k << 1][0] , tr[k << 1 | 1][0] , 0);//Max
        tr[k][1] = my_m(tr[k << 1][1] , tr[k << 1 | 1][1] , 1);//Min
    }
    inline int query(int l , int r , int k , int x , int y , int o) 
    {
        if (l >= x && r <= y) return tr[k][o];
        int mid = (l + r) >> 1 , res = INF;
        if (!o) res = -res;
        if (x <= mid) res = my_m(res , query(l , mid , k << 1 , x , y , o) , o);
        if (y > mid) res = my_m(res , query(mid + 1 , r , k << 1 | 1 , x , y , o) , o);
        return res; 
    }
}f;

int main()
{
//  freopen("PPMM.in" , "r" , stdin);
//  freopen("PPMM.out" , "w" , stdout);
    n = read();
    m = n;
    while (m--)
    {
        opt[1] = getchar();
        while (opt[1] != 'P' && opt[1] != 'M') opt[1] = getchar();
        opt[2] = getchar() , opt[3] = getchar();;
        if (opt[1] == 'P' && opt[2] == 'U') 
        {
            x = read();
            if (p == 1) x = -x;
            tail++ , s++;
            f.change(1 , n , 1 , tail , x);
        }
        if (s == 0) continue;
        if (opt[1] == 'P' && opt[2] == 'O') head++ , s--;
        else if (opt[1] == 'M' && opt[2] == 'I') p ^= 1;
        else
        if (opt[1] == 'M' && opt[2] == 'A')
        {
            if (p == 0) printf("%d\n" , f.query(1 , n , 1 , head , tail , 0));//Max
            else printf("%d\n" , -1 * f.query(1 , n , 1 , head , tail , 1));//Min
        }
    }
}

PPMM

标签:turn   printf   getchar   while   oid   amp   print   ++   pen   

原文地址:https://www.cnblogs.com/leiyuanze/p/12337021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!