北大2018acm暑期课五线段树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40379678/article/details/81348383

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.

输入

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.

输出

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

样例输入

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

样例输出

4
55
9
15

提示

The sums may exceed the range of 32-bit integers.

emmmmmm第一或者二次写线段树吧这是,知道写不出来直接抄的别人的题解,竟然抄了一个带懒惰标记的Orz打扰了。

节点信息维护区间和,至于懒惰标记是我抄错板子了。。。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 400010
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define mid(l,r) (((l)+(r))>>1)
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

struct node {
   int l, r, length;
   ll lazy, sum;
}tree[N];

void up(int o) {
    tree[o].sum = tree[L].sum + tree[R].sum;
}
void down(int o)
{
    if(!tree[o].lazy) return;
    tree[L].sum += tree[L].length * tree[o].lazy;
    tree[R].sum += tree[R].length * tree[o].lazy;
    tree[L].lazy += tree[o].lazy;
    tree[R].lazy += tree[o].lazy;
    tree[o].lazy = 0;
}
void build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].lazy = 0; tree[o].length = r - l + 1;
    if(l == r) {
        scanf("%lld", &tree[o].sum); return;
    }
    build(L, l, mid(l, r));
    build(R, 1 + mid(l, r), r);
    up(o);
}
void add(int o, int l, int r, int v)
{
    if(tree[o].l == l && tree[o].r == r) {
        tree[o].sum += tree[o].length * v;
        tree[o].lazy += v;
        return;
    }
    down(o);
    if(r <= mid(tree[o].l, tree[o].r)) add(L, l, r, v);
    else if(l > mid(tree[o].l, tree[o].r)) add(R, l, r, v);
    else {
        add(L, l, mid(tree[o].l, tree[o].r), v);
        add(R, 1 + mid(tree[o].l, tree[o].r), r, v);
    }
    up(o);
}
ll query(int o, int l, int r)
{
    if(tree[o].l == l && tree[o].r == r) return tree[o].sum;
    down(o);
    if(r <= mid(tree[o].l, tree[o].r)) return query(L, l, r);
    else if(l > mid(tree[o].l, tree[o].r)) return query(R, l, r);
    else {
        return query(L, l, mid(tree[o].l, tree[o].r)) + query(R, mid(tree[o].l, tree[o].r) + 1, r);
    }
}

int main()
{
    freopen("data.txt", "r", stdin);

    char ch;
    int a, b, c, n, Q;
    while(~scanf("%d%d", &n, &Q)) {
        build(1, 1, n);
        while(Q--) {
            cin >> ch;

            if(ch == 'Q') {
                scanf("%d%d", &a, &b);
                printf("%lld\n", query(1, a, b));
            }
            else {
                scanf("%d%d%d", &a, &b, &c);
                add(1, a, b, c);
            }
        }
    }

    return 0;
}

//
//struct node {
//   int l, r;
//   ll sum, lazy;
//   int length;
//}tree[N];
//
//void pushup(int o){
//    tree[o].sum = tree[L].sum + tree[R].sum;
//}
//
//void pushdown(int o)
//{
//    if(tree[o].lazy) {
//        tree[L].sum += tree[L].length * tree[o].lazy;
//        tree[R].sum += tree[R].length * tree[o].lazy;
//        tree[L].lazy += tree[o].lazy;
//        tree[R].lazy += tree[o].lazy;
//        tree[o].lazy = 0;
//    }
//}
//
//void build(int o, int l, int r)
//{
//    tree[o].l = l;
//    tree[o].r = r;
//    tree[o].lazy = 0;
//    tree[o].length = r - l + 1;
//    if(l != r) {
//        build(L, l, (l + r) >> 1);
//        build(R,    1 + ((l + r) >> 1), r);
//    }
//    else {
//        scanf("%lld", &tree[o].sum);
//        //cout <<tree[o].sum << endl;
//        return;
//    }
//    pushup(o);
//}
//
//void add(int o, int l, int r, int v)
//{
//    if(tree[o].l == l && tree[o].r == r) {
//        tree[o].sum += v * tree[o].length;
//        tree[o].lazy += v;
//        return;
//    }
//    pushdown(o);
//    int mid = (tree[o].l + tree[o].r) >> 1;
//    if(r <= mid) add(L, l, r, v);
//    else if(l > mid) add(R, l, r, v);
//    else {
//        add(L, l, mid, v);
//        add(R, mid + 1, r, v);
//    }
//    pushup(o);
//}
//
//ll query(int o, int l, int r)
//{
//    if(tree[o].l == l && tree[o].r == r) return tree[o].sum;
//    pushdown(o);
//    int mid = (tree[o].l + tree[o].r) >> 1;
//    if(r <= mid) return query(L, l, r);
//    else if(l > mid) return query(R, l, r);
//    else {
//        return query(L, l, mid) + query(R, mid + 1, r);
//    }
//}
//
//int main()
//{
//    freopen("data.txt", "r", stdin);
//
//    char ch;
//    int a, b, c, n, Q;
//    while(~scanf("%d%d", &n, &Q)) {
//        build(1, 1, n);
//        while(Q--) {
//            cin >> ch;
//
//            if(ch == 'Q') {
//                scanf("%d%d", &a, &b);
//                printf("%lld\n", query(1, a, b));
//            }
//            else {
//                scanf("%d%d%d", &a, &b, &c);
//                add(1, a, b, c);
//            }
//        }
//    }
//
//    return 0;
//}

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.

