码迷,mamicode.com
首页 > 其他好文 > 详细

线段树(全)

时间:2017-04-26 21:37:04      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:which   everyone   tran   directly   transform   spec   app   general   include   

A - 敌兵布阵

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

      

#include"stdio.h"
#include"cstdio"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
const int max_n=1e5+10;
int A[max_n];
int B[max_n<<1];
void init(int l,int r,int rt)
{
    if(l==r)
    {
        B[rt]=A[l];return ;
    }
    int mid=(l+r)>>1;
    init(l,mid,rt<<1);
    init(mid+1,r,(rt<<1)|1);
    B[rt]=B[rt<<1]+B[(rt<<1)|1];
} 
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return B[rt];
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid) ans+=query(L,R,l,mid,rt<<1);
    if(R>mid) ans+=query(L,R,mid+1,r,(rt<<1)|1);
    return ans;
}
void update(int L,int R,int l,int r,int rt)
{
    if(l==r)
    {
        B[rt]=R+B[rt];return ;
    }
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,l,mid,rt<<1);
    if(L>mid) update(L,R,mid+1,r,(rt<<1)|1);
    B[rt]=B[rt<<1]+B[(rt<<1)|1];
}
int main()
{
    int t,i;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        int n;scanf("%d",&n);
        for(int j=1;j<=n;j++) scanf("%d",&A[j]);
        init(1,n,1);
        printf("Case %d:\n",i+1);
        char c[6];
        int L,R;
        while(scanf("%s",c)!=EOF)
        {
            if(c[0]==E)
            break;
            else scanf("%d%d",&L,&R);
            int ret;
            if(c[0]==Q)
            {
                ret=query(L,R,1,n,1);printf("%d\n",ret);
            }
            if(c[0]==A)
            update(L,R,1,n,1);
            if(c[0]==S)
            update(L,-1*R,1,n,1);    
        }
    }
}

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
#include"stdio.h"
#include"cstdio"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
const int maxn =2*1e5+10;
int A[maxn<<2],B[maxn];
int n;
void init(int l,int r,int rt)
{
    if(l==r)
    {
        A[rt]=B[l];return ;
    }
    int mid=(l+r)>>1;
    init(l,mid,rt<<1);
    init(mid+1,r,(rt<<1)|1);
    A[rt]=max(A[rt<<1],A[(rt<<1)|1]);
}
int query(int L,int R,int l, int r,int rt)
{
    if(L<=l&&R>=r)
    return A[rt];
    int mid=(l+r)>>1,ans=0;
    if(L<=mid)
    ans=max(ans,query(L,R,l,mid,rt<<1));
    if(R>mid)
    ans=max(ans,query(L,R,mid+1,r,(rt<<1)|1));
    return ans;    
}
void update(int pos,int val,int l,int r,int rt)
{
    if(l==r)
    {
        A[rt]=val;return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid)
    update(pos,val,l,mid,rt<<1);
    if(pos>mid)
    update(pos,val,mid+1,r,(rt<<1)|1);
    A[rt]=max(A[rt<<1],A[(rt<<1)|1]);
}
int main()
{
    int L,R,n,m,i,j;
    char c[5];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
        scanf("%d",&B[i]);
        init(1,n,1);
        for(i=0;i<m;i++)
        {
            scanf("%s%d%d",c,&L,&R);
            if(c[0]==Q)
            printf("%d\n",query(L,R,1,n,1));
            else
            update(L,R,1,n,1);
        }
    }
}

C - A Simple Problem with Integers

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers

 

