[kuangbin带你飞]专题七 线段树

 
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 
Sample Output
Case 1:
6
33
59

#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
const int mxn = 50000+5;
struct node
{
    int l,r,val,lazy;
}no[mxn<<2];
void pushup(int now) { no[now].val = no[lc].val+no[rc].val; return ;}
void build(int now , int l,int r)
{
    no[now].l = l;no[now].r = r;
    if(l==r)
    {
        cin>>no[now].val;
        //cout<<no[now].val<<" ";
        return ;
    }
    int mid = l+((r-l)>>1);
    build(lc,l,mid);
    build(rc,mid+1,r);
    pushup(now);
}
int query(int now,int l,int r)
{
    if(no[now].l>=l && r>=no[now].r)
        return no[now].val;
    int mid = no[now].l+((no[now].r-no[now].l)>>1),cnt = 0;
    if(l<=mid)
        cnt+=query(lc,l,r);
    if(r>mid)
        cnt+=query(rc,l,r);
    return cnt;
}
void updata(int now,int pos,int key)
{
    if(no[now].l==no[now].r)
    {
        no[now].val+=key;
        return ;
    }
    int mid = half;
    if(pos<=mid)
        updata(lc,pos,key);
    else
        updata(rc,pos,key);
    pushup(now);
}
int main()
{
    TLE;
    int t,n,l,r;
    string str;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        cout<<"Case "<<i<<":"<<endl;
        cin>>n;
        build(1,1,n);
        while(cin>>str&&(str[0]!='E'))
        {
            if(str[0]=='Q')
            {
                cin>>l>>r;
                cout<<query(1,l,r)<<endl;
            }
            else if(str[0]=='A')
            {
                cin>>l>>r;
                updata(1,l,r);
            }

            else if(str[0]=='S')
            {
                cin>>l>>r;
                updata(1,l,-1*r);
            }
        }
    }
    return 0;
}

B - I Hate It

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output对于每一次询问操作,在一行里面输出最高成绩。Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
5
6
5
9


        
 
Hint
Huge input,the C function scanf() will work better than cin
        
 
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
const int mxn = 200000+5; int mx;
struct node
{
    int l,r,val,lazy;
}no[mxn<<2];
void pushup(int now) { no[now].val = max( no[lc].val,no[rc].val); return ;}
void build(int now , int l,int r)
{
    no[now].l = l;no[now].r = r;
    if(l==r)
    {
        cin>>no[now].val;
        return ;
    }
    int mid = l+((r-l)>>1);
    build(lc,l,mid);
    build(rc,mid+1,r);
    pushup(now);
}
void query(int now,int l,int r)
{
    if(no[now].l>=l && r>=no[now].r)
        {mx = max(mx,no[now].val);return ;}
    int mid = no[now].l+((no[now].r-no[now].l)>>1);
    if(l<=mid)
        query(lc,l,r);
    if(r>mid)
        query(rc,l,r);
}
void updata(int now,int pos,int key)
{
    if(no[now].l==no[now].r&&no[now].l==pos)
    {
        no[now].val = key;
        return ;
    }
    int mid = half;
    if(pos<=mid)
        updata(lc,pos,key);
    else
        updata(rc,pos,key);
    pushup(now);
}
int main()
{
    TLE;
    int t,n,l,r;
    while(cin>>n>>t)
    {
        build(1,1,n);
        while(t--)
        {
            mx = -1;
            char ch;
            cin>>ch>>l>>r;
            if(ch=='Q')
            {
                query(1,l,r);
                cout<<mx<<endl;
            }
            else
                updata(1,l,r);
        }
        
    }
    return 0;
}

K - Atlantis

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

InputThe input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.OutputFor each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00 


明天更新吧,到了睡觉时间了

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11741472.html