結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
iwkjosec
|
| 提出日時 | 2019-10-05 17:28:44 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 127 ms / 2,000 ms |
| コード長 | 4,063 bytes |
| コンパイル時間 | 1,087 ms |
| コンパイル使用メモリ | 115,688 KB |
| 実行使用メモリ | 22,144 KB |
| 最終ジャッジ日時 | 2024-10-06 11:38:00 |
| 合計ジャッジ時間 | 3,478 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;
class Program
{
static void Main()
{
SetOut(new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false });
var sc = new StreamScanner(OpenStandardInput());
var N = sc.Int();
var Q = sc.Int();
var a=new Pair[N];
for (int i = 0; i < a.Length; i++)
{
a[i]=new Pair(i+1,sc.Int());
}
var s = new SegmentTree<Pair>(a, Mindex, new Pair(0, int.MaxValue));
for (int i = 0; i < Q; i++)
{
var q = sc.Int();
var l = sc.Int() - 1;
var r = sc.Int() + (q == 1 ? -1 : 0);
if (q == 1)
{
var k = s[l].Value;
s[l] = new Pair(l + 1, s[r].Value);
s[r] = new Pair(r + 1, k);
}
else
{
WriteLine(s.Query(l, r).Key);
}
}
Out.Flush();
}
static Pair Mindex(Pair l, Pair r) => l.Value < r.Value ? l : r;
}
public class SegmentTree<T>
{
T[] buffer;//0-index
int N;
public int Length => N;
T TI;
Func<T, T, T> F;//デリゲート遅そう
public SegmentTree(int length, Func<T, T, T> f, T ti)//モノイドが直接渡せれば C#9.0ではいるか
{
N = length;
buffer = new T[N * 2 - 1];
for (int i = 0; i < buffer.Length; i++) buffer[i] = ti; //!
F = f;
TI = ti;
}
public SegmentTree(T[] init, Func<T, T, T> f, T ti)
{
N = init.Length;
buffer = new T[N * 2 - 1];
Array.Copy(init, 0, buffer, N - 1, init.Length);
for (int i = N - 2; i >= 0; i--)
{
buffer[i] = f(buffer[i * 2 + 1], buffer[i * 2 + 2]);
}
F = f;
TI = ti;
}
public void Update(int k, T v)//k [0, N)
{
k += N - 1;
buffer[k] = v;
while (k > 0)
{
k = (k - 1) >> 1;
buffer[k] = F(buffer[k * 2 + 1], buffer[k * 2 + 2]);
}
}
public T this[int k]
{
set//setの式形式はC#7.0以降
{
Update(k, value);
}
get
{
return buffer[k + N - 1];
}
}
public T Query(int l, int r)//[l, r) 0 <= l, r <= N // 0 <= r - l のとき空区間
{
var lv = TI;
var rv = TI;
l += N - 1;
r += N - 1;
while (l < r)
{
if ((l & 1) == 0)
{
lv = F(lv, buffer[l]);
}
if ((r & 1) == 0)
{
rv = F(buffer[r - 1], rv);
}
l >>= 1;
r = (r - 1) >> 1;
}
return F(lv, rv);
}
}
public class StreamScanner
{
public StreamScanner(System.IO.Stream stream)
{
str = stream;
}
public readonly System.IO.Stream str;
private readonly byte[] buf = new byte[1024];
private int len, ptr;
public bool isEof;
public bool IsEndOfStream
{
get { return isEof; }
}
private byte read()
{
if (isEof) return 0;
if (ptr < len) return buf[ptr++];
ptr = 0;
if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++];
isEof = true;
return 0;
}
public long Long()
{
if (isEof) return long.MinValue;
long ret = 0;
byte b;
var ng = false;
do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b));
if (b == 0) return long.MinValue;
if (b == '-')
{
ng = true;
b = read();
}
for (; ; b = read())
{
if (b < '0' || '9' < b)
return ng ? -ret : ret;
ret = ret * 10 + b - '0';
}
}
public int Int()
{
return (isEof) ? int.MinValue : (int)Long();
}
}
iwkjosec