No.7 线段树

来源:互联网 发布:北京达内java 编辑:程序博客网 时间:2024/04/28 06:38

crf 出生的第三秒

1 Description
crf 是一个天才。
他出生的第三秒,偶然看到了一张简谱,可能是医院的工作人员闲的没事的时候创作的。
当时的他并不能理解简谱和音乐的关系(但这并不妨碍他今后成为世界著名小提琴演奏家),他只是单纯的看着这张写满1-7 的纸,觉得非常缺乏美感。他认为有序的数列才是最美的。
但毕竟他才出生第三秒,他的mogic 也只是初步显现,他可以改变一段连续的音符的顺序,但他的mogic 不足以一次改掉整张简谱。
这是crf 这辈子第一次犯蠢,也是唯一一次犯蠢。他尝试着将一段一段的音符调成升序,但他并没有意识到就算这样调了也不能让整个序列变得有序。
crf 的mogic 可以选出一段连续的音符,将它们重新排为升序之后再放回,他尝试了一些这样的操作之后发现并没有将序列变得有序就放弃了。
后来的程序员为了纪念伟大的crf,将他唯一犯蠢的这段经历重新变成了一道算法题。
虽然这道题大部分题面都是对crf 的赞美,但可能你们不敢兴趣所以就直接上题吧。
给定一个长度为n 的只有1-7 的数字的序列,有q 次操作,每次操作是将一段连续的数字拿出来,排成升序或者降序后放回。求最终序列。

2 Input
输入的第一行为两个整数n; q,表示序列的长度和操作的数量。
接下来一行有n 个整数,表示crf 的数字序列。
接下来q 行,每行有三个整数l; r; op,l; r 含义如题目中所示,op 为0 表示升序,为1 表示降序。

3 Output
输出共1 行,包含n 个整数,表示最终的序列,中间用空格隔开。

Sample Input
5 2
3 1 2 5 4
1 3 0
2 5 1
Sample Output
1 5 4 3 2

4 Hint
对于30% 的数据,保证1 <= n, q <= 1000。
对于100% 的数据,保证1 <= n , q <= 100000。

思路:
线段树,因为只有7种数字,用线段树维护每一段的7种数字分别有多少,每次操作即为一次查询和七次覆盖。

#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;const int maxn = 100010;int a[maxn], n, T, cnt[20], sum[20];inline const int read(){    register int f = 1, x = 0;    register char ch = getchar();    while(ch < '0' || ch > '9') ch = getchar();    while(ch >= '0' && ch <= '9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();    return x;}struct Node{    Node *ls, *rs;    int cnt[8], flag;    Node(){ flag = 0 ; memset( cnt , 0 , sizeof( cnt )) ;}    Node operator+( const Node &a ) const{        Node tmp = Node();        for(int i=1; i<=7; ++i)            tmp.cnt[i] = cnt[i] + a.cnt[i] ;        return tmp;    }    void pushdown(int lf, int rg){        if( flag ){            int mid = (lf + rg) >> 1;            ls->cnt[flag] = mid - lf + 1;            ls->flag = flag;            rs->cnt[flag] = rg - mid;            rs->flag= flag;            for(int i=1; i<=7; ++i)                 if(i != flag) ls->cnt[i] = rs->cnt[i] = 0;            flag = 0;        }    }    void update(){        for(int i=1; i<=7; ++i)            cnt[i] = ls->cnt[i] + rs->cnt[i];    }}pool[maxn*20], *tail = pool, *rot;inline Node* build(int lf, int rg){    Node *nd = ++tail;    if(lf == rg) {        for(int i=1; i<=7; ++i)             nd->cnt[i] = 0;        nd->cnt[a[lf]]++;         return nd;    }    int mid = (lf + rg) >> 1;    nd->ls = build(lf, mid);    nd->rs = build(mid+1, rg);    nd->update();    return nd;}inline void modify(Node *nd, int lf, int rg, int L, int R, int data){    if(L <= lf && rg <= R){        nd->cnt[data] = rg - lf + 1;         nd->flag = data;        for(int i=1; i<=7; ++i)             if(i != data) nd->cnt[i] = 0;        return;    }     nd->pushdown(lf, rg);    int mid = (lf + rg) >> 1;    if(L <= mid) modify(nd->ls, lf, mid, L, R, data);    if(R > mid) modify(nd->rs, mid+1, rg, L, R, data);    nd->update();    return;}/*inline int query(Node *nd, int lf, int rg, int L, int R, int data){    if(L <= lf && rg <= R)         return nd->cnt[data];    nd->pushdown(lf, rg);    int mid = (lf + rg) >> 1, rt = 0;    if(L <= mid) rt += query(nd->ls, lf, mid, L, R, data);    if(R > mid) rt += query(nd->rs, mid+1, rg, L, R, data);    nd->update();    return rt;}*/inline Node query(Node *nd, int lf, int rg , int L , int R ){    if( L <= lf && rg <= R )        return *nd;    Node rt = Node() ;    nd->pushdown(lf, rg) ;    int mid = ( lf + rg ) >> 1 ;    if( L <= mid ) rt = rt + query( nd->ls, lf , mid , L , R ) ;    if( R >  mid ) rt = rt + query( nd->rs, mid+1, rg , L , R ) ;    return rt ;}//返回一个结构体(内有cnt[1~7]),就只需要run一次,而不是七次了。 inline int query_point(Node *nd, int lf, int rg, int pos){    if(lf == rg){        for(int i=1; i<=7; ++i)            if(nd->cnt[i]) return i;    }    nd->pushdown(lf, rg);    int mid = (lf + rg) >> 1, rt = 0;    if(pos <= mid) rt = query_point(nd->ls, lf, mid, pos);    else rt = query_point(nd->rs, mid+1, rg, pos);    nd->update();    return rt;}int main(){    freopen("third.in", "r", stdin);    freopen("third.out", "w", stdout);    n = read(), T = read();    for(register int i=1; i<=n; ++i) a[i] = read();    rot = build(1, n);    for(int i=1; i<=T; ++i){        int l = read(), r = read(), opt = read();        Node t = query(rot, 1, n, l, r);//答案结构体         for(int j=1; j<=7; ++j) sum[j] = t.cnt[j];        int st = l;        if( !opt ){            for(int j=1; j<=7; ++j)                if( sum[j] ) modify(rot, 1, n, st, st+sum[j]-1, j), st += sum[j];        }        else{            for(int j=7; j>=1; --j)                if( sum[j] ) modify(rot, 1, n, st, st+sum[j]-1, j), st += sum[j];        }    }    for(register int j=1; j<=n; ++j) printf("%d ", query_point(rot, 1, n, j));    return 0;}
原创粉丝点击