-
Notifications
You must be signed in to change notification settings - Fork 0
/
contestrank.php
executable file
·196 lines (170 loc) · 5.96 KB
/
contestrank.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
$OJ_CACHE_SHARE=true;
$cache_time=10;
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
$view_title= $MSG_CONTEST.$MSG_RANKLIST;
$title="";
require_once("./include/const.inc.php");
require_once("./include/my_func.inc.php");
class TM{
var $solved=0;
var $time=0;
var $p_wa_num;
var $p_ac_sec;
var $user_id;
var $nick;
function TM(){
$this->solved=0;
$this->time=0;
$this->p_wa_num=array(0);
$this->p_ac_sec=array(0);
}
function Add($pid,$sec,$res){
// echo "Add $pid $sec $res<br>";
if (isset($this->p_ac_sec[$pid])&&$this->p_ac_sec[$pid]>0)
return;
if ($res!=4){
if(isset($this->p_wa_num[$pid])){
$this->p_wa_num[$pid]++;
}else{
$this->p_wa_num[$pid]=1;
}
}else{
$this->p_ac_sec[$pid]=$sec;
$this->solved++;
if(!isset($this->p_wa_num[$pid])) $this->p_wa_num[$pid]=0;
$this->time+=$sec+$this->p_wa_num[$pid]*1200;
// echo "Time:".$this->time."<br>";
// echo "Solved:".$this->solved."<br>";
}
}
}
function s_cmp($A,$B){
// echo "Cmp....<br>";
if ($A->solved!=$B->solved) return $A->solved<$B->solved;
else return $A->time>$B->time;
}
// contest start time
if (!isset($_GET['cid'])) die("No Such Contest!");
$cid=intval($_GET['cid']);
$sql="SELECT `start_time`,`title`,`end_time` FROM `contest` WHERE `contest_id`='$cid'";
//$result=mysql_query($sql) or die(mysql_error());
//$rows_cnt=mysql_num_rows($result);
if($OJ_MEMCACHE){
require("./include/memcache.php");
$result = mysql_query_cache($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=count($result);
else $rows_cnt=0;
}else{
$result = mysql_query($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=mysql_num_rows($result);
else $rows_cnt=0;
}
$start_time=0;
$end_time=0;
if ($rows_cnt>0){
// $row=mysql_fetch_array($result);
if($OJ_MEMCACHE)
$row=$result[0];
else
$row=mysql_fetch_array($result);
$start_time=strtotime($row['start_time']);
$end_time=strtotime($row['end_time']);
$title=$row['title'];
}
if(!$OJ_MEMCACHE)mysql_free_result($result);
if ($start_time==0){
$view_errors= "No Such Contest";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
if ($start_time>time()){
$view_errors= "Contest Not Started!";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
if(!isset($OJ_RANK_LOCK_PERCENT)) $OJ_RANK_LOCK_PERCENT=0;
$lock=$end_time-($end_time-$start_time)*$OJ_RANK_LOCK_PERCENT;
//echo $lock.'-'.date("Y-m-d H:i:s",$lock);
$sql="SELECT count(1) as pbc FROM `contest_problem` WHERE `contest_id`='$cid'";
//$result=mysql_query($sql);
if($OJ_MEMCACHE){
// require("./include/memcache.php");
$result = mysql_query_cache($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=count($result);
else $rows_cnt=0;
}else{
$result = mysql_query($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=mysql_num_rows($result);
else $rows_cnt=0;
}
if($OJ_MEMCACHE)
$row=$result[0];
else
$row=mysql_fetch_array($result);
//$row=mysql_fetch_array($result);
$pid_cnt=intval($row['pbc']);
if(!$OJ_MEMCACHE)mysql_free_result($result);
$sql="SELECT
users.user_id,users.nick,solution.result,solution.num,solution.in_date
FROM
(select * from solution where solution.contest_id='$cid' and num>=0 ) solution
left join users
on users.user_id=solution.user_id
ORDER BY users.user_id,in_date";
//echo $sql;
//$result=mysql_query($sql);
if($OJ_MEMCACHE){
// require("./include/memcache.php");
$result = mysql_query_cache($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=count($result);
else $rows_cnt=0;
}else{
$result = mysql_query($sql);// or die("Error! ".mysql_error());
if($result) $rows_cnt=mysql_num_rows($result);
else $rows_cnt=0;
}
$user_cnt=0;
$user_name='';
$U=array();
for ($i=0;$i<$rows_cnt;$i++){
if($OJ_MEMCACHE)
$row=$result[$i];
else
$row=mysql_fetch_array($result);
$n_user=$row['user_id'];
if (strcmp($user_name,$n_user)){
$user_cnt++;
$U[$user_cnt]=new TM();
$U[$user_cnt]->user_id=$row['user_id'];
$U[$user_cnt]->nick=$row['nick'];
$user_name=$n_user;
}
if(time()<$end_time&&$lock<strtotime($row['in_date']))
$U[$user_cnt]->Add($row['num'],strtotime($row['in_date'])-$start_time,0);
else
$U[$user_cnt]->Add($row['num'],strtotime($row['in_date'])-$start_time,intval($row['result']));
}
if(!$OJ_MEMCACHE) mysql_free_result($result);
usort($U,"s_cmp");
////firstblood
$first_blood=array();
for($i=0;$i<$pid_cnt;$i++){
$sql="select user_id from solution where contest_id=$cid and result=4 and num=$i order by in_date limit 1";
$result=mysql_query($sql);
$row_cnt=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if($row_cnt==1){
$first_blood[$i]=$row['user_id'];
}else{
$first_blood[$i]="";
}
}
/////////////////////////Template
require("template/".$OJ_TEMPLATE."/contestrank.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
require_once('./include/cache_end.php');
?>