首页 > 代码库 > Hdu3308LCIS线段树

Hdu3308LCIS线段树

 

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn = 55555;int color[maxn << 2];int sum[maxn << 2], lsum[maxn << 2], rsum[maxn << 2];void down(int rt, int m){    if (~color[rt]){        color[rt << 1] = color[rt << 1 | 1] = color[rt];        sum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = color[rt] ? 0: (m - (m >> 1));        sum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = color[rt] ? 0 : (m >> 1);        color[rt] = -1;    }}void up(int rt, int m){    lsum[rt] = lsum[rt << 1];    rsum[rt] = rsum[rt << 1 | 1];    if (lsum[rt] == (m - (m >> 1))) lsum[rt] += lsum[rt << 1 | 1];    if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];    sum[rt] = max(rsum[rt << 1] + lsum[rt << 1 | 1], max(sum[rt << 1], sum[rt << 1 | 1]));}void build(int l, int r, int rt){    color[rt] = -1; sum[rt] = lsum[rt] = rsum[rt] = r - l + 1;    if (l == r) return;    int mid = (l + r) >> 1;    build(lson);    build(rson);}void update(int L, int R, int add, int l, int r, int rt){    if (L <= l&&r <= R){        sum[rt] = lsum[rt] = rsum[rt] = add ? 0 : r - l + 1;        color[rt] = add; return;    }    down(rt, r - l + 1);    int mid=(l + r) >> 1;    if (L <=mid) update(L, R, add, lson);    if (R>mid) update(L, R, add, rson);    up(rt, r - l + 1);}int ask(int key, int l, int r, int rt){    if (l == r) return l;    int mid = (l + r) >> 1;    down(rt, r - l + 1);    if (sum[rt << 1] >= key) return ask(key, lson);    else if (rsum[rt << 1] + lsum[rt << 1 | 1] >= key) return mid - rsum[rt << 1] + 1;    return ask(key, rson);}int main(){    int n, m;    int a, b, c;    while (cin >> n >> m){        build(1, n, 1);        for (int i = 0; i < m; i++){            scanf("%d",&a);            if (a == 1){                scanf("%d",&b);                if(b>sum[1]){                    printf("0\n");continue;                }                int pos = ask(b, 1, n, 1);                printf("%d\n", pos);                update(pos, pos + b - 1,1, 1, n, 1);            }            else{                scanf("%d%d",&b,&c);                update(b, b+c-1, 0, 1, n, 1);            }        }    }    return 0;}