输入

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 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

输出

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.

样例输入

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

样例输出

6
3
0

每个区间不能简单维护差值,不好实现,应该维护两个最值。询问的时候存进全局变量。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 200010
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define mid(l,r) (((l)+(r))>>1)
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

struct node {
    int l, r;
    int mn, mx;
}tree[N];

void build(int o, int l, int r)
{//cout<<o<<' ' <<l<<' '<<r<<endl;
    tree[o].l = l; tree[o].r = r;
    if(l == r) {
        scanf("%d", &tree[o].mn);
        tree[o].mx = tree[o].mn;
       // cout << o << endl;
        return;
    }
    build(L, l, mid(l, r));
    build(R, mid(l, r) + 1, r);
    tree[o].mn = min(tree[L].mn, tree[R].mn);
    tree[o].mx = max(tree[L].mx, tree[R].mx);
    //cout << l <<' ' <<r<<' ' <<tree[o].mn<< endl;
}
int minv, maxv;
void query(int o, int l, int r)
{//cout << o <<' ' <<l << ' ' << r << ' ' <<minv <<' ' <<maxv<<endl;
    if(tree[o].l == l && tree[o].r == r) {
        minv = min(minv, tree[o].mn);
        maxv = max(maxv, tree[o].mx);
        return;
    }

    if(r <= mid(tree[o].l, tree[o].r)) query(L, l, r);
    else if(l > mid(tree[o].l, tree[o].r)) query(R, l, r);
    else {
        query(L, l, mid(tree[o].l, tree[o].r));
        query(R, mid(tree[o].l, tree[o].r) + 1, r);
    }
}

int main()
{
  //  freopen("data.txt", "r", stdin);

    int n, q, a, b;
    scanf("%d%d", &n, &q);

    build(1, 1, n);
    while(q--) {
        scanf("%d%d", &a, &b);
        minv = INF; maxv = -INF;
        query(1, a, b);
        printf("%d\n", maxv - minv);
    }

    return 0;
}

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. 

输入

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.

输出

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.

样例输入

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

样例输出

4

这题好难Orz。

首先是反顺序贴,贴一张看一张的思路,然后是离散化的操作 Orz。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 1001000
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define mid(l,r) (((l)+(r))>>1)
#define tr tree[o].r
#define tl tree[o].l
#define tc tree[o].cover
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

struct Post{
   int l, r;
}post[10010];
struct node{
  int l, r;
  bool cover;
}tree[N];
int A[N], Hash[10000001], cnt, ninterval;
void build(int o, int l, int r)
{
    tl = l; tr = r; tc = false;
    if(l == r) return;
    build(L, l, mid(l, r));
    build(R, 1 + mid(l, r), r);
}
bool add(int o, int l, int r)
{// cout << o<<' ' <<tl <<' ' <<tr<<' '<<l<<' ' <<r << ' ' <<tc<<' '<<(tl == l && tr == r)<<endl;
    if( tc ) return false;
    if(tl == l && tr == r) {
        tc = true;
        return true;
    }

    bool flag = false;;
    if(r <= mid(tl, tr)) flag = add(L, l, r);
    else if(l > mid(tl, tr)) flag = add(R, l, r);
    else {
        bool flag1 = add(L, l, mid(tl, tr));
        bool flag2 = add(R, 1 + mid(tl, tr), r);
        flag = flag1 || flag2;
    }
    if(tree[L].cover && tree[R].cover)
        tc = true;
    return flag;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    int t, n;
    scanf("%d", &t);

    while(t--) {
        scanf("%d", &n);
        cnt = 0;
        rep(i, 1, n) {
            scanf("%d%d", &post[i].l, &post[i].r);
            A[cnt++] = post[i].l; A[cnt++] = post[i].r;
        }
        sort(A, A + cnt);
        ninterval = 1;
        cnt = unique(A, A + cnt) - A;
        rep(i, 0, cnt - 2) {
            Hash[A[i]] = ninterval;
            ninterval++;
        }
        Hash[A[cnt - 1]] = ninterval;

        build(1, 1, ninterval);
        int ans = 0;
      //  cout << Hash[post[n].l]<<' ' << Hash[post[n].r]<<' ' <<post[n].l<<' ' <<post[n].r<<endl;
        for(int i = n; i >= 1; i--){
            if(add(1, Hash[post[i].l], Hash[post[i].r]))
                ans++;
        }

        printf("%d\n", ans);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40379678/article/details/81348383
今日推荐