CodeForces Round #179 (295A) - Greg and Array

题目链接:http://codeforces.com/problemset/problem/295/A

我的做法,两次线段树

#include <cstdio>
#include <cstring>

const int N = 100005;

long long sumv[N * 3];
long long add[N * 3];
long long a[N];

struct opera {
    int l, r;
    long long d;
} op[N];

void pushdown(int o, int l, int r)
{
    int m = (l + r) >> 1;
    sumv[o << 1] += add[o] * (m - l + 1);
    add[o << 1] += add[o];
    sumv[o << 1 | 1] += add[o] * (r - m);
    add[o << 1 | 1] += add[o];
    add[o] = 0;
}

int ql, qr;
long long val;
void update(int o, int l, int r)
{
    if (ql <= l && qr >= r) {
        sumv[o] += (r - l + 1) * val;
        add[o] += val;
        return ;
    }
    if (add[o]) pushdown(o, l, r);
    int m = (l + r) >> 1;
    if (m < qr) update(o << 1 | 1, m + 1, r);
    if (m >= ql) update(o << 1, l, m);
    sumv[o] = sumv[o << 1] + sumv[o << 1 | 1];
}

int q;
long long query(int o, int l, int r)
{
    if (l == r) return sumv[o];
    if (add[o]) pushdown(o, l, r);
    int m = (l + r) >> 1;
    if (m < q) return query(o << 1 | 1, m + 1, r);
    else return query(o << 1, l, m);
}

int main()
{
    //freopen("a.in", "r", stdin);

    int n, m, k;
    int i;
    scanf("%d%d%d", &n, &m, &k);
    for (i = 1; i <= n; ++i) scanf("%lld", a + i);
    for (i = 1; i <= m; ++i) {
        scanf("%d%d%lld", &op[i].l, &op[i].r, &op[i].d);
    }
    val = 1;
    for (i = 1; i <= k; ++i) {
        scanf("%d%d", &ql, &qr);
        update(1, 1, m);
    }
    for (i = 1; i <= m; ++i) {
        q = i;
        op[i].d *= query(1, 1, m);
    }
    memset(sumv, 0, sizeof sumv);
    memset(add, 0, sizeof add);
    for (i = 1; i <= m; ++i) {
        ql = op[i].l;
        qr = op[i].r;
        val = op[i].d;
        update(1, 1, n);
    }
    for (i = 1; i <= n; ++i) {
        q = i;
        printf("%lld", a[i] + query(1, 1, n));
        if (i != n) printf(" ");
    }
    return 0;
}

  

后来看了学长的代码,又写了一遍.......:

#include <cstdio>

const int N = 100005;

long long l[N];
long long r[N];
long long d[N];
long long a[N];
long long op[N];
long long b[N];

main()
{
    //freopen("in.txt", "r", stdin);
    long long n, m, k;
    scanf("%lld%lld%lld", &n, &m, &k);
    for (int i = 1; i <= n; ++i)
        scanf("%lld", &a[i]);
    for (int i = 1; i <=  m; ++i)
        scanf("%lld%lld%lld", &l[i], &r[i], &d[i]);
    long long x, y;
    for (int i = 1; i <= k; ++i) {
        scanf("%lld%lld", &x, &y);
        ++op[x];
        --op[y+1];
    }
    for (int i = 1; i <= m; ++i) {
        op[i] += op[i - 1];
        d[i] *= op[i];
    }
    for (int i = 1; i <= m; ++i) {
        b[l[i]] += d[i];
        b[r[i]+1] -= d[i];
    }
    for (int i = 1; i <= n; ++i) {
        b[i] += b[i - 1];
        a[i] += b[i];
    }
    printf("%lld", a[1]);
    for (int i = 2; i <= n; ++i)
        printf(" %lld", a[i]);
}

  

时间: 2024-11-05 20:33:55

CodeForces Round #179 (295A) - Greg and Array的相关文章

Codeforces Round #179 (Div. 2)---C. Greg and Array

Greg has an array a?=?a1,?a2,?-,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?-,?ri by value di. Greg wrote down k queries

Codeforces 295A. Greg and Array

题目链接:http://codeforces.com/problemset/problem/295/A 题意: 给你一个含有 n 个数的数组, 以及 m 个形如 ( l,  r,  v) 的操作,代表把这个数组从第 l 个到第 r 个全部加 v; 然后再给 k 个形如 ( l, r) 的操作,代表对于这 m 个操作, 分别执行第 a 个操作到第 b 个操作各一次. 思路: 这道题可以分解成两个同样的子问题,设原始数组为 a, 第一个操作为 b, 第二个操作为 c, 可以看到 b 是对于 a 数组

Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n steps.

Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s,

Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings

题目链接:点这里!!!! 题意: 给定两个长度为n(n<=1e5)的串s,w,串中能包含'0'~'9','?','?'可以代替'0'~'9'中任何一个数字. 问你能组成多少对特殊的串('?'代替为数字后的s,w),使得存在(1<=i,j<=n)si<wi,sj>wj. 题解: 1.我们假设'?'的总个数为num,则能够成的总情况为10^num. 2.我们可以通过求相反的情况来求出答案. 3.可以分为4种情况. (1)已知存在(1<=i,j<=n)si<wi,

Codeforces Round #284 (Div. 1) C. Array and Operations 二分图匹配

因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #defin

Codeforces Round #531 (Div. 3)

Codeforces Round #531 (Div. 3) B. Array K-Coloring Description You are given an array aa consisting of nn integer numbers. You have to color this array in kk colors in such a way that: Each element of the array should be colored in some color; For ea

Educational Codeforces Round 23 D. Imbalanced Array(单调栈)

题目链接:Educational Codeforces Round 23 D. Imbalanced Array 题意: 给你n个数,定义一个区间的不平衡因子为该区间最大值-最小值. 然后问你这n个数所有的区间的不平衡因子和 题解: 对每一个数算贡献,a[i]的贡献为 当a[i]为最大值时的 a[i]*(i-l+1)*(r-i+1) - 当a[i]为最小值时的a[i]*(i-l+1)*(r-i+1). 计算a[i]的l和r时,用单调栈维护.具体看代码,模拟一下就知道了. 然后把所有的贡献加起来.

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma