线段树plus

来源:互联网 发布:js div添加点击事件 编辑:程序博客网 时间:2024/06/07 04:02

Just a HookHDU1698

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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.

48569

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.
###Input
The 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.

Output

For 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.


输入一个n,表示n个钩子,每个钩子初值为1,
只有一个q,然后q个操作,a b c 表示从第a个到第b个改为c。
求最后所有钩子的总值


线段树区间更新求和
因为新增的更改会覆盖以前的,所以再更新的时候:
1,走到某个节点但不是目标区间且此节点有修改值。
2,将这个节点原有的值分到左右儿子位置,并将此节点归零。
3,继续往下递归

查询:
把所有的更改值传下来,计算。

只会有一层flag有修改值,其余都是0,因为在update 的时候遇到一个flag不为零则将这个flag压下去到儿子那里。


#include<stdio.h>int tree[400000];int n, q, ans;void build(int t, int l, int r){    tree[t] = 0;    if (l == r)    {        tree[t] = 1;        return;    }    int mid = l + r >> 1;    build(t << 1, l, mid);    build(t << 1 | 1, mid + 1, r);}void update(int t, int l, int r, int x, int y, int z){    if (l >= x&&r <= y)    {        tree[t] = z;        return;    }    if (tree[t] != 0)//这个点flag不为零,压下去    {        tree[t << 1] = tree[t << 1 | 1] = tree[t];        tree[t] = 0;    }    int mid = l + r >> 1;    if (x > mid)    {        update(t << 1 | 1, mid + 1, r, x, y, z);    }    else if (y <= mid)    {        update(t << 1, l, mid, x, y, z);    }    else    {        update(t << 1, l, mid, x, y, z);        update(t << 1 | 1, mid + 1, r, x, y, z);    }}void getans(int t, int l, int r){    if (tree[t] != 0)    {        ans += (r-l + 1)*tree[t];        return;    }    int mid = l + r >> 1;    getans(t << 1, l, mid);    getans(t << 1 | 1, mid + 1, r);}int main(){    int T;    scanf("%d", &T);    for (int I = 1; I <= T; I++)    {        scanf("%d", &n);        build(1, 1, n);        scanf("%d", &q);        int l, r, t;        while (q--)        {            scanf("%d%d%d", &l, &r, &t);            update(1, 1, n, l, r, t);        }        ans = 0;        getans(1, 1, n);        printf("Case %d: The total value of the hook is %d.\n", I, ans);    }}

A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Case Time Limit: 2000MS

Description

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


n个数,q个操作
Q a b 查询a-b的和
C a b c a-b都加c


#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;typedef long long ll;struct node{    ll num;    ll flag;}tre[5000000 + 5];void build(int t, int l, int r){    tre[t].flag = 0;    if (l == r)    {        scanf("%lld", &tre[t].num);        return;    }    int mid = l + r >> 1;    build(t << 1, l, mid);    build(t  << 1 | 1, mid + 1, r);    tre[t].num = tre[t << 1].num + tre[t << 1 | 1].num;}ll ans;int getlen(int l, int x, int r, int y){    return min(r, y) - max(l, x) + 1;}void update(int t, int l, int r, int x, int y, int k){    if (l >= x&&r <= y)    {        tre[t].flag += k;        return;    }    tre[t].num += (getlen(l, x, r, y)*k);    int mid = l + r >> 1;    if (y <= mid)        update(t << 1, l, mid, x, y, k);    else if (x > mid)        update(t << 1 | 1, mid + 1, r, x, y, k);    else    {        update(t << 1, l, mid, x, y, k);        update(t << 1 | 1, mid + 1, r, x, y, k);    }}void query(int t, int l, int r, int x, int y, ll f){    if (l >= x&&r <= y)    {        ans += tre[t].num;        ans += (f+tre[t].flag)*getlen(l,x,r,y);        return;    }    int mid = l + r >> 1;    if (y <= mid)        query(t << 1, l, mid, x, y, f + tre[t].flag);    else if (x > mid)        query(t << 1 | 1, mid + 1, r, x, y, f + tre[t].flag);    else    {        query(t << 1, l, mid, x, y, f + tre[t].flag);        query(t << 1 | 1, mid + 1, r, x, y, f + tre[t].flag);    }}int main(){    int n, q;    scanf("%d%d", &n, &q);    build(1, 1, n);    while (q--)    {        char s[5];        int a, b, c;        scanf("%s%d%d", s, &a, &b);        if (s[0] == 'Q')        {            ans = 0;            query(1, 1, n, a, b, 0);            printf("%lld\n", ans);        }        else        {            scanf("%d", &c);            update(1, 1, n, a, b, c);        }    }}

Mayor’s postersPOJ2528

Time Limit: 1000MS Memory Limit: 65536K

Description

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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


n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) 。求出最后还能看见多少张海报。
Input
第一行: 样例个数T
第二行: 贴海报的人n
第三行: 每个人贴海报的范围
接下来n行: 每个人贴海报的范围
Output
对于每一个输入,输出最后可以看到的海报张数

#include<iostream>#include<stdio.h>#include<math.h>#include<algorithm>#include<map>#include<string.h>using namespace std;int Left[11111], Right[11111];int tree[150000];int existed[11111];int answer;map<int, int>mp;void build(int temp, int left, int right){    tree[temp] = 0;    if (left == right)    {        return;    }    int mid = left + right >> 1;    build(temp << 1, left, mid);    build(temp << 1 | 1, mid + 1, right);}void update(int temp, int l, int r, int x, int y, int k){    if (tree[temp] != 0)    {        tree[temp << 1] = tree[temp << 1 | 1] = tree[temp];        tree[temp] = 0;    }    if (x<=l&&r <= y)    {        tree[temp] = k;        return;    }    int mid = l + r >> 1;    if (y <= mid)    {        update(temp << 1, l, mid, x, y, k);    }    else if (x > mid)    {        update(temp << 1 | 1, mid + 1, r, x, y, k);    }    else    {        update(temp << 1, l, mid, x, y, k);        update(temp << 1 | 1, mid + 1, r, x, y, k);    }}void query(int temp, int left, int right){    if (left == right || tree[temp] != 0)    {        if (existed[tree[temp]] == 0)            answer++;        existed[tree[temp]]++;        return;    }    int mid = left + right >> 1;    query(temp << 1, left, mid);    query(temp << 1 | 1, mid + 1, right);}int main(){    int cases;    scanf("%d", &cases);    while (cases--)    {        memset(existed, 0, sizeof(existed));        mp.clear();        int n;        scanf("%d", &n);        int maxpoint = 0;        for (int i = 1; i <= n; i++)        {            scanf("%d%d", &Left[i], &Right[i]);            mp[Left[i]]++;            mp[Right[i]]++;        }        map<int, int>::iterator it = mp.begin();        for ( int i=1; it != mp.end(); it++)        {            it->second = i++;        }        for (int i = 1; i <= n; i++)        {            Left[i] = mp[Left[i]];            Right[i] = mp[Right[i]];            maxpoint = max(Left[i], max(maxpoint, Right[i]));        }        build(1, 1, maxpoint);        for (int i = 1; i <= n; i++)        {            update(1, 1, maxpoint, Left[i], Right[i], i);        }        answer = 0;        query(1, 1, maxpoint);        printf("%d\n", answer);    }}