[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;
}

 

C - Just a Hook

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.

InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
#include <iostream>
using namespace std;
const int N = 5*1e5+5;
#define ls now<<1
#define rs now<<1|1
#define lli long long
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef struct node
{
    lli val,lazy,l,r,mx,mn;
}node;
node dp[N<<2];
int a[N];
void pushup(int now) {dp[now].val = dp[ls].val+dp[rs].val;return ;}
void build(int now,int l,int r)
{
    dp[now].l=l; dp[now].r=r; dp[now].lazy=1; dp[now].val=1;
    if(l==r) {return ;}
    int mid = (r+l)>>1;
    //cout<<"mid=====      " << mid <<endl;
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(now);
}
void pushdown(int now)
{
    if(dp[now].lazy)
    {
        dp[ls].lazy = dp[now].lazy;
        dp[rs].lazy = dp[now].lazy;
        dp[ls].val = dp[now].lazy*(dp[ls].r-dp[ls].l+1);
        dp[rs].val = dp[now].lazy*(dp[rs].r-dp[rs].l+1);
        dp[now].lazy = 0;
    }
}
lli query(int now,int l,int r)
{
    if(dp[now].l>=l && dp[now].r<=r)
        {/*cout<<"+++++"<<endl;*/return dp[now].val;}
    pushdown(now);
    lli mid = ( dp[now].l+dp[now].r )>>1;
    long long cnt=0;
    if(l<=mid)
        cnt+=query(ls,l,r);
    if(r>mid)
        cnt+=query(rs,l,r);
    return cnt;
}
void updata(int now,int l,int r,lli k)
{
    if(dp[now].l>=l && dp[now].r<=r)
    {
        dp[now].lazy=k;
        dp[now].val=(dp[now].r-dp[now].l+1)*k;
        return ;
    }
    pushdown(now);
    int mid = ( dp[now].l+dp[now].r )>>1;
    if(l<=mid) updata(ls,l,r,k);
    if(r>mid)  updata(rs,l,r,k);
    pushup(now);

}
int main()
{
    TLE;
    int t;
    cin>>t;
    lli n,m,lll,rrr,k;
    for(int kk=1;kk<=t;kk++)
    {
        cin>>n>>m;
        //for(int i=1;i<=n;i++) cin>>a[i];
        build(1,1,n);
        string str;
        for(int i=0;i<m;i++)
        {
            cin>>lll>>rrr>>k;
            updata(1,lll,rrr,k);
        }
        cout<<"Case "<<kk<<": The total value of the hook is "<<query(1,1,n)<<"."<<endl;
    }
    ok;
}

 

 

D - Can you answer these queries?

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.

InputThe input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6
#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 = 100000+5; int mx;
struct node
{
    int l,r,lazy;
    ll val, num;
}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)
    {
        scanf("%lld",&no[now].val);
        return ;
    }
    int mid = l+((r-l)>>1);
    build(lc,l,mid);
    build(rc,mid+1,r);
    pushup(now);
}
ll query(int now,int l,int r)
{
    if(no[now].l>=l && r>=no[now].r)
        return no[now].val;
    ll mid = half;
    ll 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 l,int r)
{
    if(no[now].r-no[now].l+1 == no[now].val)    return ;
    if(no[now].l == no[now].r)
    {
        no[now].val = sqrt(no[now].val) ;
        return ;
    }
    int mid = half;
    if(l<=mid)
        updata(lc,l,r);
    if(r>mid)
        updata(rc,l,r);
    pushup(now);
}
  //  #define LOCAL
int main()
{
    TLE;
    int t,n,l,r,ase=1;
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",ase++);
        build(1,1,n);
        scanf("%d",&t);
        while(t--)
        {
            int ch;
            scanf("%d %d %d",&ch,&l,&r);
            if(l>r) swap(l,r);
            if(ch)
                printf("%lld\n",query(1,l,r));
            else
                updata(1,l,r);
        }
        printf("\n");
    }
    return 0;
}

 

F - Assign the task

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody's boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.

InputThe first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.

For each test case:

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).

The next line contains an integer M (M ≤ 50,000).

The following M lines each contain a message which is either

"C x" which means an inquiry for the current task of employee x

or

"T x y"which means the company assign task y to employee x.

(1<=x<=N,0<=y<=10^9)OutputFor each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.Sample Input
1 
5 
4 3 
3 2 
1 3 
5 2 
5 
C 3 
T 2 1
 C 3 
T 3 2 
C 3
Sample Output
Case #1:
-1 
1 
2

DFS+线段树,或者vector+DFS

 



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 


明天更新吧,到了睡觉时间了
posted @ 2019-10-25 23:50  __MEET  阅读(199)  评论(0编辑  收藏  举报