結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-18 23:41:51 |
| 言語 | Java (openjdk 23) |
| 結果 |
RE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,622 bytes |
| コンパイル時間 | 2,316 ms |
| コンパイル使用メモリ | 79,704 KB |
| 実行使用メモリ | 50,308 KB |
| 最終ジャッジ日時 | 2024-06-25 06:02:45 |
| 合計ジャッジ時間 | 5,830 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 1 RE * 14 |
ソースコード
import java.util.*;
import java.io.*;
class Main{
public static void main(String[] args){
new Main().run();
}
void run(){
Scanner sc=new Scanner();
int n=sc.nextInt();
int[][] a=new int[n][];
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<n;++i){
int q=sc.nextInt();
if(q==0){
a[i]=new int[2];
a[i][0]=q;
a[i][1]=sc.nextInt();
list.add(a[i][1]);
}else if(q==1){
a[i]=new int[3];
a[i][0]=q;
a[i][1]=sc.nextInt();
a[i][2]=sc.nextInt();
list.add(a[i][1]);
list.add(a[i][2]);
}
}
Collections.sort(list);
long ret=0;
BIT bit=new BIT(list.size());
for(int i=0;i<n;++i){
if(a[i][0]==0){
a[i][1]=Collections.binarySearch(list,a[i][1]);
}else{
a[i][1]=Collections.binarySearch(list,a[i][1]);
a[i][2]=Collections.binarySearch(list,a[i][2]);
}
}
for(int i=0;i<n;++i){
if(a[i][0]==0){
bit.add(a[i][1],1);
}else{
ret+=bit.sum(a[i][1],a[i][2]);
}
}
System.out.println(ret);
}
class BIT{
int n;
long[] arr;
public BIT(int n_){
n=n_;
arr=new long[n_+1];
}
void add(int k,long val){
++k;
while(k<=n){
arr[k]+=val;
k+=k&-k;
}
}
long sum(int k){
++k;
long ret=0;
while(k>0){
ret+=arr[k];
k-=k&-k;
}
return ret;
}
long sum(int l,int r){
return sum(r)-sum(l-1);
}
}
class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
}
}