4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

paiza POH paizen #paizahack_02 [JavaScript]

Last updated at Posted at 2014-04-17
問題 https://paiza.jp/poh/paizen
タイム一覧/Ruby/C/C++/C#/Java http://qiita.com/cielavenir/items/17f66daa2be639fd74f3
Perl/PHP/Python http://qiita.com/cielavenir/items/5b57808a28a8b4c5c7b2
JavaScript http://qiita.com/cielavenir/items/9dab2bfbcfa0047b345f
CoffeeScript/Go/Scala/VB/F# http://qiita.com/cielavenir/items/32591dd03376ba534d1e

ある操作を行うことでJavaScriptによる提出が可能になります。
が、メールは来ましたがWebは500になってしまいました。
ですので、サーバーに迷惑かかるかもなんで真似しないで下さい…。

[追記・訂正]
入力が巨大なため、JavaScriptは入力に注意が必要なだけのようです。

JavaScript (0.37/0.33)

#!/usr/bin/node
(function(){
	var T=[];
	var stdin = process.openStdin();
	stdin.setEncoding('utf8');

	var input_fragment="";
	stdin.on('data', function(input) {
		var i=0,len,ref,x=[];
		ref=(input_fragment+input).split("\n");
		input_fragment=ref.pop();
		for(len=ref.length;i<len;i++){
    		if(ref[i]=='')continue;
    		x.push(ref[i]);
		}
		T=T.concat(x);
	});

	stdin.on('end', function(z) {
		if(input_fragment){
			var i=0,len,ref,x=[];
			ref=(input_fragment+"\n").split("\n");
			input_fragment=ref.pop();
			for(len=ref.length;i<len;i++){
    			if(ref[i]=='')continue;
    			x.push(ref[i]);
			}
			T=T.concat(x);
		}

		var arg=T[0].split(' ').map(Number);
		var h=arg[0];
		var w=arg[1];
		var a=Array(h);
		for(var i=0;i<h;i++){
			var line=T[1+i];
			a[i]=Array(w);
			for(var j=0;j<w;j++){
				a[i][j]=Array(h);
				a[i][j][0]=line[j]^1;
			}
		}
		var accum=Array(h);
		for(var i=0;i<h;i++){
			accum[i]=Array(w+1);
			for(var j=0;j<=w;j++)accum[i][j]=0;
		}
		for(var i=0;i<h;i++){
			accum[0][a[i][0][0]]++;
			for(var j=1;j<w;j++){
				if(a[i][j][0]){
					a[i][j][0]+=a[i][j-1][0];
					accum[0][a[i][j][0]]++;
				}
			}
		}
		for(var i=1;i<h;i++)for(var j=0;j<w;j++)for(var k=1;k<=i;k++){
			a[i][j][k]=a[i-1][j][k-1];
			if(a[i][j][k]>a[i][j][k-1]){a[i][j][k]=a[i][j][k-1];}
			if(!a[i][j][k])break;
			accum[k][a[i][j][k]]++;
		}
		for(var i=0;i<h;i++)for(var j=w-1;j>=0;j--)accum[i][j]+=accum[i][j+1];
		var k=Number(T[1+h]);
		for(var i=0;i<k;i++){
			arg=T[1+h+1+i].split(' ').map(Number);
			var s=arg[0];
			var t=arg[1];
			console.log(s<=h&&t<=w?accum[s-1][t]:0);
		}
	});
})();
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?