HDOJ 4614——線分樹区間更新&区間求和

7027 ワード

Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1575    Accepted Submission(s): 629
Problem Description
  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]

 
Source
2013 Multi-University Training Contest 2
 
Recommend
zhuyuanchen520
 
問題はあなたにn個の花瓶をあげて、最初はすべて空いていて、あなたは2つの操作があります:
一:1 L kはLから花瓶にk本の花を挿し、各花瓶に1本、現在の花瓶に花があれば次の花瓶に挿します.生け花の最初の花瓶と最後の花瓶を出力します.
2:2 L RはLからR区間の花瓶に捨てた花を表し、その総数を出力する.
構想:私たちは花瓶に線分の木を建てて、sum、val、flagの3つのドメインを維持します.
sumは区間内の空き花瓶数を表し、valはこの花瓶に花があるかどうかを示す:1はない、0はある、flagは遅延標識である.
それから2分で最初の花瓶と最後の花瓶を探せばいいです.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 50000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int n , m ;
struct node
{
    int l , r , sum , val , flag ;
    int len()
    {
        return r - l + 1;
    }
}a[MAXN << 2];
void build(int l , int r , int rt)
{
    a[rt].l = l , a[rt].r = r;
    a[rt].sum = a[rt].len();
    a[rt].val = 1;
    a[rt].flag = 0;
    if(l == r)return ;
    int mid = (l + r) >> 1;
    build(lson) ,build(rson);
}
void PushUp(int rt)
{
    a[rt].sum = a[rt << 1].sum + a[rt << 1 | 1].sum;
}
void PushDown(int rt)
{
    if(a[rt].flag)
    {
        a[rt << 1].val = a[rt].val;
        a[rt << 1 | 1].val = a[rt].val;
        a[rt << 1].sum = a[rt].val ? a[rt << 1].len() : 0;
        a[rt << 1 | 1].sum = a[rt].val ? a[rt << 1 | 1].len() : 0;
        a[rt << 1].flag = a[rt << 1 | 1].flag = 1;
        a[rt].flag = 0;
    }
}
void update(int delta , int L , int R , int rt)
{
    if(a[rt].l > R || a[rt].r < L)return ;
    if(a[rt].l >= L && a[rt].r <= R)
    {
        a[rt].val = delta;
        a[rt].sum = delta ? a[rt].len() : 0;
        a[rt].flag = 1;
        return ;
    }
    PushDown(rt);
    update(delta , L , R , rt << 1);
    update(delta , L , R , rt << 1 | 1);
    PushUp(rt);
}
int query(int L , int R , int rt)
{
    if(a[rt].l > R || a[rt].r < L)return 0;
    if(a[rt].l >= L && a[rt].r <= R)
    {
        return a[rt].sum;
    }
    PushDown(rt);
    return query(L , R , rt << 1) + query(L , R , rt << 1 | 1);
}
int BSearch(int l , int r , int key)
{
    while(l < r)
    {
        int mid = (l + r) >> 1;
        int sum = query(1 , mid , 1);
        if(sum < key)l = mid + 1;
        else r = mid;
    }
    return l;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%d%d" , &n , &m);
        build(1 , n , 1);
        while(m--)
        {
            int op , l,  r;
            scanf("%d%d%d" , &op  , &l , &r);
            l++;
            if(op == 1)
            {
                int s = query(l , n , 1);
                if(s == 0)
                {
                    puts("Can not put any one.");
                    continue;
                }
                if(s < r)
                {
                    r = s;
                }
                int t = query(1 , l - 1 , 1);
                int FF = BSearch(1 , n , t + 1);
                int LL = BSearch(1 , n , t + r);
                printf("%d %d
" , FF - 1 , LL - 1); update(0 , FF , LL , 1); } else { r++; int t = query(l, r , 1); printf("%d
" , r - l + 1 - t); update(1 , l , r , 1); } } puts(""); } return 0; }