結果

問題 No.469 区間加算と一致検索の問題
コンテスト
ユーザー bal4u
提出日時 2019-08-11 21:01:08
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 186 ms / 5,000 ms
コード長 1,759 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 311 ms
コンパイル使用メモリ 39,912 KB
最終ジャッジ日時 2026-02-22 04:14:35
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.469 区間加算と一致検索の問題
// bal4u 2019.8.11

#include <stdio.h>

typedef unsigned long long ll;

//// 入出力関係
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(int n) { // 非負整数の表示(出力)
	int i;
	char b[20];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

//// ハッシュ関数
#define HASHSIZ 9999991
typedef struct { ll s; int id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash + HASHSIZ;

int lookup(ll s, int id) {
	HASH *p = hash + (int)(s % HASHSIZ);
	while (p->s) {
		if (p->s == s) return p->id;
		if (++p == hashend) p = hash;
	}
	return id;
}

void insert(ll s, int id) {
	HASH *p = hash + (int)(s % HASHSIZ);
	while (p->s) {
		if (p->s == s) return;
		if (++p == hashend) p = hash;
	}
	p->s = s, p->id = id;
}

//// 疑似乱数発生
#if 0
unsigned xorshift() {
    static unsigned y = 2463534242;
	y = y ^ (y << 13), y = y ^ (y >> 17), y = y ^ (y << 5);
	return y;
}
#endif

ll phash[1000005];
const unsigned r = 723471715;

int main()
{
	int i, N, Q, x;
	ll s;

	N = in(), Q = in();
//	r = xorshift();
	phash[0] = 1; for (i = 1; i <= N; i++) phash[i] = phash[i-1] * r;

	s = 0;
	insert(s+1, 0);
	for (i = 1; i <= Q; i++) {
		x = gc(), gc();
		if (x == '?') out(lookup(s+1, i));
		else {
			int l = in(), r = in(), k = in();
			s += (ll)(phash[r] - phash[l]) * k;
			insert(s+1, i);
		}
	}
	return 0;
}
0