Submission #3381598


Source Code Expand

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'
#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 1e9+7;

template<typename T>
struct delay_segtree {
private:
    int N;
    vector<T> node, lazy;
    //例外値 ex)INF,0
    //-------書くとこ---------------------
    T init_value = 0;
    T default_value = INF;
    T default_lazy = 0;
    static inline T merge(const T& u, const T& v) {
        return min(u,v);
    }
    static inline void lazy_transmit(T& u, const T& v) {
        u += v;
    }
    static inline void node_transmit(T& u,const T& v,int l,int r){
        u += v;
    }
    //------------------------------------
    // k 番目のノードについて遅延評価を行う
    void eval(int k, int l, int r) {
        // 遅延配列が空でない場合、自ノード及び子ノードへの
        // 値の伝播が起こる
        if (lazy[k] != default_lazy) {
            node_transmit(node[k], lazy[k],l,r);
            // 最下段かどうかのチェックをしよう
            if (r - l > 1) {
                lazy_transmit(lazy[2 * k + 1], lazy[k]);
                lazy_transmit(lazy[2 * k + 2], lazy[k]);
            }
            // 伝播が終わったので、自ノードの遅延配列を空にする
            lazy[k] = default_lazy;
        }
    }
public:
    delay_segtree(int n) {
        for(N=1;N<n;N*=2);
        node.resize(2 * N, init_value);
        lazy.resize(2 * N, default_lazy);
    }
    delay_segtree(vector<int> v) {
        int sz = v.size();
        for(N=1;N<sz;N*=2);
        node.resize(2 * N, init_value);
        lazy.resize(2 * N, default_lazy);
        for (int i = 0; i < sz; i++)
            node[i + N - 1] = v[i];
        for (int i = N - 2; i >= 0; i--)
            node[i] = merge(node[2 * i + 1], node[2 * i + 2]);
    }
    void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = N;
        // k 番目のノードに対して遅延評価を行う
        eval(k, l, r);
        // 範囲外なら何もしない
        if (b <= l || r <= a)
            return;
        // 完全に被覆しているならば、遅延配列に値を入れた後に評価
        if (a <= l && r <= b) {
            lazy_transmit(lazy[k], x);
            eval(k, l, r);
        }
        // そうでないならば、子ノードの値を再帰的に計算して、
        // 計算済みの値をもらってくる
        else {
            update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            node[k] = merge(node[2 * k + 1], node[2 * k + 2]);
        }
    }
    // update k th element
    void update(int k, T val) {
        k += N - 1; // leaf
        node[k] = val;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    void add(int k, T val) {
        k += N - 1; // leaf
        node[k] += val;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    // [a, b)
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = N;
        eval(k, l, r);
        if (r <= a or b <= l)
            return default_value;
        if (a <= l and r <= b)
            return node[k];
        int m = (l + r) / 2;
        T vl = query(a, b, k * 2 + 1, l, m);
        T vr = query(a, b, k * 2 + 2, m, r);
        return merge(vl, vr);
    }

    T operator[](const int &k) {
        //遅延評価をまずしておく
        query(k, k + 1);
        return node[k + N - 1];
    }
};


int main(){
  int N;cin>>N;
  vll a(N);
  repeat(i,N){
    cin>>a[i];
  }
  for(int i=N-1;i>0;i--){
    a[i] -= a[i-1];
  }
  int A;cin>>A;
  delay_segtree<ll> seg(A+1);
  vector<pair<Pii,ll> > L(A),R(A);
  repeat(i,A){
    ll X;
    cin>>L[i].first.first>>R[i].first.first>>X;
    L[i].first.first--;
    L[i].first.second=R[i].first.second=i+1;
    R[i].second=L[i].second=X;
  }
  sort(L.begin(),L.end());
  sort(R.begin(),R.end());
  int B;cin>>B;
  vector<pair<Pii,Pii> > query(B);
  repeat(i,B){
    cin>>query[i].second.first>>query[i].second.second>>query[i].first.first;
    query[i].second.first--;query[i].second.second++;query[i].first.first--;
    query[i].first.second=i;
  }
  sort(query.begin(),query.end());
  vll ans(B);
  int l=0,r=0,q=0;
  repeat(i,N){
    seg.update(0,A+1,a[i]);
    while(l<A&&L[l].first.first==i){
      seg.update(L[l].first.second,A+1,L[l].second);
      l++;
    }
    while(r<A&&R[r].first.first==i){
      seg.update(R[r].first.second,A+1,-R[r].second);
      r++;
    }
    //debugArray(seg,A+1);
    while(q<B&&query[q].first.first==i){
      int s,t;
      tie(s,t)=query[q].second;
      ans[query[q].first.second] = seg.query(s,t);
      q++;
    }
  }
  repeat(i,B){
    cout << ans[i] << endl;
  }
  return 0;
}

Submission Info

Submission Time
Task D - 天下一数列にクエリを投げます
User hashiryo
Language C++14 (GCC 5.4.1)
Score 900
Code Size 5834 Byte
Status AC
Exec Time 574 ms
Memory 11776 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 3
AC × 24
Set Name Test Cases
Sample example0, example1, example2
All example0, example1, example2, maxrand0, maxrand1, maxrand2, medrand0, medrand1, medrand2, medrand3, medrand4, overflow0, overflow1, overflow2, posneg0, posneg1, rand0, rand1, rand2, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4
Case Name Status Exec Time Memory
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 574 ms 11648 KB
maxrand1 AC 555 ms 11648 KB
maxrand2 AC 559 ms 11520 KB
medrand0 AC 3 ms 256 KB
medrand1 AC 5 ms 256 KB
medrand2 AC 7 ms 384 KB
medrand3 AC 6 ms 256 KB
medrand4 AC 8 ms 384 KB
overflow0 AC 564 ms 11776 KB
overflow1 AC 559 ms 11776 KB
overflow2 AC 557 ms 11776 KB
posneg0 AC 551 ms 11648 KB
posneg1 AC 556 ms 11520 KB
rand0 AC 318 ms 5376 KB
rand1 AC 289 ms 4992 KB
rand2 AC 342 ms 9088 KB
smallrand0 AC 1 ms 256 KB
smallrand1 AC 1 ms 256 KB
smallrand2 AC 1 ms 256 KB
smallrand3 AC 1 ms 256 KB
smallrand4 AC 1 ms 256 KB