求hack

P3372 【模板】线段树 1

GabrielAC_CSPS @ 2024-01-28 14:35:09

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int _N = 500;
int n, m; int q, num;
ll a[N];
ll sum[_N], lazy[_N];
void init()
{
    q = sqrt(n), num = q+(q*q<n);
    for (int i=1;i<=n;++i)
    {
        sum[i/q-(i%q==0)]+=a[i];
    }
    //for (int i=0;i<num;++i)printf("%d ", sum[i]); printf("\n");
}
void change(int l, int r, int v)
{
    int i1 = l/q, i2 = r/q;
    int m1 = l%q, m2 = r%q;
    int p1 = i1+(m1!=0&&m1!=1), p2 = i2-1;
    for (int i=p1;i<=p2;++i)
        lazy[i] += v;
    for (int i=l;i<=p1*q&&i<=r;++i)
    {
        a[i]+=v;
        sum[i/q-(i%q==0)]+=v;
    }
    if(l/q-(l%q==0) != r/q-(r%q==0))
    for (int i=r;i>i2*q&&i>=l;--i)
    {
        a[i]+=v;
        sum[i/q-(i%q==0)]+=v;
    }
    //for (int i=1;i<=n;++i) printf("%lld ", a[i]); printf("\n");
    //for (int i=0;i<num;++i) printf("%lld ", lazy[i]); printf("\n");
    //printf("%d %d\n", p1, p2);
}
ll query(int l, int r)
{
    ll ans = 0;
    int i1 = l/q, i2 = r/q;
    int m1 = l%q, m2 = r%q;
    int p1 = i1+(m1!=0&&m1!=1), p2 = i2-1;
    for (int i=p1;i<=p2;++i)
        ans += sum[i]+q*lazy[i];
    for (int i=l;i<=p1*q&&i<=r;++i)
        ans += a[i]+lazy[i/q-(i%q==0)];
    if(l/q-(l%q==0) != r/q-(r%q==0))
    for (int i=r;i>i2*q&&i>=l;--i)
        ans += a[i]+lazy[i/q-(i%q==0)];
    return ans;
}
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;++i) scanf("%lld", &a[i]);
    init();
    scanf("%d", &m);
    for (int i=1;i<=m;++i)
    {
        int op, l, r, v;
        scanf("%d%d%d", &op, &l, &r);
        if(op==1)
        {
            scanf("%d", &v);
            change(l, r, v);
        }
        else{
            printf("%lld\n", query(l, r));
        }
    }
    return 0;
}

by Super_Cube @ 2024-01-28 14:43:05

@GabrielAC_CSPS 你这个样例都过不了


by Special_Tony @ 2024-01-28 14:47:25

@GabrielAC_CSPS

Hack.in:

5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4

Hack.out

11
8
20

by GabrielAC_CSPS @ 2024-01-30 14:26:07

@Super_Cube 是这样的,但能AC hhhh


by GabrielAC_CSPS @ 2024-01-30 14:26:45

@Super_Cube 欸放错代码了


by GabrielAC_CSPS @ 2024-01-30 14:34:46

不用hack了,正确代码放这hhh

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int _N = 500;
int n, m; int q, num;
ll a[N];
ll sum[_N], lazy[_N];
int group[N];
void init()
{
    q = sqrt(n), num = q+(q*q<n);
    for (int i=1;i<=n;++i)
    {
        sum[i/q-(i%q==0)]+=a[i];
    }
    //for (int i=0;i<num;++i)printf("%d ", sum[i]); printf("\n");
    for (int i=1;i<=n;++i)
        group[i] = i/q-(i%q==0);
}
bool check(int l, int r)//检查l,r是否在同一块
{
    return group[l]==group[r];
}
void change(int l, int r, int v)
{
    int i1 = l/q, i2 = r/q;
    int m1 = l%q, m2 = r%q;
    int p1 = i1+(m1!=0&&m1!=1), p2 = i2-1;
    if(!check(l, r)){
        for (int i=p1;i<=p2;++i)
            lazy[i] += v;
        for (int i=l;i<=p1*q&&i<=r;++i){
            a[i]+=v;
            sum[group[i]]+=v;
        }
        for (int i=r;i>i2*q&&i>=l;--i){
            a[i]+=v;
            sum[group[i]]+=v;
        }
    }
    else
        for (int i=l;i<=r;++i){
            a[i]+=v;
            sum[group[i]]+=v;
        }
}
ll query(int l, int r)
{
    ll ans = 0;
    int i1 = l/q, i2 = r/q;
    int m1 = l%q, m2 = r%q;
    int p1 = i1+(m1!=0&&m1!=1), p2 = i2-1;
    if(!check(l, r)){
        for (int i=p1;i<=p2;++i)
            ans += sum[i]+q*lazy[i];
        for (int i=l;i<=p1*q&&i<=r;++i)
            ans += a[i]+lazy[group[i]];
        for (int i=r;i>i2*q&&i>=l;--i)
            ans += a[i]+lazy[group[i]];
    }
    else
        for (int i=l;i<=r;++i)
            ans += a[i]+lazy[group[i]];
    return ans;
}
int main()
{
    scanf("%d", &n);
    for (int i=1;i<=n;++i) scanf("%lld", &a[i]);
    init();
    scanf("%d", &m);
    for (int i=1;i<=m;++i)
    {
        int op, l, r, v;
        scanf("%d%d%d", &op, &l, &r);
        if(op==1)
        {
            scanf("%d", &v);
            change(l, r, v);
        }
        else{
            printf("%lld\n", query(l, r));
        }
    }
    return 0;
}

楼主集训营的题库刷得差不多了有些题荒,所以胡了点冷门算法结果洛谷AC了自己网站没A所以发了这个贴子hhh

后来remake了但没有过来及时删帖 就酱


|