#include"stdio.h"
#include"cstdio"
#include"algorithm"
#define INF 0x3f3f3f3f
typedef long long ll;
const ll max_n=1e5+10;
ll A[max_n<<4],lazy[max_n<<4];
ll B[max_n];
using namespace std;
void init(ll l,ll r,ll rt)
{
    lazy[rt]=0; 
    if(l==r)
    {
        A[rt]=B[l];return ;
    }
    ll mid=(r+l)>>1;
    init(l,mid,rt<<1);
    init(mid+1,r,(rt<<1)|1);
    A[rt]=A[rt<<1]+A[(rt<<1)|1];
}
void down(ll rt,ll lens)
{
    if(lazy[rt])
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[(rt<<1)|1]+=lazy[rt];
        A[rt<<1]+=lazy[rt]*(lens-(lens>>1));
        A[(rt<<1)|1]+=lazy[rt]*(lens>>1);
        lazy[rt]=0;
    }
}
ll query(ll L,ll R,ll l,ll r,ll rt)
{
    if(l>=L&&r<=R) return A[rt];
    down(rt,r-l+1);
    ll ans=0;
    ll mid=(l+r)>>1;
    if(L<=mid) ans+=query(L,R,l,mid,rt<<1);
    if(R>mid) ans+=query(L,R,mid+1,r,(rt<<1)|1);
    return ans;
}
void update(ll L,ll R,ll val,ll l,ll r,ll rt)
{
    if(l>=L&&r<=R)
    {
        lazy[rt]+=val;
        A[rt]+=val*(r-l+1);
        return ; 
    }
    down(rt,r-l+1);
    ll mid=(l+r)>>1;
    if(L<=mid) update(L,R,val,l,mid,rt<<1);
    if(R>mid) update(L,R,val,mid+1,r,(rt<<1)|1);
    A[rt]=A[rt<<1]+A[(rt<<1)|1];
}
int main() 
{
    ll n,q;
    while(scanf("%lld%lld",&n,&q)!=EOF)
    {
        ll i;
        for(i=1;i<=n;i++) scanf("%lld",&B[i]);
        init(1,n,1);
        char c[5];
        ll L,R;
        while(q--)
        {
            scanf("%s",c);
            if(c[0]==Q)
            {
                scanf("%lld%lld",&L,&R);
                ll ret=query(L,R,1,n,1);
                printf("%lld\n",ret);
            }
            else
            {
                ll val;
                scanf("%lld%lld%lld",&L,&R,&val);
                update(L,R,val,1,n,1);
            }
        }
    }
}

D - Mayor‘s posters

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.


They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters‘ size, their place and order of placement on the electoral wall.
Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.
技术分享

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4
#include"stdio.h"
#include"cstdio"
#include"algorithm"
typedef long long ll;
using namespace std;
const int max_n=1e4+10;
ll lazy[max_n<<8],visit[max_n<<8];
ll A[max_n<<8];
ll B[max_n<<8],C[max_n<<8];
ll a[max_n],b[max_n];
int len=0;
void chang(int m)
{
    int i;
    len=0;
    B[++len]=C[1];
    for(i=2;i<=m;i++)
    {
        if(C[i]-C[i-1]>1) B[++len]=C[i-1]+1;
        if(C[i]!=B[len]) B[++len]=C[i]; 
    }
}
int EF(int l,int r,ll num)
{
    int mid=(l+r)>>1;
    while(l<=r)
    {
        if(B[mid]>num) r=mid-1;
        else if(B[mid]<num) l=mid+1;
        else if(B[mid]==num) return mid;
        mid=(l+r)>>1;
    }
    return mid;
}
void down(int rt)
{
    lazy[rt<<1]=lazy[rt];
    lazy[(rt<<1)|1]=lazy[rt];
    lazy[rt]=0;
}
void init(int l,int r,int rt)
{
    lazy[rt]=0;A[rt]=0;
    if(l==r)
    {
        A[rt]=B[l];return ;
    }
    int mid=(l+r)>>1;
    init(l,mid,rt<<1);
    init(mid+1,r,(rt<<1)|1);
}
void update(int L,int R,ll i,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        lazy[rt]=i;return ;
    }
    if(lazy[rt])  down(rt);
    int mid=(l+r)>>1;
    if(mid>=L) update(L,R,i,l,mid,rt<<1);
    if(mid<R) update(L,R,i,mid+1,r,(rt<<1)|1);
}
void query(int l,int r,int rt)
{    
    if(lazy[rt]) 
    {
        visit[lazy[rt]]=1;return ;
    }
    if(l==r) return ;
//    if(lazy[rt]) down(rt);
    int mid=(l+r)>>1;
    query(l,mid,rt<<1);
    query(mid+1,r,(rt<<1)|1);
}
int main()
{
    int i,c;
    while(scanf("%d",&c)!=EOF)
    {
        while(c--)
        {
            int ret=0,m=0,n;
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                scanf("%lld%lld",&a[i],&b[i]);
                C[++m]=a[i];C[++m]=b[i];
            }
            sort(C+1,C+m+1);
            chang(m);
            init(1,len,1);
            for(i=1;i<=n;i++)
            {
                int L=EF(1,len,a[i]);
                int R=EF(1,len,b[i]);
                update(L,R,i,1,len,1);
            }
            query(1,len,1);
            for(i=1;i<=n;i++)
            {
                if(visit[i]==1) ret++;
                visit[i]=0;
            }
            printf("%d\n",ret);
        }
    }
}

