HDOJ 1698 Just a Hook

来源:互联网 发布:返利网 淘宝taosave 编辑:程序博客网 时间:2024/05/17 22:13

题意:给出n组数据,每组第一行表示数的范围,第二行表示有k组数据,接下来k行,每行有三个数,l,r,t,将l->r范围内的所有数变成t,求最终的这个数列的和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

思路:线段树成段更新,查询树根。

注意点:裸线段树,第一次cin没关闭流同步TLE了一次。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor126168062014-12-30 19:13:23Accepted16981045MS3784K2260 BG++luminous11

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <deque>#include <list>#include <cctype>#include <algorithm>#include <climits>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#include <iomanip>#include <cstdlib>#include <ctime>#define ll long long#define ull unsigned long long#define all(x) (x).begin(), (x).end()#define clr(a, v) memset( a , v , sizeof(a) )#define pb push_back#define mp make_pair#define read(f) freopen(f, "r", stdin)#define write(f) freopen(f, "w", stdout)using namespace std;int num[100005<<2];int cnt[100005<<2];inline void pushdown ( int rt, int len ){    if ( cnt[rt] > 0 )    {        cnt[rt<<1] = cnt[rt<<1|1] = cnt[rt];        num[rt<<1] = ( len - ( len >> 1 ) ) * cnt[rt];        num[rt<<1|1] = ( len >> 1 ) * cnt[rt];        cnt[rt] = 0;    }}void update ( int L, int R, int l, int r, int rt, int n ){    if ( L <= l && r <= R )    {        cnt[rt] = n;        num[rt] = ( r - l + 1 ) * n;        return;    }    else    {        pushdown ( rt, r - l + 1 );        int mid = ( l + r ) >> 1;        if ( L <= mid )        {            update ( L, R, l, mid, rt << 1, n );        }        if ( mid < R )        {            update ( L, R, mid + 1, r, rt << 1 | 1, n );        }        num[rt] = num[rt<<1] + num[rt<<1|1];    }}void build ( int l, int r, int rt ){    cnt[rt] = 0;    if ( l == r )num[rt] = 1;    else    {        int mid = ( l + r ) >> 1;        build ( l, mid, rt << 1 );        build ( mid + 1, r, rt << 1 | 1 );        num[rt] = num[rt<<1] + num[rt<<1|1];    }}int main(){    ios::sync_with_stdio( false );    int ncase;    cin >> ncase;    for ( int nc = 1; nc <= ncase; nc ++ )    {        int m, k, l, r, n;        memset ( num, 0, sizeof ( num ) );        cin >> m >> k;        build ( 1, m, 1 );        for ( int i = 0; i < k; i ++ )        {            cin >> l >> r >> n;            update ( l, r, 1, m, 1, n );        }        cout << "Case " << nc << ": The total value of the hook is " << num[1] << "." << endl;    }    return 0;}


0 0
原创粉丝点击