提出 #56317618


ソースコード 拡げる

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define REP_(I,N) for (int I=0,END=(N);I<END;I++)
#define rREP_(I,N) for (int I=(N)-1;I>=0;I--)
#define rep_(I,S,N) for (int I=(S),END=(N);I<END;I++)
#define rrep_(I,S,N) for (int I=(N)-1,START=(S);I>=START;I--)
#define FOR_(I,S,N) for (int I=(S),END=(N);I<=END;I++)
#define rFOR_(I,S,N) for (int I=(N),START=(S);I>=START;I--)

#define DEBUG
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define deputs(str) fprintf(stderr, "%s\n",str)
#else
#define debug(...)
#define deputs(str)
#endif // DEBUG
typedef unsigned long long ULL;
typedef unsigned long long ull;
typedef long long LL;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL maxn=1e6+7;
const double pi=acos(-1.0);
const double eps=1e-10;
template<typename T>inline void pr2(T x,int k=64) {REP_(i,k) printf("%d",(x>>i)&1); putchar(' ');}
template<typename T>inline void max_(T &A,T B) {(A<B) &&(A=B);}
template<typename T>inline void min_(T &A,T B) {(A>B) &&(A=B);}
inline ll fastgcd(ll a, ll b) {  // __gcd()
    if (!b||!a) return a|b;
    ll az=__builtin_ctzll(a),bz=__builtin_ctzll(b),z=min(az,bz),diff; b>>=bz;
    while (a) {
        a>>=az, diff=b-a, az=__builtin_ctzll(diff);
        (b>a)&&(b=a), a=abs(diff);
    }
    return b<<z;
}
int startTime;
void startTimer() {startTime=clock();}
void printTimer() {debug("/--- Time: %ld milliseconds ---/\n",clock()-startTime);}
typedef array<int,5> ar5;
typedef array<int,4> ar4;
typedef array<int,3> ar3;
std::mt19937 rng(time(0));
std::mt19937_64 rng64(time(0));
vector<pii> direction4 = {{-1,0},{0,-1},{0,1},{1,0}};
vector<pii> direction8 = {{-1,-1},{-1,0},{1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

// const int mod = 1e9+7;
const int mod=998244353;
struct mint {
    long long x;
    mint():x(0) {}
    mint(long long x):x((x%mod+mod)%mod) {}
    // mint(long long x):x(x){}
    mint &fix() { x = (x%mod+mod)%mod; return *this; }
    mint operator-() const { return mint(0) - *this; }
    mint operator~() const { return mint(1) / *this; }
    mint &operator+=(const mint &a) { if ((x+=a.x)>=mod) x-=mod; return *this; }
    mint &operator-=(const mint &a) { if ((x+=mod-a.x)>=mod) x-=mod; return *this; }
    mint &operator*=(const mint &a) { (x*=a.x)%=mod; return *this; }
    mint &operator/=(const mint &a) { (x*=a.pow(mod-2).x)%=mod; return *this; }
    mint operator+(const mint &a)const { return mint(*this) += a; }
    mint operator-(const mint &a)const { return mint(*this) -= a; }
    mint operator*(const mint &a)const { return mint(*this) *= a; }
    mint operator/(const mint &a)const { return mint(*this) /= a; }
    mint pow(long long t) const {
        mint ret=1,cur=x;
        for (; t; t>>=1ll,cur=cur*cur)
            if (t&1) ret=ret*cur;
        return ret;
    }
    bool operator<(const mint &a)const { return x < a.x; }
    bool operator==(const mint &a)const { return x == a.x; }
};
istream & operator>>(istream &o,mint &a) { o>>a.x; a=a.fix(); return o; }
ostream & operator<<(ostream &o,const mint &a) { o<<a.x; return o; }


template<class Info>
struct SegmentTree {
    int n;
    std::vector<Info> info;
    SegmentTree() : n(0) {}
    SegmentTree(int n_, Info v_ = Info()) {
        init(n_, v_);
    }
    template<class T>
    SegmentTree(std::vector<T> init_) {
        init(init_);
    }
    void init(int n_, Info v_ = Info()) {
        init(std::vector(n_, v_));
    }
    template<class T>
    void init(std::vector<T> init_) {
        n = init_.size();
        info.assign(4 << std::__lg(n), Info());
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init_[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = info[2 * p] + info[2 * p + 1];
    }
    void modify(int p, int l, int r, int x, const Info &v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int p, const Info &v) {
        modify(1, 0, n, p, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        auto ret = rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
        // printf("%d - %d\n",l,r-1);
        return ret;
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }
    template<class F>
    int findFirst(int p, int l, int r, int x, int y, F pred) {
        if (l >= y || r <= x || !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        int res = findFirst(2 * p, l, m, x, y, pred);
        if (res == -1) {
            res = findFirst(2 * p + 1, m, r, x, y, pred);
        }
        return res;
    }
    template<class F>
    int findFirst(int l, int r, F pred) {
        return findFirst(1, 0, n, l, r, pred);
    }
    template<class F>
    int findLast(int p, int l, int r, int x, int y, F pred) {
        if (l >= y || r <= x || !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        int res = findLast(2 * p + 1, m, r, x, y, pred);
        if (res == -1) {
            res = findLast(2 * p, l, m, x, y, pred);
        }
        return res;
    }
    template<class F>
    int findLast(int l, int r, F pred) {
        return findLast(1, 0, n, l, r, pred);
    }
};

struct Info {
    int l,r,outl,outr;
    ll value;
    Info(int l=0,int r=INF,int outl=0,int outr=INF,ll value=0):l(l),r(r),outl(outl),outr(outr),value(value) {};
};
Info operator+(const Info &c,const Info &b) {
    Info a=c;
    if (a.outl<b.l) {
        if (a.outr<b.l) {
            a.value+=b.l-a.outr;
            a.l=a.r=a.r;  // from r
            a.outl=a.outr=b.l;
        } else {
            a.outl=b.l;
            a.l=b.l;
        }
    }
    if (a.outr>b.r) {
        if (a.outl>b.r) {
            a.value+=a.outl-b.r;
            a.l=a.r=a.l;  // from r
            a.outl=a.outr=b.r;
        } else {
            a.outr=b.r;
            a.r=b.r;
        }
    }
    if (b.outl==b.outr) a.outl=a.outr=b.outl;  // single
    a.value+=b.value;
    // printf("mid-solve a = %d %d %d %d %lld\n",c.l,c.r,c.outl,c.outr,c.value);
    // printf("mid-solve b = %d %d %d %d %lld\n",b.l,b.r,b.outl,b.outr,b.value);
    // printf("mid-solve fin %d %d %d %d %lld\n",a.l,a.r,a.outl,a.outr,a.value);
    return a;
}

int solve() {
    int n;
    scanf("%d",&n);
    vector<Info> info(n+1);
    FOR_(i,1,n) {
        int l,r;
        scanf("%d%d",&l,&r);
        info[i]={l,r,l,r,0};
    }
    SegmentTree<Info> tree(info);
    int q;
    scanf("%d",&q);
    FOR_(i,1,q) {
        int sx,sy,tx,ty;
        scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
        if (sx>tx) swap(sx,tx),swap(sy,ty);
        // Info cur=Info({sy,sy,sy,sy,0});
        // FOR_(x,sx,tx) {
        //     cur=cur+info[x];
        //     printf("solve %d %d %d %d %lld\n",cur.l,cur.r,cur.outl,cur.outr,cur.value);
        // }
        // cur=cur+Info({ty,ty,ty,ty,0});

        Info cur=tree.rangeQuery(sx,tx+1);
        // printf("solve %d %d %d %d %lld\n",cur.l,cur.r,cur.outl,cur.outr,cur.value);
        cur=Info({sy,sy,sy,sy,0})+cur;
        cur=cur+Info({ty,ty,ty,ty,0});
        cur.value+=tx-sx;
        printf("%lld\n",cur.value);
    }
    return 0;
}
int main() {
    // ios_base::sync_with_stdio(false);
    // cin.tie(0), cout.tie(0);
    int T = 1;
    // cin>>T;
    // scanf("%d",&T);
    startTimer();
    FOR_(_, 1, T) { solve(); }
    // printTimer();
}
/*
*/

提出情報

提出日時
問題 F - Takahashi on Grid
ユーザ zlc1114
言語 C++ 20 (gcc 12.2)
得点 575
コード長 8553 Byte
結果 AC
実行時間 294 ms
メモリ 29880 KiB

コンパイルエラー

Main.cpp: In function ‘int solve()’:
Main.cpp:227:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  227 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Main.cpp:231:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  231 |         scanf("%d%d",&l,&r);
      |         ~~~~~^~~~~~~~~~~~~~
Main.cpp:236:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  236 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
Main.cpp:239:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  239 |         scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 575 / 575
結果
AC × 3
AC × 63
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 02_handmade_45.txt, 02_handmade_46.txt, 02_handmade_47.txt, 02_handmade_48.txt, 02_handmade_49.txt, 02_handmade_53.txt, 02_handmade_54.txt, 02_handmade_55.txt, 02_handmade_56.txt, 02_handmade_57.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3844 KiB
00_sample_01.txt AC 1 ms 3920 KiB
00_sample_02.txt AC 1 ms 3844 KiB
01_random_03.txt AC 294 ms 29880 KiB
01_random_04.txt AC 292 ms 29684 KiB
01_random_05.txt AC 292 ms 29880 KiB
01_random_06.txt AC 293 ms 29692 KiB
01_random_07.txt AC 292 ms 29744 KiB
01_random_08.txt AC 293 ms 29692 KiB
01_random_09.txt AC 290 ms 29776 KiB
01_random_10.txt AC 290 ms 29848 KiB
01_random_11.txt AC 293 ms 29796 KiB
01_random_12.txt AC 289 ms 29688 KiB
01_random_13.txt AC 293 ms 29784 KiB
01_random_14.txt AC 291 ms 29652 KiB
01_random_15.txt AC 292 ms 29732 KiB
01_random_16.txt AC 293 ms 29716 KiB
01_random_17.txt AC 291 ms 29780 KiB
01_random_18.txt AC 292 ms 29784 KiB
01_random_19.txt AC 291 ms 29772 KiB
01_random_20.txt AC 293 ms 29840 KiB
01_random_21.txt AC 239 ms 17580 KiB
01_random_22.txt AC 72 ms 16684 KiB
01_random_23.txt AC 112 ms 26872 KiB
01_random_24.txt AC 22 ms 4136 KiB
01_random_25.txt AC 63 ms 10840 KiB
01_random_26.txt AC 173 ms 27132 KiB
01_random_27.txt AC 89 ms 17276 KiB
01_random_28.txt AC 58 ms 17968 KiB
01_random_29.txt AC 162 ms 29456 KiB
01_random_30.txt AC 137 ms 26704 KiB
01_random_31.txt AC 37 ms 9192 KiB
01_random_32.txt AC 158 ms 28424 KiB
01_random_33.txt AC 55 ms 17868 KiB
01_random_34.txt AC 80 ms 4048 KiB
01_random_35.txt AC 156 ms 25192 KiB
01_random_36.txt AC 53 ms 16576 KiB
01_random_37.txt AC 113 ms 4956 KiB
01_random_38.txt AC 149 ms 10716 KiB
01_random_39.txt AC 106 ms 17272 KiB
01_random_40.txt AC 47 ms 4984 KiB
01_random_41.txt AC 119 ms 10592 KiB
01_random_42.txt AC 136 ms 14204 KiB
01_random_43.txt AC 133 ms 14196 KiB
01_random_44.txt AC 54 ms 14860 KiB
01_random_45.txt AC 189 ms 25128 KiB
01_random_46.txt AC 191 ms 24988 KiB
01_random_47.txt AC 182 ms 25092 KiB
01_random_48.txt AC 184 ms 25108 KiB
01_random_49.txt AC 184 ms 18600 KiB
01_random_50.txt AC 184 ms 18656 KiB
01_random_51.txt AC 186 ms 18748 KiB
01_random_52.txt AC 184 ms 18660 KiB
02_handmade_45.txt AC 193 ms 29848 KiB
02_handmade_46.txt AC 192 ms 29716 KiB
02_handmade_47.txt AC 170 ms 29688 KiB
02_handmade_48.txt AC 187 ms 29880 KiB
02_handmade_49.txt AC 70 ms 3732 KiB
02_handmade_53.txt AC 191 ms 29740 KiB
02_handmade_54.txt AC 191 ms 29652 KiB
02_handmade_55.txt AC 169 ms 29788 KiB
02_handmade_56.txt AC 188 ms 29760 KiB
02_handmade_57.txt AC 70 ms 3824 KiB