E - Just a Hook

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
#include"stdio.h"
#include"cstdio"
#include"algorithm"
using namespace std;
const int max_n=1e5+10;
int A[max_n<<2],lazy[max_n<<2];
int B[max_n];
void init(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        A[rt]=B[l];return ;
    }
    int mid=(l+r)>>1;
    init(l,mid,rt<<1);
    init(mid+1,r,(rt<<1)|1);
    A[rt]=A[rt<<1]+A[(rt<<1)|1];
}
void down(int rt,int len)
{
    lazy[rt<<1]=lazy[rt];
    lazy[(rt<<1)|1]=lazy[rt];
    A[rt<<1]=(len-(len>>1))*lazy[rt];
    A[(rt<<1)|1]=(len>>1)*lazy[rt];
    lazy[rt]=0;
}
void update(int L,int R,int val,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        A[rt]=(r-l+1)*val;
        lazy[rt]=val;return ;
    }
    if(lazy[rt]) down(rt,r-l+1);
    int mid=(l+r)>>1;
    if(mid>=L) update(L,R,val,l,mid,rt<<1);
    if(mid<R) update(L,R,val,mid+1,r,(rt<<1)|1);
    A[rt]=A[rt<<1]+A[(rt<<1)|1];
}
int main()
{
    int c,i;
    while(scanf("%d",&c)!=EOF)
    {
        int j=0;
        while(c--)
        {
            int L,R,val,n,m;
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++) B[i]=1;
            init(1,n,1);
            while(m--)
            {
                scanf("%d%d%d",&L,&R,&val);update(L,R,val,1,n,1);    
            } 
            printf("Case %d: The total value of the hook is %d.\n",++j,A[1]);
        }
    }
}

F - Count the Color

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

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

G - Balanced Lineup

For the daily milking, Farmer John‘s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q.
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0
 

H - 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

I - Tunnel Warfare

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
InputThe first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
OutputOutput the answer to each of the Army commanders’ request in order on a separate line.
Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4
                                                          

J - 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

K - Transformation

L - Vases and Flowers

Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.Input  The first line contains an integer T, indicating the number of test cases.
  For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).Output  For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output ‘Can not put any one.‘. For each operation of which K is 2, output the number of discarded flowers.
   Output one blank line after each test case.Sample Input

2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3

Sample Output

[pre]3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3

[/pre]

M - 约会安排

