提出 #15647373


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

struct node {
    int val;
    int data;
    node *left, *right;
    node () {}
    node (node *l, node *r, int _) {
        left = l;
        right = r;
        val = _;
    }
};

void build(node *n, int l, int r) {
    if(l == r) {
        n->val = 0;
        return;
    }
    int mid = (l + r) / 2;
    n->left = new node(NULL, NULL, 0);
    n->right = new node(NULL, NULL, 0);
    build(n->left,l, mid);
    build(n->right,mid + 1, r);
    n->val = n->left->val + n->right->val;
}

void update(node *last, node *cur, int l, int r, int pos, int x) {
    if(l == r) {
        cur->val = x;
        return;
    }
    int mid = (l + r) / 2;
    if(pos <= mid) {
        cur->right = last->right;
        cur->left = new node(NULL, NULL, 0);
        update(last->left, cur->left, l, mid, pos, x);
    } else {
        cur->left = last->left;
        cur->right = new node(NULL, NULL, 0);
        update(last->right, cur->right, mid + 1, r, pos, x);
    }
    cur->val = cur->right->val + cur->left->val;
}

int query(node *n, int l, int r, int i, int j) {
    if(i > r or j < l) return -1;
    if(i <= l and r <= j) return n->val;
    int mid = (l+r)/2;
    int p1 = query(n->left,l,mid,i,j);
    int p2 = query(n->right,mid+1,r,i,j);
    if(p1 == -1) return p2;
    if(p2 == -1) return p1;
    return p1 + p2;
}

vector<node*> version;
int v[500010];
int a[500010];

int main() {

    int n,q;
    scanf("%d %d", &n, &q);
    
    node *root = new node(NULL,NULL,0);

    build(root,0, n + 10);

    version.push_back(root);
    for (int i = 0; i < n; ++i)
        cin >> v[i];

    for (int i = n - 1; i >= 0; i--) {
        node *nv = new node(NULL,NULL,0);
        update(version.back(), nv, 0, n + 10, a[v[i]], 0);
        node *nv2 = new node(NULL,NULL,0);
        update(nv, nv2, 0, n + 10, i, 1);
        version.push_back(nv2);
        a[v[i]] = i;
    }

    reverse(version.begin(), version.end());

    while(q--) {
        int x, y;
        cin >> x >> y;
        x--,y--;
        printf("%d\n", query(version[x], 0, n + 10, x, y));
    }

    return 0;
}

提出情報

提出日時
問題 F - Range Set Query
ユーザ mijatola
言語 C++ (GCC 9.2.1)
得点 0
コード長 2210 Byte
結果 TLE
実行時間 2231 ms
メモリ 667244 KiB

コンパイルエラー

./Main.cpp: In function ‘int main()’:
./Main.cpp:66:10: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   66 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 600
結果
AC × 2
AC × 4
TLE × 3
セット名 テストケース
Sample sample00, sample01
All handmade02, handmade03, handmade04, handmade05, random06, sample00, sample01
ケース名 結果 実行時間 メモリ
handmade02 AC 6 ms 3732 KiB
handmade03 AC 478 ms 121224 KiB
handmade04 TLE 2231 ms 667244 KiB
handmade05 TLE 2228 ms 663976 KiB
random06 TLE 2227 ms 629356 KiB
sample00 AC 7 ms 3700 KiB
sample01 AC 3 ms 3572 KiB