Color the ball 线段树 区间更新但点查询

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100001
#define L 31
#define INF 1000000009
#define eps 0.00000001
struct node
{
    LL l, r, data;
}T[MAXN*4+10];
LL n,a[MAXN];
LL Query(LL p, LL k)
{
    if (T[p].l == T[p].r)
        return T[p].data;
    LL mid = (T[p].l + T[p].r) >> 1;
    LL sum = T[p].data;
    if (k <= mid)
        sum += Query(p << 1, k);
    else
        sum += Query(p << 1 | 1, k);
    return sum;
}
void Build(LL p, LL l, LL r)
{
    T[p].l = l, T[p].r = r, T[p].data = 0;
    if (l == r)
    {
        T[p].data = a[l];
        return;
    }
    LL mid = (l + r) >> 1;
    Build(p << 1, l, mid);
    Build(p <<1 | 1, mid + 1, r);
}
void Insert(LL p, LL l, LL r, LL num)
{
    //cout << p << ‘ ‘ << l << ‘ ‘ << r << ‘ ‘ << num << endl;
    if (l <= T[p].l&&r >= T[p].r)
    {
        T[p].data += num;
        return;
    }
    LL mid = (T[p].l + T[p].r) / 2;
    if (r <= mid)
        Insert(p << 1, l, r, num);
    else if (l > mid)
        Insert(p << 1 | 1, l, r, num);
    else
    {
        Insert(p << 1, l, mid, num);
        Insert(p << 1 | 1, mid + 1, r, num);
    }
}
int main()
{
    while (scanf("%lld", &n), n)
    {
        LL t1, t2;
        memset(a, 0, sizeof(a));
        Build(1, 1, n);
        for (LL i = 1; i <= n; i++)
        {
            scanf("%lld%lld", &t1, &t2);
            Insert(1, t1, t2, 1);
        }
        for (LL i = 1; i <= n; i++)
        {
            if (i>1) printf(" ");
            printf("%lld", Query(1, i));
        }
        printf("\n");
    }
    return 0;
}

Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum |Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams
ACM Steps
Go to Job
Contest LiveCast
[email protected]
Best Coder beta
VIP | STD Contests
Virtual Contests
  DIY |Web-DIY beta
Recent Contests

 joey97
 Mail 0(0)
 Control Panel
 Sign Out
 

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19857    Accepted Submission(s): 9901

Problem Description

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。

Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

Sample Output

1 1 1
3 2 1

Author

8600

时间: 2024-10-12 04:30:48

Color the ball 线段树 区间更新但点查询的相关文章

hdu1556 Color the ball(线段树区间更新)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12814    Accepted Submission(s): 6401 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况就需要处理一下,代码中把所有的左端点都+1,避免了这种情况. 3. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define L(root) ((root) &l

hdu 1556 Color the ball(线段树区间维护+单点求值)

传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25511    Accepted Submission(s): 12393 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele

nyoj 116 士兵杀敌(四)(线段树区间更新和单点查询)

题目123 题目信息 运行结果 本题排行 讨论区 士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战(编号相近的人经常在一块,相互之间比较熟悉),最终他们获得的军功,也将会平分到每个人身上,这样,有时候,计算他们中的哪一个人到底有多少军功就是一个比较困难的事情,军师小工的任务就是在南将军询问他某个人的军功的时候,快速的报出此人的军功,请你编写一个程序来

HDU 1556 Color the ball 线段树更新区间查点

点击打开链接 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9120    Accepted Submission(s): 4665 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

PKU 2777 Count Color (线段树区间更新)

题意: 给你三个数:L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000),表示有一长度为L的板(1~L), 有T种颜色(1~T),然后有O个操作,初始板1~L的颜色为1,"C A B C"表示在区间A,B图上C颜色, "P A B" 表示询问 A,B区间有几种不同的颜色. #include <stdio.h> #include <iostr

poj 2777 Count Color (线段树区间更新)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37647   Accepted: 11315 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

POJ2777 Count Color 线段树区间更新

题目描述: 长度为L个单位的画板,有T种不同的颜料,现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B",查询[A,B]区域内出现的颜色种类 出现操作2时,请输出答案 PS:初始状态下画板颜色为1 一开始没有想那么好,用int整型位移来代替颜色,还是使用了最传统的bool color[来记录,可是不知道错在了哪里, #include<iostream> #include<cstdio>