寒假来了,又到了小明和女神们约会的季节。
  小明虽为?潘考堵肱?,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
  我们已知小明一共有T的空闲时间,期间会有很多女神或者基友来找小明。
  作为一个操作系统曾经怒考71分的大神,小明想到了一个算法,即“首次适应算法”,根据操作系统课本的描述,就是找一段最靠前的符合要求的连续空间分配给每个请求,由此小明做出了一个决定:
  当一个基友来找小明时,小明就根据“首次适应算法”来找一段空闲的时间来和基友约好,如果找到,就说“X,let’s fly”(此处,X为开始时间),否则就说“fly with yourself”;
  当女神来找小明时,先使用一次“首次适应算法”,如果没有找到,小明就冒着木叽叽的风险无视所有?潘炕?友的约定,再次使用“无视基友首次适应算法”,两次只要有一次找到,就说“X,don’t put my gezi”(此处,X为开始时间),否则就说“wait for me”
  当然,我们知道小明不是一个节操负无穷的人,如果和女神约会完,还有剩余时间,他还是会和原来约好的基友去dota的。(举个例子:小西(?潘浚┖托∶髟己迷?1~5这个时间单位段内打dota,这时候,女神来和小明预约长度为3的时间段,那么最终就是1~3小明去和女神约会,搞定后在4~5和小西打dota)
  小明偶尔也会想要学习新知识,此时小明就会把某一个时间区间的所有已经预定的时间全部清空用来学习并且怒吼“I am the hope of chinese chengxuyuan!!”,不过小明一般都是三分钟热度,再有人来预定的话,小明就会按耐不住寂寞把学习新知识的时间分配出去。Input输入第一行为CASE,表示有CASE组测试数据;
每组数据以两个整数T,N开始,T代表总共的时间,N表示预约请求的个数;
接着的N行,每行表示一个女神或者基友的预约,“NS QT”代表一个女神来找小明约一段长为QT的时间,“DS QT”则代表一个?潘康某の?QT的请求,当然也有可能是小明想学知识了,“STUDY!! L R”代表清空L~R区间内的所有请求。

TechnicalSpecificationTechnicalSpecification
1. 1 <= CASE <= 30
2. 1 <= T, N <= 100000
3. 1 <= QT <= 110000
4. 1 <= L <= R <=T
Output对于每一个case,第一行先输出“Case C:”代表是第几个case,然后N行,每行对应一个请求的结果(参照描述)。
输出样本(可复制此处):
“X,let‘s fly”,”fly with yourself”,”X,don‘t put my gezi”,”wait for me”,”I am the hope of chinese chengxuyuan!!”
Sample Input

1
5 6
DS 3
NS 2
NS 4
STUDY!! 1 5
DS 4
NS 2

Sample Output

Case 1:
1,let‘s fly
4,don‘t put my gezi
wait for me
I am the hope of chinese chengxuyuan!!
1,let‘s fly
1,don‘t put my gezi

N - Picture

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

技术分享


The corresponding boundary is the whole set of line segments drawn in Figure 2.

技术分享


The vertices of all rectangles have integer coordinates.
Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

Sample Input

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16

Sample Output

228

O - 覆盖的面积

给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

技术分享


Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
Output对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input

2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1

Sample Output

7.63
0.00

P - 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 

Q - Get The Treasury

Jack knows that there is a great underground treasury in a secret region. And he has a special device that can be used to detect treasury under the surface of the earth. One day he got outside with the device to ascertain the treasury. He chose many different locations on the surface of the earth near the secret region. And at each spot he used the device to detect treasury and got some data from it representing a region, which may contain treasury below the surface. The data from the device at each spot is six integers x 1, y 1, z 1, x 2, y 2 and z 2 (x 1<x 2, y 1<y 2, z 1<z 2). According to the instruction of the device they represent the range of x, y and z coordinates of the region. That is to say, the x coordinate of the region, which may contain treasury, ranges from x 1 to x 2. So do y and z coordinates. The origin of the coordinates is a fixed point under the ground.
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.

InputThe first line of the input file contains a single integer t, the number of test cases, followed by the input data for each test case.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x 1, y 1, z 1, x 2, y 2 and z 2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, and that of z coordinate is no more than 500.

OutputFor each test case, you should output “Case a: b” in a single line. a is the case number, and b is the minimum volume of treasury. The case number is counted from one.
Sample Input

2
1
0 0 0 5 6 4
3
0 0 0 5 5 5
3 3 3 9 10 11
3 3 3 13 20 45

Sample Output

Case 1: 0
Case 2: 8

 

                    
                   
                   
                    
                   

线段树(全)

标签:which   everyone   tran   directly   transform   spec   app   general   include   

原文地址:http://www.cnblogs.com/lch316/p/6770833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!