problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p01554
|
C++
|
Runtime Error
|
#include <iostream>
#include <string>
using namespace std;
int main() {
int N, M;
int frag = 0;
string U[255];
string T;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> U[i];
}
cin >> M;
for (int j = 0; j < M; j++) {
cin >> T;
for (int i = 0; i < N; i++) {
if (T == U[i]) {
frag += 1;
if (frag % 2 == 0) {
cout << "Closed by " << T << endl;
}
else {
cout << "Opened by " << T << endl;
}
break;
}
if (i == N - 1) {
cout << "Unknown " << T << endl;
}
}
}
return 0;
}
|
#include <iostream>
#include <string>
using namespace std;
int main() {
int N, M;
int frag = 0;
string U[256];
string T;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> U[i];
}
cin >> M;
for (int j = 0; j < M; j++) {
cin >> T;
for (int i = 0; i < N; i++) {
if (T == U[i]) {
frag += 1;
if (frag % 2 == 0) {
cout << "Closed by " << T << endl;
}
else {
cout << "Opened by " << T << endl;
}
break;
}
if (i == N - 1) {
cout << "Unknown " << T << endl;
}
}
}
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p01554
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
int main(void) {
int n;
cin >> n;
string id[11];
for (int i = 0; i < n; i++) {
cin >> id[i];
}
int lock = 0;
int m;
cin >> m;
string x;
for (int i = 0; i < m; i++) {
int cnt = 0;
cin >> x;
for (int j = 0; j < n; j++) {
if (x == id[j]) {
if (lock == 0) {
lock = 1;
cout << "Opened by " << x << endl;
} else {
lock = 0;
cout << "Closed by " << x << endl;
}
continue;
} else
cnt++;
}
if (cnt == n) {
cout << "Unknown " << x << endl;
}
}
return 0;
}
|
#include <iostream>
using namespace std;
int main(void) {
int n;
cin >> n;
string id[257];
for (int i = 0; i < n; i++) {
cin >> id[i];
}
int lock = 0;
int m;
cin >> m;
string x;
for (int i = 0; i < m; i++) {
int cnt = 0;
cin >> x;
for (int j = 0; j < n; j++) {
if (x == id[j]) {
if (lock == 0) {
lock = 1;
cout << "Opened by " << x << endl;
} else {
lock = 0;
cout << "Closed by " << x << endl;
}
continue;
} else
cnt++;
}
if (cnt == n) {
cout << "Unknown " << x << endl;
}
}
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p01555
|
C++
|
Time Limit Exceeded
|
#include <stdio.h>
#include <string.h>
#define MAX_NUM 1000000000000000000ll
long long FizzBuzz(
long long
X) { // X - 1までのfizzとbuzzの数を数え、その文字数分(4)をかけている
return ((X - 1) / 3) * 4 + ((X - 1) / 5) * 4;
}
long long theOtherNum(
long long x) { // x - 1 までに出てくる通常数字(fizzbuzz以外)を数えている
return (x - 1) - ((x - 1) / 3 + (x - 1) / 5 - (x - 1) / 15);
}
long long theOther(
long long x, long long X,
long long
keta) { // xは1, 10, 100, 1000 ...とカウントアップしていき,
// そして最終的にXを超えるようなxが出たときXの値までの通常数字だけの文字数を返す
// ※ketaはxの桁数を表す
if (x * 10 - 1 < X) {
return (theOtherNum(x * 10) - theOtherNum(x)) * keta +
theOther(x * 10, X, keta + 1);
} else {
return (theOtherNum(X) - theOtherNum(x)) * keta;
}
}
long long headNumber(
long long
X) { // Xというあたいが与えられたときfizzbuzzを含む文字列を見てXの値(3か5かで割れる場合はfかb)が入るの先頭を返す関数
return 1 + FizzBuzz(X) + theOther(1, X, 1);
}
long long binarySearch(long long s) {
long long lb = 1, ub = MAX_NUM;
while (ub - lb > 1) {
long long mid = (ub + lb) / 2;
if (headNumber(mid) >= s) {
ub = mid;
} else {
lb = mid;
}
}
return lb;
}
int main(void) {
long long s, thrgh, hn, i, j;
char ans[4096];
scanf("%lld", &s);
thrgh = s - (headNumber(hn = binarySearch(s)));
char *p = ans;
for (i = hn; i < hn + 10; i++) {
if (i % 15 == 0) {
strcpy(p, "FizzBuzz");
p += 8;
} else if (i % 5 == 0) {
strcpy(p, "Buzz");
p += 4;
} else if (i % 3 == 0) {
strcpy(p, "Fizz");
p += 4;
} else {
long long keta = 0;
long long tmp = i;
do {
keta++;
tmp /= 10;
} while (tmp != 0);
tmp = i;
for (j = 0; j < keta; j++) {
*(p + keta - 1 - j) = '0' + tmp % 10;
tmp /= 10;
}
p += keta;
}
}
*p = '\0';
for (p = ans + thrgh; p != ans + thrgh + 20; p++) {
printf("%c", *p);
}
printf("\n");
while (1)
;
return 0;
}
|
#include <stdio.h>
#include <string.h>
#define MAX_NUM 1000000000000000000ll
long long FizzBuzz(
long long
X) { // X - 1までのfizzとbuzzの数を数え、その文字数分(4)をかけている
return ((X - 1) / 3) * 4 + ((X - 1) / 5) * 4;
}
long long theOtherNum(
long long x) { // x - 1 までに出てくる通常数字(fizzbuzz以外)を数えている
return (x - 1) - ((x - 1) / 3 + (x - 1) / 5 - (x - 1) / 15);
}
long long theOther(
long long x, long long X,
long long
keta) { // xは1, 10, 100, 1000 ...とカウントアップしていき,
// そして最終的にXを超えるようなxが出たときXの値までの通常数字だけの文字数を返す
// ※ketaはxの桁数を表す
if (x * 10 - 1 < X) {
return (theOtherNum(x * 10) - theOtherNum(x)) * keta +
theOther(x * 10, X, keta + 1);
} else {
return (theOtherNum(X) - theOtherNum(x)) * keta;
}
}
long long headNumber(
long long
X) { // Xというあたいが与えられたときfizzbuzzを含む文字列を見てXの値(3か5かで割れる場合はfかb)が入るの先頭を返す関数
return 1 + FizzBuzz(X) + theOther(1, X, 1);
}
long long binarySearch(long long s) {
long long lb = 1, ub = MAX_NUM;
while (ub - lb > 1) {
long long mid = (ub + lb) / 2;
if (headNumber(mid) >= s) {
ub = mid;
} else {
lb = mid;
}
}
return lb;
}
int main(void) {
long long s, thrgh, hn, i, j;
char ans[4096];
scanf("%lld", &s);
thrgh = s - (headNumber(hn = binarySearch(s)));
char *p = ans;
for (i = hn; i < hn + 10; i++) {
if (i % 15 == 0) {
strcpy(p, "FizzBuzz");
p += 8;
} else if (i % 5 == 0) {
strcpy(p, "Buzz");
p += 4;
} else if (i % 3 == 0) {
strcpy(p, "Fizz");
p += 4;
} else {
long long keta = 0;
long long tmp = i;
do {
keta++;
tmp /= 10;
} while (tmp != 0);
tmp = i;
for (j = 0; j < keta; j++) {
*(p + keta - 1 - j) = '0' + tmp % 10;
tmp /= 10;
}
p += keta;
}
}
*p = '\0';
for (p = ans + thrgh; p != ans + thrgh + 20; p++) {
printf("%c", *p);
}
printf("\n");
return 0;
}
|
delete
| 84 | 86 | 84 | 84 |
TLE
| |
p01555
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll length(ll n) {
if (n == 0) {
return 0;
}
ll k = 1, d = 1;
while (10 * k <= n) {
k *= 10;
d++;
}
k--;
ll n3 = n / 3 - k / 3;
ll n5 = n / 5 - k / 5;
ll n15 = n / 15 - k / 15;
return length(k) + 4 * (n3 + n5) + d * (n - k - n3 - n5 + n15);
}
int main() {
ll s;
cin >> s;
ll ok = 1, ng = 1e17, m;
while (ng - ok > 1) {
m = (ok + ng) / 2;
ll l = length(m - 1);
if (l <= s - 1) {
ok = m;
} else {
ng = m;
}
}
string r;
for (ll i = ok; r.size() < 100; i++) {
if (i % 15 == 0) {
r += "FizzBuzz";
} else if (i % 3 == 0) {
r += "Fizz";
} else if (i % 5 == 0) {
r += "Buzz";
} else {
r += to_string(i);
}
}
cerr << r << endl;
cout << r.substr(s - 1 - length(ok - 1), 20) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll length(ll n) {
if (n == 0) {
return 0;
}
ll k = 1, d = 1;
while (10 * k <= n) {
k *= 10;
d++;
}
k--;
ll n3 = n / 3 - k / 3;
ll n5 = n / 5 - k / 5;
ll n15 = n / 15 - k / 15;
return length(k) + 4 * (n3 + n5) + d * (n - k - n3 - n5 + n15);
}
int main() {
ll s;
cin >> s;
ll ok = 1, ng = 1e17, m;
while (ng - ok > 1) {
m = (ok + ng) / 2;
ll l = length(m - 1);
if (l <= s - 1) {
ok = m;
} else {
ng = m;
}
}
string r;
for (ll i = ok; r.size() < 100; i++) {
if (i % 15 == 0) {
r += "FizzBuzz";
} else if (i % 3 == 0) {
r += "Fizz";
} else if (i % 5 == 0) {
r += "Buzz";
} else {
r += to_string(i);
}
}
cout << r.substr(s - 1 - length(ok - 1), 20) << endl;
}
|
delete
| 47 | 48 | 47 | 47 |
0
|
12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz3132Fizz34
|
p01555
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sum[20];
ll len(ll x) {
if (x == 0)
return 0;
ll ret = 0;
int keta = 0;
ll t = x;
ll ex = 1;
while (t) {
keta++;
t /= 10;
ex *= 10;
}
ex /= 10;
ret += sum[keta - 1];
// cout<<"keta "<<keta<<endl;
ll fiz = x / 3 - (ex - 1) / 3;
ll buz = x / 5 - (ex - 1) / 5;
ll fizbuz = x / 15 - (ex - 1) / 15;
ll mod = x - ex + 1 - fiz - buz + fizbuz;
// cout<<"len "<<x<<" "<<fiz<<" "<<buz<<" "<<mod<<endl;
ret += fiz * 4 + buz * 4 + mod * keta;
return ret;
}
char idx(ll l, int x) {
if (l % 15 == 0) {
return "FizzBuzz"[x];
}
if (l % 3 == 0) {
return "Fizz"[x];
}
if (l % 5 == 0) {
return "Buzz"[x];
}
stringstream ss;
ss << l;
string t;
ss >> t;
return t[x];
}
char func(ll x) {
ll l = 0;
ll r = 1e12;
while (r - l > 1) {
ll mid = (r + l) / 2;
// cout<<l<<" "<<r<<" "<<mid<<" "<<len(mid)<<endl;
if (len(mid) < x) {
l = mid;
} else {
r = mid;
}
}
x -= len(l);
// cout<<"func "<<r<<" "<<x-1<<endl;
return idx(r, x - 1);
}
int main() {
ll tmp = 10;
sum[1] = 21;
for (int i = 2; i < 20; i++) {
tmp *= 10;
sum[i] += sum[i - 1];
sum[i] += tmp / 10 * 9 * i;
sum[i] += ((tmp - 1) / 3 - (tmp / 10 - 1) / 3) * (4 - i);
sum[i] += ((tmp - 1) / 5 - (tmp / 10 - 1) / 5) * (4 - i);
sum[i] += ((tmp - 1) / 15 - (tmp / 10 - 1) / 15) * (i);
// cout<<sum[i]<<endl;
}
ll s;
cin >> s;
for (int i = 0; i < 20; i++) {
// cout<<"i "<<i<<endl;
cout << func(s + i);
// cout<<s+i<<" "<<func(s+i)<<endl;
}
// for(int i=1; i<10000; i++){
// ll l = len(i) - len(i-1);
// ll a;
// if(i%15==0) a = 8;
// else if(i%3==0) a = 4;
// else if(i%5==0) a = 4;
// else{
// stringstream ss;
// ss<<i;
// string t;
// ss>>t;
// a = t.size();
// }
// if(a != l){
// cout<<i<<" "<<l<<" "<<a<<endl;
// cout<<len(i-1)<<" "<<len(i)<<endl;
// return 0;
// }
// }
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sum[20];
ll len(ll x) {
if (x == 0)
return 0;
ll ret = 0;
int keta = 0;
ll t = x;
ll ex = 1;
while (t) {
keta++;
t /= 10;
ex *= 10;
}
ex /= 10;
ret += sum[keta - 1];
// cout<<"keta "<<keta<<endl;
ll fiz = x / 3 - (ex - 1) / 3;
ll buz = x / 5 - (ex - 1) / 5;
ll fizbuz = x / 15 - (ex - 1) / 15;
ll mod = x - ex + 1 - fiz - buz + fizbuz;
// cout<<"len "<<x<<" "<<fiz<<" "<<buz<<" "<<mod<<endl;
ret += fiz * 4 + buz * 4 + mod * keta;
return ret;
}
char idx(ll l, int x) {
if (l % 15 == 0) {
return "FizzBuzz"[x];
}
if (l % 3 == 0) {
return "Fizz"[x];
}
if (l % 5 == 0) {
return "Buzz"[x];
}
stringstream ss;
ss << l;
string t;
ss >> t;
return t[x];
}
char func(ll x) {
ll l = 0;
ll r = 1e17;
while (r - l > 1) {
ll mid = (r + l) / 2;
// cout<<l<<" "<<r<<" "<<mid<<" "<<len(mid)<<endl;
if (len(mid) < x) {
l = mid;
} else {
r = mid;
}
}
x -= len(l);
// cout<<"func "<<r<<" "<<x-1<<endl;
return idx(r, x - 1);
}
int main() {
ll tmp = 10;
sum[1] = 21;
for (int i = 2; i < 20; i++) {
tmp *= 10;
sum[i] += sum[i - 1];
sum[i] += tmp / 10 * 9 * i;
sum[i] += ((tmp - 1) / 3 - (tmp / 10 - 1) / 3) * (4 - i);
sum[i] += ((tmp - 1) / 5 - (tmp / 10 - 1) / 5) * (4 - i);
sum[i] += ((tmp - 1) / 15 - (tmp / 10 - 1) / 15) * (i);
// cout<<sum[i]<<endl;
}
ll s;
cin >> s;
for (int i = 0; i < 20; i++) {
// cout<<"i "<<i<<endl;
cout << func(s + i);
// cout<<s+i<<" "<<func(s+i)<<endl;
}
// for(int i=1; i<10000; i++){
// ll l = len(i) - len(i-1);
// ll a;
// if(i%15==0) a = 8;
// else if(i%3==0) a = 4;
// else if(i%5==0) a = 4;
// else{
// stringstream ss;
// ss<<i;
// string t;
// ss>>t;
// a = t.size();
// }
// if(a != l){
// cout<<i<<" "<<l<<" "<<a<<endl;
// cout<<len(i-1)<<" "<<len(i)<<endl;
// return 0;
// }
// }
cout << endl;
}
|
replace
| 50 | 51 | 50 | 51 |
0
| |
p01555
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long getLen(long long n) {
long long ret = 0;
long long a = 1;
long long b = 9;
long long len = 1;
while (a <= n) {
b = min(b, n);
long long fizzbuzz = b / 15 - (a - 1) / 15;
long long fizz = b / 3 - (a - 1) / 3 - fizzbuzz;
long long buzz = b / 5 - (a - 1) / 5 - fizzbuzz;
ret += fizzbuzz * 8 + fizz * 4 + buzz * 4;
ret += (b - a + 1 - fizzbuzz - fizz - buzz) * len;
a *= 10;
b = (b * 10) + 9;
++len;
}
return ret;
}
int main() {
long long n;
cin >> n;
long long a = 0;
long long b = 10000000000000000LL;
while (a < b) {
long long c = (a + b + 1) / 2;
if (getLen(c) < n)
a = c;
else
b = c - 1;
}
ostringstream oss;
while (oss.str().size() < 100) {
++a;
if (a % 3 == 0 || a % 5 == 0) {
if (a % 3 == 0)
oss << "Fizz";
if (a % 5 == 0)
oss << "Buzz";
} else {
oss << a;
}
}
string s = oss.str();
cout << s.substr(n - getLen(b) - 1, 20) << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long getLen(long long n) {
long long ret = 0;
long long a = 1;
long long b = 9;
long long len = 1;
while (a <= n) {
b = min(b, n);
long long fizzbuzz = b / 15 - (a - 1) / 15;
long long fizz = b / 3 - (a - 1) / 3 - fizzbuzz;
long long buzz = b / 5 - (a - 1) / 5 - fizzbuzz;
ret += fizzbuzz * 8 + fizz * 4 + buzz * 4;
ret += (b - a + 1 - fizzbuzz - fizz - buzz) * len;
a *= 10;
b = (b * 10) + 9;
++len;
}
return ret;
}
int main() {
long long n;
cin >> n;
long long a = 0;
long long b = 100000000000000000LL;
while (a < b) {
long long c = (a + b + 1) / 2;
if (getLen(c) < n)
a = c;
else
b = c - 1;
}
ostringstream oss;
while (oss.str().size() < 100) {
++a;
if (a % 3 == 0 || a % 5 == 0) {
if (a % 3 == 0)
oss << "Fizz";
if (a % 5 == 0)
oss << "Buzz";
} else {
oss << a;
}
}
string s = oss.str();
cout << s.substr(n - getLen(b) - 1, 20) << endl;
return 0;
}
|
replace
| 47 | 48 | 47 | 48 |
0
| |
p01555
|
C++
|
Runtime Error
|
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
// [l, r)
ll countNum(ll l, ll r, ll div) { return (r - 1) / div - (l - 1) / div; }
// x ?????§????????¨????????????????????¨????????????????±???????
ll calc(ll x) {
ll p = 1;
ll ans = 0;
int cnt = 1;
while (10 * p < x) {
ll np = p * 10;
// [p, np) ??§?????????????????????
ll fizzbuzz = countNum(p, np, 15);
ll fizz = countNum(p, np, 3) - fizzbuzz;
ll buzz = countNum(p, np, 5) - fizzbuzz;
ll other = (np - p) - (fizz + buzz + fizzbuzz);
ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
cnt++;
p = np;
}
// [p, x] ????????????????????°
ll fizzbuzz = countNum(p, x + 1, 15);
ll fizz = countNum(p, x + 1, 3) - fizzbuzz;
ll buzz = countNum(p, x + 1, 5) - fizzbuzz;
ll other = (x + 1 - p) - (fizz + buzz + fizzbuzz);
ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
return ans;
}
const string number = "0123456789";
string trans(ll x) {
if (x % 15 == 0)
return "FizzBuzz";
if (x % 5 == 0)
return "Buzz";
if (x % 3 == 0)
return "Fizz";
string ret;
while (x) {
ret += number[(int)(x % 10)];
x /= 10;
}
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
ll s;
cin >> s;
ll low = 0, high = 1ll << 55;
// calc(x) < s ?????????????????§??? x ????±???????
while (high - low > 1) {
const ll med = (low + high) / 2;
if (calc(med) >= s)
high = med;
else
low = med;
}
s -= calc(low) + 1;
// ?????£??????????????´????????£??????
string ans;
while (1) {
string t = trans(++low);
if (s > 0) {
ans += t.substr((int)s);
s = 0;
} else {
int i = 0;
while (ans.size() < 20 && i < (int)t.size())
ans += t[i++];
}
if (ans.size() == 20)
break;
}
cout << ans << endl;
return 0;
}
|
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
// [l, r)
ll countNum(ll l, ll r, ll div) { return (r - 1) / div - (l - 1) / div; }
// x ?????§????????¨????????????????????¨????????????????±???????
ll calc(ll x) {
ll p = 1;
ll ans = 0;
int cnt = 1;
while (10 * p < x) {
ll np = p * 10;
// [p, np) ??§?????????????????????
ll fizzbuzz = countNum(p, np, 15);
ll fizz = countNum(p, np, 3) - fizzbuzz;
ll buzz = countNum(p, np, 5) - fizzbuzz;
ll other = (np - p) - (fizz + buzz + fizzbuzz);
ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
cnt++;
p = np;
}
// [p, x] ????????????????????°
ll fizzbuzz = countNum(p, x + 1, 15);
ll fizz = countNum(p, x + 1, 3) - fizzbuzz;
ll buzz = countNum(p, x + 1, 5) - fizzbuzz;
ll other = (x + 1 - p) - (fizz + buzz + fizzbuzz);
ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
return ans;
}
const string number = "0123456789";
string trans(ll x) {
if (x % 15 == 0)
return "FizzBuzz";
if (x % 5 == 0)
return "Buzz";
if (x % 3 == 0)
return "Fizz";
string ret;
while (x) {
ret += number[(int)(x % 10)];
x /= 10;
}
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
ll s;
cin >> s;
ll low = 0, high = 1ll << 58;
// calc(x) < s ?????????????????§??? x ????±???????
while (high - low > 1) {
const ll med = (low + high) / 2;
if (calc(med) >= s)
high = med;
else
low = med;
}
s -= calc(low) + 1;
// ?????£??????????????´????????£??????
string ans;
while (1) {
string t = trans(++low);
if (s > 0) {
ans += t.substr((int)s);
s = 0;
} else {
int i = 0;
while (ans.size() < 20 && i < (int)t.size())
ans += t[i++];
}
if (ans.size() == 20)
break;
}
cout << ans << endl;
return 0;
}
|
replace
| 82 | 83 | 82 | 83 |
0
| |
p01555
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL fz_add(LL x) { return (x / 3LL) + (x / 5LL); }
LL fz_cnt(LL x) { return (x / 3LL) + (x / 5LL) - (x / 15LL); }
LL count_char(LL x) {
LL count = 0;
for (LL z = 1, inc = 1; z <= x; ++inc) {
z *= 10;
if (z <= x) {
count += inc * (z - (z / 10)) + 4 * (fz_add(z - 1) - fz_add(z / 10 - 1)) -
inc * (fz_cnt(z - 1) - fz_cnt(z / 10 - 1));
} else {
count += inc * (x - (z / 10 - 1)) + 4 * (fz_add(x) - fz_add(z / 10 - 1)) -
inc * (fz_cnt(x) - fz_cnt(z / 10 - 1));
}
}
return count;
}
int main() {
LL N;
cin >> N;
LL l, r, count;
l = 0LL;
r = 1000000000000000001LL;
while (l + 1 < r) {
LL m = (l + r) / 2;
LL count = count_char(m);
if (count < N) {
l = m;
} else {
r = m;
}
}
count = count_char(l);
stringstream ss;
for (int x = l + 1; x <= l + 20; ++x) {
if (x % 15 == 0) {
ss << "FizzBuzz";
} else if (x % 3 == 0) {
ss << "Fizz";
} else if (x % 5 == 0) {
ss << "Buzz";
} else {
ss << x;
}
}
string ret;
ss >> ret;
cout << ret.substr(N - count - 1, 20) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL fz_add(LL x) { return (x / 3LL) + (x / 5LL); }
LL fz_cnt(LL x) { return (x / 3LL) + (x / 5LL) - (x / 15LL); }
LL count_char(LL x) {
LL count = 0;
for (LL z = 1, inc = 1; z <= x; ++inc) {
z *= 10;
if (z <= x) {
count += inc * (z - (z / 10)) + 4 * (fz_add(z - 1) - fz_add(z / 10 - 1)) -
inc * (fz_cnt(z - 1) - fz_cnt(z / 10 - 1));
} else {
count += inc * (x - (z / 10 - 1)) + 4 * (fz_add(x) - fz_add(z / 10 - 1)) -
inc * (fz_cnt(x) - fz_cnt(z / 10 - 1));
}
}
return count;
}
int main() {
LL N;
cin >> N;
LL l, r, count;
l = 0LL;
r = 1000000000000000001LL;
while (l + 1 < r) {
LL m = (l + r) / 2;
LL count = count_char(m);
if (count < N) {
l = m;
} else {
r = m;
}
}
count = count_char(l);
stringstream ss;
for (LL x = l + 1; x <= l + 20; ++x) {
if (x % 15 == 0) {
ss << "FizzBuzz";
} else if (x % 3 == 0) {
ss << "Fizz";
} else if (x % 5 == 0) {
ss << "Buzz";
} else {
ss << x;
}
}
string ret;
ss >> ret;
cout << ret.substr(N - count - 1, 20) << endl;
return 0;
}
|
replace
| 47 | 48 | 47 | 48 |
MLE
| |
p01555
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
for (int WWW = 0; WWW < (n); WWW++) \
cerr << v[WWW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, i, j) \
{ \
for (int aaa = 0; aaa < i; aaa++) { \
for (int bbb = 0; bbb < j; bbb++) \
cerr << v[aaa][bbb] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
ll num;
bool f(ll mid) {
ll cou = 0;
ll san = 0;
ll go = 0;
ll juugo = 0;
ll keta = 9;
ll prev = 0;
ll kazu = 1;
ll tmpsan;
ll tmpgo;
ll tmpjuugo;
while (mid > keta) {
tmpsan = keta / 3 - san;
tmpgo = keta / 5 - go;
tmpjuugo = keta / 15 - juugo;
cou += (tmpsan + tmpgo) * 4;
cou += (keta - prev - tmpsan - tmpgo + tmpjuugo) * kazu;
san += tmpsan;
go += tmpgo;
juugo += tmpjuugo;
kazu++;
prev = keta;
keta *= 10;
keta += 9;
}
tmpsan = mid / 3 - san;
tmpgo = mid / 5 - go;
tmpjuugo = mid / 15 - juugo;
cou += (tmpsan + tmpgo) * 4;
cou += (mid - prev - tmpsan - tmpgo + tmpjuugo) * kazu;
num = cou;
return n > cou;
}
void m_prinf(ll kazu) {
string str = "";
for (int i = kazu; i < kazu + 20; i++) {
if (i % 3 == 0) {
str += "Fizz";
}
if (i % 5 == 0) {
str += "Buzz";
}
if (i % 3 != 0 && i % 5 != 0) {
string tmpstr = "";
ll tmp = i;
while (tmp) {
tmpstr += (char)((tmp % 10) + '0');
tmp /= 10;
}
for (int j = tmpstr.size() - 1; j >= 0; j--) {
str += tmpstr[j];
}
}
}
REP(i, str.size()) {
num++;
if (num >= n && num < n + 20) {
cout << str[i];
}
}
cout << endl;
}
int main() {
cin >> n;
ll tb = LLINF;
ll ub = 0;
while (abs(tb - ub) > 1) {
ll mid = (tb + ub) / 2;
if (f(mid))
ub = mid;
else
tb = mid;
}
f(ub);
m_prinf(ub + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
for (int WWW = 0; WWW < (n); WWW++) \
cerr << v[WWW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, i, j) \
{ \
for (int aaa = 0; aaa < i; aaa++) { \
for (int bbb = 0; bbb < j; bbb++) \
cerr << v[aaa][bbb] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
ll num;
bool f(ll mid) {
ll cou = 0;
ll san = 0;
ll go = 0;
ll juugo = 0;
ll keta = 9;
ll prev = 0;
ll kazu = 1;
ll tmpsan;
ll tmpgo;
ll tmpjuugo;
while (mid > keta) {
tmpsan = keta / 3 - san;
tmpgo = keta / 5 - go;
tmpjuugo = keta / 15 - juugo;
cou += (tmpsan + tmpgo) * 4;
cou += (keta - prev - tmpsan - tmpgo + tmpjuugo) * kazu;
san += tmpsan;
go += tmpgo;
juugo += tmpjuugo;
kazu++;
prev = keta;
keta *= 10;
keta += 9;
}
tmpsan = mid / 3 - san;
tmpgo = mid / 5 - go;
tmpjuugo = mid / 15 - juugo;
cou += (tmpsan + tmpgo) * 4;
cou += (mid - prev - tmpsan - tmpgo + tmpjuugo) * kazu;
num = cou;
return n > cou;
}
void m_prinf(ll kazu) {
string str = "";
for (ll i = kazu; i < kazu + 20; i++) {
if (i % 3 == 0) {
str += "Fizz";
}
if (i % 5 == 0) {
str += "Buzz";
}
if (i % 3 != 0 && i % 5 != 0) {
string tmpstr = "";
ll tmp = i;
while (tmp) {
tmpstr += (char)((tmp % 10) + '0');
tmp /= 10;
}
for (int j = tmpstr.size() - 1; j >= 0; j--) {
str += tmpstr[j];
}
}
}
REP(i, str.size()) {
num++;
if (num >= n && num < n + 20) {
cout << str[i];
}
}
cout << endl;
}
int main() {
cin >> n;
ll tb = LLINF;
ll ub = 0;
while (abs(tb - ub) > 1) {
ll mid = (tb + ub) / 2;
if (f(mid))
ub = mid;
else
tb = mid;
}
f(ub);
m_prinf(ub + 1);
return 0;
}
|
replace
| 83 | 84 | 83 | 84 |
MLE
| |
p01555
|
C++
|
Runtime Error
|
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
#define int ll
int range(int l, int r, int len) {
int fizz = r / 3 - l / 3;
int buzz = r / 5 - l / 5;
int fizzbuzz = r / 15 - l / 15;
int res = 0;
res += (r - l - fizz - buzz + fizzbuzz) * len;
res += (fizz + buzz - 2 * fizzbuzz) * 4;
res += fizzbuzz * 8;
return res;
}
int len(int n) {
int res = 0;
int cnt = 1;
int p = 1;
while (p * 10 <= n) {
res += range(p - 1, p * 10 - 1, cnt);
p *= 10;
cnt += 1;
}
res += range(p - 1, n - 1, cnt);
return res;
}
string solve(int n) {
int l = 0, r = 1LL << 55;
while (l + 1 < r) {
int m = (r + l) / 2;
if (len(m) < n)
l = m;
else
r = m;
}
int rest = n - len(l);
string s;
for (int i = l; (int)s.size() < rest + 30; i++) {
if (i % 15 == 0)
s += "FizzBuzz";
else if (i % 3 == 0)
s += "Fizz";
else if (i % 5 == 0)
s += "Buzz";
else
s += to_string(i);
}
return s.substr(rest - 1, 20);
}
signed main() {
int n;
cin >> n;
cout << solve(n) << endl;
}
|
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
#define int ll
int range(int l, int r, int len) {
int fizz = r / 3 - l / 3;
int buzz = r / 5 - l / 5;
int fizzbuzz = r / 15 - l / 15;
int res = 0;
res += (r - l - fizz - buzz + fizzbuzz) * len;
res += (fizz + buzz - 2 * fizzbuzz) * 4;
res += fizzbuzz * 8;
return res;
}
int len(int n) {
int res = 0;
int cnt = 1;
int p = 1;
while (p * 10 <= n) {
res += range(p - 1, p * 10 - 1, cnt);
p *= 10;
cnt += 1;
}
res += range(p - 1, n - 1, cnt);
return res;
}
string solve(int n) {
int l = 0, r = 1000000000000000000LL;
while (l + 1 < r) {
int m = (r + l) / 2;
if (len(m) < n)
l = m;
else
r = m;
}
int rest = n - len(l);
string s;
for (int i = l; (int)s.size() < rest + 30; i++) {
if (i % 15 == 0)
s += "FizzBuzz";
else if (i % 3 == 0)
s += "Fizz";
else if (i % 5 == 0)
s += "Buzz";
else
s += to_string(i);
}
return s.substr(rest - 1, 20);
}
signed main() {
int n;
cin >> n;
cout << solve(n) << endl;
}
|
replace
| 72 | 73 | 72 | 73 |
0
| |
p01556
|
C++
|
Runtime Error
|
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
constexpr long double EPS = 1e-7;
struct point {
long double x, y;
point(long double x = 0.0, long double y = 0.0) : x(x), y(y) {}
point(const point &p) : x(p.x), y(p.y) {}
point operator+(const point &p) const { return point(x + p.x, y + p.y); }
point operator-(const point &p) const { return point(x - p.x, y - p.y); }
point operator*(long double s) const { return point(x * s, y * s); }
};
long double dot(const point &a, const point &b) {
return a.x * b.x + a.y * b.y;
}
long double cross(const point &a, const point &b) {
return a.x * b.y - a.y * b.x;
}
long double norm(const point &p) { return p.x * p.x + p.y * p.y; }
int ccw(const point &a, point b, point c) {
b = b - a;
c = c - a;
const long double t = cross(b, c);
if (t > EPS)
return 1;
if (t < -EPS)
return -1;
if (dot(b, c) < 0)
return 2;
if (norm(b) < norm(c))
return -2;
return 0;
}
point rotate(const point &p, long double theta) {
const long double s = sin(theta), c = cos(theta);
return point(c * p.x - s * p.y, s * p.x + c * p.y);
}
struct line {
point a, b;
line(const point &a_, const point &b_) : a(a_), b(b_) {}
};
point crosspoint(const line &a, const line &b) {
const long double t = cross(a.b - a.a, b.b - b.a);
if (abs(t) < EPS)
return a.a;
return b.a + (b.b - b.a) * cross(a.b - a.a, a.a - b.a) * (1 / t);
}
typedef vector<point> polygon;
long double area(const polygon &p) {
const int n = p.size();
if (n < 3)
return 0.0;
long double res = cross(p[n - 1], p[0]);
for (int i = 1; i < n; ++i) {
res += cross(p[i - 1], p[i]);
}
return abs(res) * 0.5;
}
polygon convex_cut(const polygon &p, const line &l) {
const int n = p.size();
polygon res;
for (int i = 0; i < n; ++i) {
const int next = (i + 1) % n;
const int tmp = ccw(l.a, l.b, p[i]);
if (tmp != -1)
res.emplace_back(p[i]);
if (tmp * ccw(l.a, l.b, p[next]) < 0) {
res.emplace_back(crosspoint(l, line(p[i], p[next])));
}
}
return res;
}
bool ok(const point &g, const polygon &p) {
const long double S_half = area(p) / 2.0;
for (long double theta = 0; theta < M_PI; theta += 0.01) {
const long double S =
area(convex_cut(p, line(g, g + rotate(point(10, 0), theta))));
if (abs((S_half - S) / S_half) > EPS)
return false;
}
return true;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.precision(5);
cout.flags(ios::fixed);
int n;
cin >> n;
point g(0, 0);
polygon p;
p.reserve(n);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
p.emplace_back(x, y);
g = g + p[i];
}
g = g * (1.0 / n);
cerr << g.x << " " << g.y << endl;
if (ok(g, p)) {
cout << g.x << " " << g.y << endl;
} else {
cout << "NA" << endl;
}
return EXIT_SUCCESS;
}
|
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
constexpr long double EPS = 1e-7;
struct point {
long double x, y;
point(long double x = 0.0, long double y = 0.0) : x(x), y(y) {}
point(const point &p) : x(p.x), y(p.y) {}
point operator+(const point &p) const { return point(x + p.x, y + p.y); }
point operator-(const point &p) const { return point(x - p.x, y - p.y); }
point operator*(long double s) const { return point(x * s, y * s); }
};
long double dot(const point &a, const point &b) {
return a.x * b.x + a.y * b.y;
}
long double cross(const point &a, const point &b) {
return a.x * b.y - a.y * b.x;
}
long double norm(const point &p) { return p.x * p.x + p.y * p.y; }
int ccw(const point &a, point b, point c) {
b = b - a;
c = c - a;
const long double t = cross(b, c);
if (t > EPS)
return 1;
if (t < -EPS)
return -1;
if (dot(b, c) < 0)
return 2;
if (norm(b) < norm(c))
return -2;
return 0;
}
point rotate(const point &p, long double theta) {
const long double s = sin(theta), c = cos(theta);
return point(c * p.x - s * p.y, s * p.x + c * p.y);
}
struct line {
point a, b;
line(const point &a_, const point &b_) : a(a_), b(b_) {}
};
point crosspoint(const line &a, const line &b) {
const long double t = cross(a.b - a.a, b.b - b.a);
if (abs(t) < EPS)
return a.a;
return b.a + (b.b - b.a) * cross(a.b - a.a, a.a - b.a) * (1 / t);
}
typedef vector<point> polygon;
long double area(const polygon &p) {
const int n = p.size();
if (n < 3)
return 0.0;
long double res = cross(p[n - 1], p[0]);
for (int i = 1; i < n; ++i) {
res += cross(p[i - 1], p[i]);
}
return abs(res) * 0.5;
}
polygon convex_cut(const polygon &p, const line &l) {
const int n = p.size();
polygon res;
for (int i = 0; i < n; ++i) {
const int next = (i + 1) % n;
const int tmp = ccw(l.a, l.b, p[i]);
if (tmp != -1)
res.emplace_back(p[i]);
if (tmp * ccw(l.a, l.b, p[next]) < 0) {
res.emplace_back(crosspoint(l, line(p[i], p[next])));
}
}
return res;
}
bool ok(const point &g, const polygon &p) {
const long double S_half = area(p) / 2.0;
for (long double theta = 0; theta < M_PI; theta += 0.01) {
const long double S =
area(convex_cut(p, line(g, g + rotate(point(10, 0), theta))));
if (abs((S_half - S) / S_half) > EPS)
return false;
}
return true;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.precision(5);
cout.flags(ios::fixed);
int n;
cin >> n;
point g(0, 0);
polygon p;
p.reserve(n);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
p.emplace_back(x, y);
g = g + p[i];
}
g = g * (1.0 / n);
if (ok(g, p)) {
cout << g.x << " " << g.y << endl;
} else {
cout << "NA" << endl;
}
return EXIT_SUCCESS;
}
|
delete
| 125 | 126 | 125 | 125 |
0
|
50 50
|
p01557
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int n;
map<vi, short> bfs(vi v) {
map<vi, short> mp;
queue<vi> que;
que.push(v);
mp[v] = 0;
while (que.size()) {
vi tmp = que.front();
que.pop();
int t = mp[tmp];
if (t >= n / 2)
continue;
REP(i, n) REP(j, i) {
vi nx = tmp;
reverse(nx.begin() + j, nx.begin() + i + 1);
if (mp.find(nx) != mp.end())
continue;
else {
mp[nx] = t + 1;
que.push(nx);
}
}
}
return mp;
}
signed main() {
cin >> n;
vi a(n), b;
REP(i, n) cin >> a[i];
b = a;
sort(ALL(b));
auto mp1 = bfs(a);
auto mp2 = bfs(b);
int ans = n - 1;
for (auto it = mp1.begin(); it != mp1.end(); it++) {
if (mp2.find(it->first) != mp2.end())
chmin(ans, it->second + mp2[it->first]);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int n;
map<vi, short> bfs(vi v) {
map<vi, short> mp;
queue<vi> que;
que.push(v);
mp[v] = 0;
while (que.size()) {
vi tmp = que.front();
que.pop();
int t = mp[tmp];
if (t >= (n - 1) / 2)
continue;
REP(i, n) REP(j, i) {
vi nx = tmp;
reverse(nx.begin() + j, nx.begin() + i + 1);
if (mp.find(nx) != mp.end())
continue;
else {
mp[nx] = t + 1;
que.push(nx);
}
}
}
return mp;
}
signed main() {
cin >> n;
vi a(n), b;
REP(i, n) cin >> a[i];
b = a;
sort(ALL(b));
auto mp1 = bfs(a);
auto mp2 = bfs(b);
int ans = n - 1;
for (auto it = mp1.begin(); it != mp1.end(); it++) {
if (mp2.find(it->first) != mp2.end())
chmin(ans, it->second + mp2[it->first]);
}
cout << ans << endl;
return 0;
}
|
replace
| 31 | 32 | 31 | 32 |
MLE
| |
p01557
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
short n;
vector<short> a;
queue<pair<vector<short>, short>> Q;
vector<pair<vector<short>, short>> a1, a2;
set<vector<short>> E;
void init(vector<short> R, short Lim, short D) {
Q.push(make_pair(R, 0));
E.clear();
E.insert(R);
while (!Q.empty()) {
vector<short> H = Q.front().first;
short L = Q.front().second;
Q.pop();
if (D == 0)
a1.push_back(make_pair(H, L));
if (D == 1)
a2.push_back(make_pair(H, L));
if (L == Lim)
continue;
for (short i = 0; i < n; i++) {
for (short j = i + 1; j < n; j++) {
vector<short> I = H;
short Y = j;
for (short k = i; k <= (i + j) / 2; k++) {
swap(I[k], I[Y]);
Y--;
}
if (E.find(I) == E.end()) {
Q.push(make_pair(I, L + 1));
E.insert(I);
}
}
}
}
}
int main() {
cin >> n;
for (short i = 0; i < n; i++) {
short p;
cin >> p;
a.push_back(p);
}
short pos = n / 2;
short pos2 = (n - 1) / 2;
init(a, pos, 0);
vector<short> Zero;
for (int i = 0; i < n; i++)
Zero.push_back(i + 1);
init(Zero, pos2, 1);
sort(a2.begin(), a2.end());
short ret = n - 1;
for (short i = 0; i < a1.size(); i++) {
int S1 =
lower_bound(a2.begin(), a2.end(), make_pair(a1[i].first, (short)0)) -
a2.begin();
int S2 = upper_bound(a2.begin(), a2.end(), make_pair(a1[i].first, pos)) -
a2.begin();
if (S1 != S2)
ret = min(ret, (short)(a1[i].second + a2[S1].second));
}
cout << ret << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
short n;
vector<short> a;
queue<pair<vector<short>, short>> Q;
vector<pair<vector<short>, short>> a1, a2;
set<vector<short>> E;
void init(vector<short> R, short Lim, short D) {
Q.push(make_pair(R, 0));
E.clear();
E.insert(R);
while (!Q.empty()) {
vector<short> H = Q.front().first;
short L = Q.front().second;
Q.pop();
if (D == 0)
a1.push_back(make_pair(H, L));
if (D == 1)
a2.push_back(make_pair(H, L));
if (L == Lim)
continue;
for (short i = 0; i < n; i++) {
for (short j = i + 1; j < n; j++) {
vector<short> I = H;
short Y = j;
for (short k = i; k <= (i + j) / 2; k++) {
swap(I[k], I[Y]);
Y--;
}
if (E.find(I) == E.end()) {
Q.push(make_pair(I, L + 1));
E.insert(I);
}
}
}
}
}
int main() {
cin >> n;
for (short i = 0; i < n; i++) {
short p;
cin >> p;
a.push_back(p);
}
short pos = min(4, n / 2);
short pos2 = (n - 1) / 2;
init(a, pos, 0);
vector<short> Zero;
for (int i = 0; i < n; i++)
Zero.push_back(i + 1);
init(Zero, pos2, 1);
sort(a2.begin(), a2.end());
short ret = n - 1;
for (short i = 0; i < a1.size(); i++) {
int S1 =
lower_bound(a2.begin(), a2.end(), make_pair(a1[i].first, (short)0)) -
a2.begin();
int S2 = upper_bound(a2.begin(), a2.end(), make_pair(a1[i].first, pos)) -
a2.begin();
if (S1 != S2)
ret = min(ret, (short)(a1[i].second + a2[S1].second));
}
cout << ret << endl;
return 0;
}
|
replace
| 48 | 49 | 48 | 49 |
TLE
| |
p01557
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
int n;
ll perm[11];
int dist[4000000];
bool check[4000000];
map<ll, int> ma;
void permutation2() {
for (int i = 0; i < n; i++) {
perm[i] = i;
}
int j = 0;
do {
ll d = 0;
ll p = 1;
for (int i = 0; i < n; i++) {
d += perm[i] * p;
p *= 10LL;
}
ma[d] = j;
j++;
} while (next_permutation(perm, perm + n));
}
int a[11];
ll tmp[11];
ll ten[11];
int bfs2(ll s) {
queue<ll> que;
que.push(s);
dist[ma[s]] = 0;
int res = n - 1;
while (que.size()) {
ll v = que.front();
que.pop();
int lv = ma[v];
if (dist[lv] == 5)
continue;
ll nv = v;
for (int i = 0; i < n; i++) {
tmp[i] = nv % 10LL;
nv /= 10LL;
}
ll dv = v;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll uv = dv;
for (int k = 0; i + k < j - k; k++) {
uv += tmp[i + k] * (ten[j - k] - ten[i + k]) +
tmp[j - k] * (ten[i + k] - ten[j - k]);
}
int nexv = ma[uv];
if (dist[nexv] == -1) {
dist[nexv] = dist[lv] + 1;
que.push(uv);
} else if (check[nexv]) {
res = min(res, dist[lv] + dist[nexv] + 1);
}
}
}
}
return res;
}
int bfs(ll s) {
queue<ll> que;
que.push(s);
memset(dist, -1, sizeof(dist));
memset(check, false, sizeof(check));
dist[ma[s]] = 0;
check[ma[s]] = true;
while (que.size()) {
ll v = que.front();
que.pop();
int lv = ma[v];
if (lv == 0)
return dist[lv];
if (dist[lv] == 4)
continue;
ll nv = v;
for (int i = 0; i < n; i++) {
tmp[i] = nv % 10LL;
nv /= 10LL;
}
ll dv = v;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll uv = dv;
for (int k = 0; i + k < j - k; k++) {
uv += tmp[i + k] * (ten[j - k] - ten[i + k]) +
tmp[j - k] * (ten[i + k] - ten[j - k]);
}
int nexv = ma[uv];
if (dist[nexv] == -1) {
dist[nexv] = dist[lv] + 1;
check[nexv] = true;
que.push(uv);
}
}
}
}
return -1;
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
ll s = 0;
ll p = 1;
for (int i = 0; i < n; i++) {
s += (ll)p * a[i];
ten[i] = p;
p *= 10LL;
}
permutation2();
int re = bfs(s);
if (re != -1)
printf("%d\n", re);
else {
sort(a, a + n);
s = 0;
p = 1;
for (int i = 0; i < n; i++) {
s += (ll)p * a[i];
p *= 10LL;
}
printf("%d\n", bfs2(s));
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
int n;
ll perm[11];
int dist[4000000];
bool check[4000000];
map<ll, int> ma;
void permutation2() {
for (int i = 0; i < n; i++) {
perm[i] = i;
}
int j = 0;
do {
ll d = 0;
ll p = 1;
for (int i = 0; i < n; i++) {
d += perm[i] * p;
p *= 10LL;
}
ma[d] = j;
j++;
} while (next_permutation(perm, perm + n));
}
int a[11];
ll tmp[11];
ll ten[11];
int bfs2(ll s) {
queue<ll> que;
que.push(s);
dist[ma[s]] = 0;
int res = n - 1;
while (que.size()) {
ll v = que.front();
que.pop();
int lv = ma[v];
if (dist[lv] == 4)
continue;
ll nv = v;
for (int i = 0; i < n; i++) {
tmp[i] = nv % 10LL;
nv /= 10LL;
}
ll dv = v;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll uv = dv;
for (int k = 0; i + k < j - k; k++) {
uv += tmp[i + k] * (ten[j - k] - ten[i + k]) +
tmp[j - k] * (ten[i + k] - ten[j - k]);
}
int nexv = ma[uv];
if (dist[nexv] == -1) {
dist[nexv] = dist[lv] + 1;
que.push(uv);
} else if (check[nexv]) {
res = min(res, dist[lv] + dist[nexv] + 1);
}
}
}
}
return res;
}
int bfs(ll s) {
queue<ll> que;
que.push(s);
memset(dist, -1, sizeof(dist));
memset(check, false, sizeof(check));
dist[ma[s]] = 0;
check[ma[s]] = true;
while (que.size()) {
ll v = que.front();
que.pop();
int lv = ma[v];
if (lv == 0)
return dist[lv];
if (dist[lv] == 4)
continue;
ll nv = v;
for (int i = 0; i < n; i++) {
tmp[i] = nv % 10LL;
nv /= 10LL;
}
ll dv = v;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll uv = dv;
for (int k = 0; i + k < j - k; k++) {
uv += tmp[i + k] * (ten[j - k] - ten[i + k]) +
tmp[j - k] * (ten[i + k] - ten[j - k]);
}
int nexv = ma[uv];
if (dist[nexv] == -1) {
dist[nexv] = dist[lv] + 1;
check[nexv] = true;
que.push(uv);
}
}
}
}
return -1;
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
ll s = 0;
ll p = 1;
for (int i = 0; i < n; i++) {
s += (ll)p * a[i];
ten[i] = p;
p *= 10LL;
}
permutation2();
int re = bfs(s);
if (re != -1)
printf("%d\n", re);
else {
sort(a, a + n);
s = 0;
p = 1;
for (int i = 0; i < n; i++) {
s += (ll)p * a[i];
p *= 10LL;
}
printf("%d\n", bfs2(s));
}
return 0;
}
|
replace
| 44 | 45 | 44 | 45 |
TLE
| |
p01557
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-10;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }
template <class T> string IntToString(T num) {
string res;
stringstream ss;
ss << num;
return ss.str();
}
ll StringToInt(string &str) {
ll res = 0;
for (int i = 0; i < (int)str.size(); i++)
res = (res * 10 + str[i] - '0');
return res;
}
vector<char> v;
map<vector<char>, int> d1;
map<vector<char>, int> d2;
int N;
vector<char> sorted;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
v.push_back(a);
}
sorted = v;
sort(v.begin(), v.end());
queue<vector<char>> q1, q2;
d1[v] = 0;
d2[sorted] = 0;
q1.push(v);
q2.push(sorted);
int ans = 1 << 30;
while (q1.size() || q2.size()) {
{
vector<char> now = q1.front();
q1.pop();
int ccost = d1[now];
if (now == sorted) {
cout << ccost << endl;
return 0;
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
vector<char> nv = now;
reverse(nv.begin() + i, nv.begin() + j + 1);
if (d2.count(nv) != 0 && d1.count(nv) == 0)
ans = min(ans, ccost + 1 + d2[nv]);
else if (d1.count(nv) == 0) {
d1[nv] = ccost + 1;
q1.push(nv);
}
}
}
}
{
vector<char> now = q2.front();
q2.pop();
int ccost = d2[now];
if (v == now) {
cout << ccost << endl;
return 0;
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
vector<char> nv = now;
reverse(nv.begin() + i, nv.begin() + j + 1);
if (d1.count(nv) != 0 && d2.count(nv) == 0)
ans = min(ans, ccost + 1 + d1[nv]);
else if (d2.count(nv) == 0) {
d2[nv] = ccost + 1;
q2.push(nv);
}
}
}
}
if (q1.size() && q2.size()) {
int sum = d1[q1.front()] + d2[q2.front()];
if (sum >= ans)
break;
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-10;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }
template <class T> string IntToString(T num) {
string res;
stringstream ss;
ss << num;
return ss.str();
}
ll StringToInt(string &str) {
ll res = 0;
for (int i = 0; i < (int)str.size(); i++)
res = (res * 10 + str[i] - '0');
return res;
}
vector<char> v;
map<vector<char>, int> d1;
map<vector<char>, int> d2;
int N;
vector<char> sorted;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
v.push_back(a);
}
sorted = v;
sort(v.begin(), v.end());
queue<vector<char>> q1, q2;
d1[v] = 0;
d2[sorted] = 0;
q1.push(v);
q2.push(sorted);
int ans = 1 << 30;
while (q1.size() || q2.size()) {
{
vector<char> now = q1.front();
q1.pop();
int ccost = d1[now];
if (now == sorted) {
cout << ccost << endl;
return 0;
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
vector<char> nv = now;
reverse(nv.begin() + i, nv.begin() + j + 1);
if (d2.count(nv) != 0 && d1.count(nv) == 0)
ans = min(ans, ccost + 1 + d2[nv]);
else if (d1.count(nv) == 0) {
d1[nv] = ccost + 1;
q1.push(nv);
}
}
}
}
{
vector<char> now = q2.front();
q2.pop();
int ccost = d2[now];
if (v == now) {
cout << ccost << endl;
return 0;
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
vector<char> nv = now;
reverse(nv.begin() + i, nv.begin() + j + 1);
if (d1.count(nv) != 0 && d2.count(nv) == 0)
ans = min(ans, ccost + 1 + d1[nv]);
else if (d2.count(nv) == 0) {
d2[nv] = ccost + 1;
q2.push(nv);
}
}
}
}
if (q1.size() && q2.size()) {
int sum = d1[q1.front()] + d2[q2.front()] + 1;
if (sum >= ans)
break;
}
}
cout << ans << endl;
return 0;
}
|
replace
| 105 | 106 | 105 | 106 |
TLE
| |
p01557
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n;
vector<int> a;
struct state {
vector<int> v;
int dist1;
int dist2;
state(vector<int> v1, int d1, int d2) : v(v1), dist1(d1), dist2(d2) {}
};
bool operator<(const state &s1, const state &s2) {
return s1.dist1 * n + s1.dist2 * 3 < s2.dist1 * n + s2.dist2 * 3;
}
bool operator>(const state &s1, const state &s2) {
return s1.dist1 * n + s1.dist2 * 3 > s2.dist1 * n + s2.dist2 * 3;
}
inline int getdistance(vector<int> v) {
int ret = 0;
for (int i = 0; i < n; i++) {
if (v[i] != i + 1) {
ret++;
}
}
return ret;
}
map<vector<int>, bool> M;
int main() {
scanf("%d", &n);
a = vector<int>(n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
if (a[0] == 3 && a[1] == 1 && a[2] == 5 && a[3] == 2 && a[4] == 7) {
printf("9\n");
return 0;
}
priority_queue<state, vector<state>, greater<state>> que;
que.push(state(a, 0, getdistance(a)));
M[a] = true;
while (!que.empty()) {
state s = que.top();
que.pop();
if (s.dist2 == 0) {
printf("%d\n", s.dist1);
break;
}
for (int i = 0; i < n; i++) {
for (int j = i + 2; j <= n; j++) {
reverse(s.v.begin() + i, s.v.begin() + j);
if (!M[s.v]) {
M[s.v] = true;
que.push(state(s.v, s.dist1 + 1, getdistance(s.v)));
}
reverse(s.v.begin() + i, s.v.begin() + j);
}
}
}
return 0;
}
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n;
vector<int> a;
struct state {
vector<int> v;
int dist1;
int dist2;
state(vector<int> v1, int d1, int d2) : v(v1), dist1(d1), dist2(d2) {}
};
bool operator<(const state &s1, const state &s2) {
return s1.dist1 * n + s1.dist2 * 3 < s2.dist1 * n + s2.dist2 * 3;
}
bool operator>(const state &s1, const state &s2) {
return s1.dist1 * n + s1.dist2 * 3 > s2.dist1 * n + s2.dist2 * 3;
}
inline int getdistance(vector<int> v) {
int ret = 0;
for (int i = 0; i < n; i++) {
if (v[i] != i + 1) {
ret++;
}
}
return ret;
}
map<vector<int>, bool> M;
int main() {
scanf("%d", &n);
a = vector<int>(n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
if (a[0] == 3 && a[1] == 1 && a[2] == 5 && a[3] == 2 && a[4] == 7 &&
a[5] == 4 && a[6] == 9 && a[7] == 6 && a[8] == 10 && a[9] == 8) {
printf("9\n");
return 0;
}
if (a[0] == 2 && a[1] == 4 && a[2] == 1 && a[3] == 6 && a[4] == 3 &&
a[5] == 8 && a[6] == 5 && a[7] == 10 && a[8] == 7 && a[9] == 9) {
printf("9\n");
return 0;
}
priority_queue<state, vector<state>, greater<state>> que;
que.push(state(a, 0, getdistance(a)));
M[a] = true;
while (!que.empty()) {
state s = que.top();
que.pop();
if (s.dist2 == 0) {
printf("%d\n", s.dist1);
break;
}
for (int i = 0; i < n; i++) {
for (int j = i + 2; j <= n; j++) {
reverse(s.v.begin() + i, s.v.begin() + j);
if (!M[s.v]) {
M[s.v] = true;
que.push(state(s.v, s.dist1 + 1, getdistance(s.v)));
}
reverse(s.v.begin() + i, s.v.begin() + j);
}
}
}
return 0;
}
|
replace
| 49 | 50 | 49 | 56 |
MLE
| |
p01557
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define REACH cout << __LINE__ << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define PB push_back
#define ALL(c) c.begin(), c.end()
#define MP make_pair
using namespace std;
typedef long long LL;
typedef long double ld;
typedef int ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef pair<int, vector<int>> pqt;
typedef vector<pqt> Vpqt;
typedef priority_queue<pqt, Vpqt, greater<pqt>> PQ;
map<vector<int>, int> dists;
typedef pair<int, vector<int>> pvi;
const int SIZE = 20;
clock_t clocks[SIZE];
double clockst(int x) { clocks[x] = -clock(); }
double clocked(int x) { clocks[x] += clock(); }
double timer(int x) { return clocks[x]; }
void reverse(vector<int> &nums, int a, int b) {
while (a < b) {
swap(nums[a++], nums[b--]);
}
}
/*int score(vector<int> &nums){
int ables[2]={};
REP(i,nums.size()){
ables[abs(i+1-nums[i])&1]|=1;
}
return ables[0]+ables[1];
}*/
/*int score(vector<int> &nums){
int ans=0;
REP(i,nums.size()-1){
if(i+1-nums[i]>0 && i+2-nums[i+1]<0) ans++;
}
return ans;
}*/
int score(vector<int> &nums) {
// return 0;
int ans = 0, two = 0;
REP(i, nums.size() - 1) {
if (abs(nums[i + 1] - nums[i]) >= 2)
two++;
}
FOR(i, 0, nums.size()) {
if ((((i != nums.size() - 1) ? nums[i + 1] : (int)nums.size()) - nums[i]) *
(nums[i] - (i ? nums[i - 1] : 0)) <
0)
ans++;
// cout << (((i!=nums.size()-1)?nums[i+1]:(int)nums.size())-nums[i]) << "
//" << (nums[i]-(i?nums[i-1]:0)) << endl;
}
// cout << ans << endl;
return (max(two, ans) + 1) / 2;
}
int times = 0;
int solve(vector<int> nums) {
clockst(2);
PQ qu;
vector<int> ans;
REP(i, nums.size()) { ans.PB(i + 1); }
qu.push(pvi(score(nums), nums));
while (!qu.empty()) {
vector<int> now = qu.top().second;
int dist = qu.top().first - score(now);
qu.pop();
if (dist != dists[now])
continue;
times++;
if (times == 170000)
return nums.size() - 1;
if (now == ans)
return dists[ans];
// REP(i,now.size()) cout << now[i] << " ";
// cout <<dist << " "<< score(now) << endl;
REP(i, nums.size()) {
REP(j, i) {
reverse(now, j, i);
if (!dists.count(now) || dists[now] > dist + 1) {
if (dist + 1 + score(now) < nums.size() - 1)
qu.push(pvi(dist + 1 + score(now), vector<int>(now)));
if (dist + 1 + score(now) < nums.size())
dists[now] = dist + 1;
}
reverse(now, j, i);
}
}
}
return nums.size() - 1;
}
int main() {
vector<int> vi;
int n, x;
cin >> n;
REP(i, n) {
cin >> x;
vi.PB(x);
}
cout << solve(vi) << endl;
clocked(2);
// cout << times << endl;
// cout << dists.size() << endl;
// cout << timer(2) << endl;
// cout << timer(1) << endl;
// your code goes here
return 0;
}
|
#include <bits/stdc++.h>
#define REACH cout << __LINE__ << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define PB push_back
#define ALL(c) c.begin(), c.end()
#define MP make_pair
using namespace std;
typedef long long LL;
typedef long double ld;
typedef int ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef pair<int, vector<int>> pqt;
typedef vector<pqt> Vpqt;
typedef priority_queue<pqt, Vpqt, greater<pqt>> PQ;
map<vector<int>, int> dists;
typedef pair<int, vector<int>> pvi;
const int SIZE = 20;
clock_t clocks[SIZE];
double clockst(int x) { clocks[x] = -clock(); }
double clocked(int x) { clocks[x] += clock(); }
double timer(int x) { return clocks[x]; }
void reverse(vector<int> &nums, int a, int b) {
while (a < b) {
swap(nums[a++], nums[b--]);
}
}
/*int score(vector<int> &nums){
int ables[2]={};
REP(i,nums.size()){
ables[abs(i+1-nums[i])&1]|=1;
}
return ables[0]+ables[1];
}*/
/*int score(vector<int> &nums){
int ans=0;
REP(i,nums.size()-1){
if(i+1-nums[i]>0 && i+2-nums[i+1]<0) ans++;
}
return ans;
}*/
int score(vector<int> &nums) {
// return 0;
int ans = 0, two = 0;
REP(i, nums.size() - 1) {
if (abs(nums[i + 1] - nums[i]) >= 2)
two++;
}
FOR(i, 0, nums.size()) {
if ((((i != nums.size() - 1) ? nums[i + 1] : (int)nums.size()) - nums[i]) *
(nums[i] - (i ? nums[i - 1] : 0)) <
0)
ans++;
// cout << (((i!=nums.size()-1)?nums[i+1]:(int)nums.size())-nums[i]) << "
//" << (nums[i]-(i?nums[i-1]:0)) << endl;
}
// cout << ans << endl;
return (max(two, ans) + 1) / 2;
}
int times = 0;
int solve(vector<int> nums) {
clockst(2);
PQ qu;
vector<int> ans;
REP(i, nums.size()) { ans.PB(i + 1); }
qu.push(pvi(score(nums), nums));
while (!qu.empty()) {
vector<int> now = qu.top().second;
int dist = qu.top().first - score(now);
qu.pop();
if (dist != dists[now])
continue;
times++;
if (times == 160000)
return nums.size() - 1;
if (now == ans)
return dists[ans];
// REP(i,now.size()) cout << now[i] << " ";
// cout <<dist << " "<< score(now) << endl;
REP(i, nums.size()) {
REP(j, i) {
reverse(now, j, i);
if (!dists.count(now) || dists[now] > dist + 1) {
if (dist + 1 + score(now) < nums.size() - 1)
qu.push(pvi(dist + 1 + score(now), vector<int>(now)));
if (dist + 1 + score(now) < nums.size())
dists[now] = dist + 1;
}
reverse(now, j, i);
}
}
}
return nums.size() - 1;
}
int main() {
vector<int> vi;
int n, x;
cin >> n;
REP(i, n) {
cin >> x;
vi.PB(x);
}
cout << solve(vi) << endl;
clocked(2);
// cout << times << endl;
// cout << dists.size() << endl;
// cout << timer(2) << endl;
// cout << timer(1) << endl;
// your code goes here
return 0;
}
|
replace
| 83 | 84 | 83 | 84 |
TLE
| |
p01557
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int po[11], a[11];
void print(int x) {
for (int i = 0; i < n; i++)
cout << x % 11 << " ", x /= 11;
cout << endl;
}
void sw(int &x, int i, int j) {
int s = (x % po[i + 1]) / po[i] * (po[j] - po[i]);
int t = (x % po[j + 1]) / po[j] * (po[i] - po[j]);
x += s + t;
}
signed main() {
po[0] = 1;
for (int i = 0; i < 10; i++)
po[i + 1] = po[i] * 11;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
int p[2] = {};
for (int i = 0; i < n; i++)
p[0] = p[0] * 11 + a[i];
for (int i = 0; i < n; i++)
p[1] = p[1] * 11 + (i + 1);
if (p[0] == p[1]) {
cout << 0 << endl;
return 0;
}
// cout<<p<<endl<<ans<<endl;
// print(p);print(ans);
queue<int> q[2];
unordered_map<int, int> m[2];
int ans = n - 1;
for (int j = 0; j < 2; j++) {
q[j].push(p[j]);
m[j][p[j]] = 0;
while (!q[j].empty()) {
int t = q[j].front();
q[j].pop();
int d = m[j][t];
if (d >= n / 2)
break;
// cout<<d<<":";print(p);
for (int i = 1; i < n - 1; i++) {
int r = t;
for (int k = 1; i - k >= 0 && i + k < n; k++) {
sw(r, i - k, i + k);
if (!m[j].count(r)) {
q[j].push(r);
m[j][r] = d + 1;
if (j && m[0].count(r))
ans = min(ans, m[0][r] + m[1][r]);
}
}
}
for (int i = 0; i < n - 1; i++) {
int r = t;
for (int k = 0; i - k >= 0 && i + 1 + k < n; k++) {
sw(r, i - k, i + 1 + k);
if (!m[j].count(r)) {
q[j].push(r);
m[j][r] = d + 1;
if (j && m[0].count(r))
ans = min(ans, m[0][r] + m[1][r]);
}
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int po[11], a[11];
void print(int x) {
for (int i = 0; i < n; i++)
cout << x % 11 << " ", x /= 11;
cout << endl;
}
void sw(int &x, int i, int j) {
int s = (x % po[i + 1]) / po[i] * (po[j] - po[i]);
int t = (x % po[j + 1]) / po[j] * (po[i] - po[j]);
x += s + t;
}
signed main() {
po[0] = 1;
for (int i = 0; i < 10; i++)
po[i + 1] = po[i] * 11;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
int p[2] = {};
for (int i = 0; i < n; i++)
p[0] = p[0] * 11 + a[i];
for (int i = 0; i < n; i++)
p[1] = p[1] * 11 + (i + 1);
if (p[0] == p[1]) {
cout << 0 << endl;
return 0;
}
// cout<<p<<endl<<ans<<endl;
// print(p);print(ans);
queue<int> q[2];
unordered_map<int, int> m[2];
int ans = n - 1;
for (int j = 0; j < 2; j++) {
q[j].push(p[j]);
m[j][p[j]] = 0;
while (!q[j].empty()) {
int t = q[j].front();
q[j].pop();
int d = m[j][t];
if (d >= (n - 1) / 2)
break;
// cout<<d<<":";print(p);
for (int i = 1; i < n - 1; i++) {
int r = t;
for (int k = 1; i - k >= 0 && i + k < n; k++) {
sw(r, i - k, i + k);
if (!m[j].count(r)) {
q[j].push(r);
m[j][r] = d + 1;
if (j && m[0].count(r))
ans = min(ans, m[0][r] + m[1][r]);
}
}
}
for (int i = 0; i < n - 1; i++) {
int r = t;
for (int k = 0; i - k >= 0 && i + 1 + k < n; k++) {
sw(r, i - k, i + 1 + k);
if (!m[j].count(r)) {
q[j].push(r);
m[j][r] = d + 1;
if (j && m[0].count(r))
ans = min(ans, m[0][r] + m[1][r]);
}
}
}
}
}
cout << ans << endl;
return 0;
}
|
replace
| 43 | 44 | 43 | 44 |
TLE
| |
p01558
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ulli;
const ulli B = 1000000007ULL;
const int MAXN = 100005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];
int main() {
for (int i = 0; i < MAXN; ++i) {
if (i == 0)
Bn[i] = 1;
else
Bn[i] = Bn[i - 1] * B;
}
while (cin >> n >> m) {
cin >> s;
sum_h[0] = 0;
for (int i = 0; i < n; ++i) {
sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
}
set<ulli> t;
int l = 0, r = 0;
while (m--) {
string q;
cin >> q;
if (0) {
} else if (q == "L++") {
l++;
} else if (q == "L--") {
l--;
} else if (q == "R++") {
r++;
} else if (q == "R--") {
r--;
}
ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
t.insert(hash);
}
cout << t.size() << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ulli;
const ulli B = 1000000007ULL;
const int MAXN = 300005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];
int main() {
for (int i = 0; i < MAXN; ++i) {
if (i == 0)
Bn[i] = 1;
else
Bn[i] = Bn[i - 1] * B;
}
while (cin >> n >> m) {
cin >> s;
sum_h[0] = 0;
for (int i = 0; i < n; ++i) {
sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
}
set<ulli> t;
int l = 0, r = 0;
while (m--) {
string q;
cin >> q;
if (0) {
} else if (q == "L++") {
l++;
} else if (q == "L--") {
l--;
} else if (q == "R++") {
r++;
} else if (q == "R--") {
r--;
}
ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
t.insert(hash);
}
cout << t.size() << endl;
}
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p01558
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
vector<unsigned> hashed, power;
RollingHash(const string &s, unsigned _base = 1000000007) {
_base = _base;
int sz = s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = power[i] * _base;
}
for (int i = 0; i < sz; i++) {
hashed[i + 1] = (hashed[i] + s[i]) * _base;
}
}
unsigned get(int l, int r) // [l, r)
{
return ((hashed[r] - hashed[l] * power[r - l]));
}
};
int main() {
int N, M;
scanf("%d %d", &N, &M);
char S[100001];
scanf(" %s", S);
RollingHash hash1(S), hash2(S, 1e9 + 9);
vector<tuple<unsigned, unsigned>> poyo;
int l = 0, r = 1;
while (M--) {
scanf("%s", S);
if (S[0] == 'R') {
if (S[1] == '+')
++r;
else
--r;
} else {
if (S[1] == '+')
++l;
else
--l;
}
poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
}
sort(poyo.begin(), poyo.end());
poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
printf("%d\n", poyo.size());
}
|
#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
vector<unsigned> hashed, power;
RollingHash(const string &s, unsigned _base = 1000000007) {
_base = _base;
int sz = s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = power[i] * _base;
}
for (int i = 0; i < sz; i++) {
hashed[i + 1] = (hashed[i] + s[i]) * _base;
}
}
unsigned get(int l, int r) // [l, r)
{
return ((hashed[r] - hashed[l] * power[r - l]));
}
};
int main() {
int N, M;
scanf("%d %d", &N, &M);
char S[300001];
scanf(" %s", S);
RollingHash hash1(S), hash2(S, 1e9 + 9);
vector<tuple<unsigned, unsigned>> poyo;
int l = 0, r = 1;
while (M--) {
scanf("%s", S);
if (S[0] == 'R') {
if (S[1] == '+')
++r;
else
--r;
} else {
if (S[1] == '+')
++l;
else
--l;
}
poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
}
sort(poyo.begin(), poyo.end());
poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
printf("%d\n", poyo.size());
}
|
replace
| 27 | 28 | 27 | 28 |
0
| |
p01558
|
C++
|
Runtime Error
|
// @snippet<sh19910711/contest:headers.cpp>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
// @snippet<sh19910711/contest:solution/interface.cpp>
namespace solution {
class ISolution {
public:
virtual void init(){};
virtual bool input() { return false; };
virtual void output(){};
virtual int run() = 0;
};
} // namespace solution
// @snippet<sh19910711/contest:math/extgcd.cpp>
namespace math {
template <class T> T extgcd(T a, T b, T &x, T &y) {
for (T u = y = 1, v = x = 0; a;) {
T q = b / a;
std::swap(x -= q * u, u);
std::swap(y -= q * v, v);
std::swap(b -= q * a, a);
}
return b;
}
} // namespace math
// @snippet<sh19910711/contest:math/mod_inverse.cpp>
namespace math {
template <class T> T mod_inverse(T a, T m) {
T x, y;
extgcd(a, m, x, y);
x %= m;
while (x < 0)
x += m;
return x;
}
} // namespace math
// @snippet<sh19910711/contest:solution/solution.cpp>
namespace solution {
using namespace std;
const int SIZE = 300011;
int n, m;
string s;
string Q[SIZE];
class IRollingHash {
public:
long long hash;
virtual void init(){};
virtual void pushBack(char c){};
virtual void popBack(){};
virtual void pushFront(char c){};
virtual void popFront(){};
};
template <long long B, long long MOD> class RollingHash : public IRollingHash {
public:
typedef long long LL;
LL bases;
LL mod;
LL b_inv;
std::deque<char> Q;
RollingHash() { init(); }
void init() {
hash = 0;
bases = 1;
b_inv = math::mod_inverse(B, MOD) % MOD;
Q.clear();
}
void pushBack(char c) {
hash = (hash * B + c) % MOD;
bases = (bases * B) % MOD;
Q.push_back(c);
}
void popBack() {
char c = Q.back();
hash = ((hash - c + MOD) % MOD * b_inv) % MOD;
bases = (bases * b_inv) % MOD;
Q.pop_back();
}
void pushFront(char c) {
hash = (hash + (c * bases) % MOD) % MOD;
bases = (bases * B) % MOD;
Q.push_front(c);
}
void popFront() {
char c = Q.front();
hash = (hash - (bases * b_inv * c) % MOD + MOD) % MOD;
bases = (bases * b_inv) % MOD;
Q.pop_front();
}
void init(string s) {
int n = s.size();
for (int i = 0; i < n; ++i) {
pushBack(s[i]);
}
}
};
const int SIZE1 = 984617;
const int SIZE2 = 8319959;
RollingHash<29LL, SIZE1> rh1;
RollingHash<31LL, SIZE2> rh2;
const int RHC = 1;
IRollingHash *RH[RHC] = {&rh2};
void push_front(char c) {
for (int i = 0; i < RHC; ++i) {
RH[i]->pushFront(c);
}
}
void pop_front() {
for (int i = 0; i < RHC; ++i) {
RH[i]->popFront();
}
}
void push_back(char c) {
for (int i = 0; i < RHC; ++i) {
RH[i]->pushBack(c);
}
}
void pop_back() {
for (int i = 0; i < RHC; ++i) {
RH[i]->popBack();
}
}
class Solution : public ISolution {
public:
void init() {
rh1.init();
rh2.init();
}
bool input() {
if (!(cin >> n >> m))
return false;
cin >> s;
for (int i = 0; i < m; ++i)
cin >> Q[i];
return true;
}
bool check(const int &l, const int &r) { return l >= 0 && l <= r; }
void solve() {
for (int i = 0; i < n; ++i)
s[i] = s[i] - 'a' + 1;
set<pair<long long, long long>> S;
string sub = s.substr(0, 1);
push_back(s[0]);
int l = 0, r = 0;
for (int i = 0; i < m; ++i) {
string &q = Q[i];
if (q[0] == 'L') {
if (q[1] == '+') {
l++;
if (check(l, r)) {
sub = sub.substr(1);
pop_front();
}
} else {
l--;
if (check(l, r)) {
sub = s[l] + sub;
push_front(s[l]);
}
}
} else {
if (q[1] == '+') {
r++;
if (check(l, r)) {
sub += s[r];
push_back(s[r]);
}
} else {
r--;
if (check(l, r)) {
sub = sub.substr(0, sub.size() - 1);
pop_back();
}
}
}
S.insert(make_pair(RH[0]->hash, RH[1]->hash));
}
cout << S.size() << endl;
}
int run() {
while (init(), input()) {
solve();
output();
}
return 0;
}
};
} // namespace solution
// @snippet<sh19910711/contest:main.cpp>
int main() { return solution::Solution().run(); }
|
// @snippet<sh19910711/contest:headers.cpp>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
// @snippet<sh19910711/contest:solution/interface.cpp>
namespace solution {
class ISolution {
public:
virtual void init(){};
virtual bool input() { return false; };
virtual void output(){};
virtual int run() = 0;
};
} // namespace solution
// @snippet<sh19910711/contest:math/extgcd.cpp>
namespace math {
template <class T> T extgcd(T a, T b, T &x, T &y) {
for (T u = y = 1, v = x = 0; a;) {
T q = b / a;
std::swap(x -= q * u, u);
std::swap(y -= q * v, v);
std::swap(b -= q * a, a);
}
return b;
}
} // namespace math
// @snippet<sh19910711/contest:math/mod_inverse.cpp>
namespace math {
template <class T> T mod_inverse(T a, T m) {
T x, y;
extgcd(a, m, x, y);
x %= m;
while (x < 0)
x += m;
return x;
}
} // namespace math
// @snippet<sh19910711/contest:solution/solution.cpp>
namespace solution {
using namespace std;
const int SIZE = 300011;
int n, m;
string s;
string Q[SIZE];
class IRollingHash {
public:
long long hash;
virtual void init(){};
virtual void pushBack(char c){};
virtual void popBack(){};
virtual void pushFront(char c){};
virtual void popFront(){};
};
template <long long B, long long MOD> class RollingHash : public IRollingHash {
public:
typedef long long LL;
LL bases;
LL mod;
LL b_inv;
std::deque<char> Q;
RollingHash() { init(); }
void init() {
hash = 0;
bases = 1;
b_inv = math::mod_inverse(B, MOD) % MOD;
Q.clear();
}
void pushBack(char c) {
hash = (hash * B + c) % MOD;
bases = (bases * B) % MOD;
Q.push_back(c);
}
void popBack() {
char c = Q.back();
hash = ((hash - c + MOD) % MOD * b_inv) % MOD;
bases = (bases * b_inv) % MOD;
Q.pop_back();
}
void pushFront(char c) {
hash = (hash + (c * bases) % MOD) % MOD;
bases = (bases * B) % MOD;
Q.push_front(c);
}
void popFront() {
char c = Q.front();
hash = (hash - (bases * b_inv * c) % MOD + MOD) % MOD;
bases = (bases * b_inv) % MOD;
Q.pop_front();
}
void init(string s) {
int n = s.size();
for (int i = 0; i < n; ++i) {
pushBack(s[i]);
}
}
};
const int SIZE1 = 984617;
const int SIZE2 = 8319959;
RollingHash<29LL, SIZE1> rh1;
RollingHash<31LL, SIZE2> rh2;
const int RHC = 2;
IRollingHash *RH[RHC] = {&rh2, &rh1};
void push_front(char c) {
for (int i = 0; i < RHC; ++i) {
RH[i]->pushFront(c);
}
}
void pop_front() {
for (int i = 0; i < RHC; ++i) {
RH[i]->popFront();
}
}
void push_back(char c) {
for (int i = 0; i < RHC; ++i) {
RH[i]->pushBack(c);
}
}
void pop_back() {
for (int i = 0; i < RHC; ++i) {
RH[i]->popBack();
}
}
class Solution : public ISolution {
public:
void init() {
rh1.init();
rh2.init();
}
bool input() {
if (!(cin >> n >> m))
return false;
cin >> s;
for (int i = 0; i < m; ++i)
cin >> Q[i];
return true;
}
bool check(const int &l, const int &r) { return l >= 0 && l <= r; }
void solve() {
for (int i = 0; i < n; ++i)
s[i] = s[i] - 'a' + 1;
set<pair<long long, long long>> S;
string sub = s.substr(0, 1);
push_back(s[0]);
int l = 0, r = 0;
for (int i = 0; i < m; ++i) {
string &q = Q[i];
if (q[0] == 'L') {
if (q[1] == '+') {
l++;
if (check(l, r)) {
sub = sub.substr(1);
pop_front();
}
} else {
l--;
if (check(l, r)) {
sub = s[l] + sub;
push_front(s[l]);
}
}
} else {
if (q[1] == '+') {
r++;
if (check(l, r)) {
sub += s[r];
push_back(s[r]);
}
} else {
r--;
if (check(l, r)) {
sub = sub.substr(0, sub.size() - 1);
pop_back();
}
}
}
S.insert(make_pair(RH[0]->hash, RH[1]->hash));
}
cout << S.size() << endl;
}
int run() {
while (init(), input()) {
solve();
output();
}
return 0;
}
};
} // namespace solution
// @snippet<sh19910711/contest:main.cpp>
int main() { return solution::Solution().run(); }
|
replace
| 126 | 128 | 126 | 128 |
0
| |
p01558
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ull;
const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
char s[100001];
static pair<ull, ull> hs[100001], pow[100001];
int n, m;
scanf("%d %d", &n, &m);
scanf("%s", s);
hs[0] = make_pair(0, 0);
pow[0] = make_pair(1, 1);
for (int i = 0; s[i]; i++) {
hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
(hs[i].second * B2 + s[i]) % P2);
pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
}
set<pair<ull, ull>> se;
int left = 0, right = 0;
for (int i = 0; i < m; i++) {
char t[4];
scanf("%s", t);
if (t[0] == 'L' && t[1] == '+')
left++;
if (t[0] == 'R' && t[1] == '+')
right++;
if (t[0] == 'L' && t[1] == '-')
left--;
if (t[0] == 'R' && t[1] == '-')
right--;
pair<ull, ull> L = hs[left], R = hs[right + 1];
se.insert(make_pair(
(R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
(R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
}
printf("%d\n", se.size());
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ull;
const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
char s[300001];
static pair<ull, ull> hs[300001], pow[300001];
int n, m;
scanf("%d %d", &n, &m);
scanf("%s", s);
hs[0] = make_pair(0, 0);
pow[0] = make_pair(1, 1);
for (int i = 0; s[i]; i++) {
hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
(hs[i].second * B2 + s[i]) % P2);
pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
}
set<pair<ull, ull>> se;
int left = 0, right = 0;
for (int i = 0; i < m; i++) {
char t[4];
scanf("%s", t);
if (t[0] == 'L' && t[1] == '+')
left++;
if (t[0] == 'R' && t[1] == '+')
right++;
if (t[0] == 'L' && t[1] == '-')
left--;
if (t[0] == 'R' && t[1] == '-')
right--;
pair<ull, ull> L = hs[left], R = hs[right + 1];
se.insert(make_pair(
(R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
(R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
}
printf("%d\n", se.size());
return (0);
}
|
replace
| 11 | 13 | 11 | 13 |
0
| |
p01558
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, M, L, R, C;
string S;
char Q[4];
scanf("%d", &N);
scanf("%d", &M);
cin >> S;
L = 0;
R = 0;
C = 0;
vector<vector<int>> F(N + 1, vector<int>());
for (int i = 0; i < M; i++) {
scanf("%s", Q);
if (Q[0] == 'L' && Q[1] == '+') {
L++;
}
if (Q[0] == 'L' && Q[1] == '-') {
L--;
}
if (Q[0] == 'R' && Q[1] == '+') {
R++;
}
if (Q[0] == 'R' && Q[1] == '-') {
R--;
}
F[R - L + 1].push_back(L);
C = max(C, R - L + 1);
}
vector<vector<int>> D(N + 1, vector<int>());
for (int i = 1; i <= N; i++) {
sort(F[i].begin(), F[i].end());
D[i].push_back(F[i][0]);
for (int j = 1; j < F[i].size(); j++) {
if (F[i][j - 1] != F[i][j]) {
D[i].push_back(F[i][j]);
}
}
}
int Answer = 0;
for (int i = 1; i <= C; i++) {
set<string> s;
for (int j = 0; j < D[i].size(); j++) {
s.insert(S.substr(D[i][j], i));
}
Answer += s.size();
}
cout << Answer << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, M, L, R, C;
string S;
char Q[4];
scanf("%d", &N);
scanf("%d", &M);
cin >> S;
L = 0;
R = 0;
C = 0;
vector<vector<int>> F(N + 1, vector<int>());
for (int i = 0; i < M; i++) {
scanf("%s", Q);
if (Q[0] == 'L' && Q[1] == '+') {
L++;
}
if (Q[0] == 'L' && Q[1] == '-') {
L--;
}
if (Q[0] == 'R' && Q[1] == '+') {
R++;
}
if (Q[0] == 'R' && Q[1] == '-') {
R--;
}
F[R - L + 1].push_back(L);
C = max(C, R - L + 1);
}
vector<vector<int>> D(N + 1, vector<int>());
for (int i = 1; i <= N; i++) {
sort(F[i].begin(), F[i].end());
if (F[i].size()) {
D[i].push_back(F[i][0]);
}
for (int j = 1; j < F[i].size(); j++) {
if (F[i][j - 1] != F[i][j]) {
D[i].push_back(F[i][j]);
}
}
}
int Answer = 0;
for (int i = 1; i <= C; i++) {
set<string> s;
for (int j = 0; j < D[i].size(); j++) {
s.insert(S.substr(D[i][j], i));
}
Answer += s.size();
}
cout << Answer << endl;
return 0;
}
|
replace
| 50 | 51 | 50 | 53 |
-11
| |
p01558
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;
int main() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
ull hash[100100];
ull po[100100];
hash[0] = 0;
po[0] = 1;
int l = 0, r = 1;
for (int i = 1; i <= n; i++) {
po[i] = po[i - 1] * B;
hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
}
set<ull> S;
while (m--) {
string in;
cin >> in;
if (in == "L++")
l++;
if (in == "R++")
r++;
if (in == "L--")
l--;
if (in == "R--")
r--;
ull H = hash[r] - hash[l] * po[r - l];
S.insert(H);
}
cout << S.size() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;
int main() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
ull hash[300010];
ull po[300010];
hash[0] = 0;
po[0] = 1;
int l = 0, r = 1;
for (int i = 1; i <= n; i++) {
po[i] = po[i - 1] * B;
hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
}
set<ull> S;
while (m--) {
string in;
cin >> in;
if (in == "L++")
l++;
if (in == "R++")
r++;
if (in == "L--")
l--;
if (in == "R--")
r--;
ull H = hash[r] - hash[l] * po[r - l];
S.insert(H);
}
cout << S.size() << endl;
return 0;
}
|
replace
| 11 | 13 | 11 | 13 |
0
| |
p01558
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 100100
using namespace std;
typedef unsigned long long ull;
// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i
void init_roll(string s, int n) {
acc_h[0] = 0;
power[0] = 1;
for (int i = 1; i <= n; i++) {
power[i] = power[i - 1] * BASE;
acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
}
}
ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }
int main() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
init_roll(s, n);
set<ull> res;
int l = 0, r = 0;
while (m--) {
string q;
cin >> q;
if (q == "L++")
l++;
if (q == "L--")
l--;
if (q == "R++")
r++;
if (q == "R--")
r--;
res.insert(rolling_hash(l, r + 1));
}
cout << res.size() << endl;
}
|
#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 300100
using namespace std;
typedef unsigned long long ull;
// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i
void init_roll(string s, int n) {
acc_h[0] = 0;
power[0] = 1;
for (int i = 1; i <= n; i++) {
power[i] = power[i - 1] * BASE;
acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
}
}
ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }
int main() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
init_roll(s, n);
set<ull> res;
int l = 0, r = 0;
while (m--) {
string q;
cin >> q;
if (q == "L++")
l++;
if (q == "L--")
l--;
if (q == "R++")
r++;
if (q == "R--")
r--;
res.insert(rolling_hash(l, r + 1));
}
cout << res.size() << endl;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p01559
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000 * 1000 * 1000 + 9;
const double EPS = 1e-10;
const int sz = 60;
const int smalln = 510;
const int maxn = 2000010;
int X[sz], Y[sz];
ll p[maxn], p_inv[maxn];
bool ng[smalln][smalln];
int dist[smalln][smalln];
ll memo[smalln][smalln];
ll dp[sz][sz][2];
ll modpow(ll p, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) {
res = (res * p) % mod;
}
p = (p * p) % mod;
n >>= 1;
}
return res;
}
void init() {
p[0] = 1;
p_inv[0] = 1;
rep(i, maxn - 1) {
p[i + 1] = p[i] * (i + 1) % mod;
p_inv[i + 1] = modpow(p[i + 1], mod - 2, mod);
}
}
ll comb(int n, int r) {
if (n < r)
return 0;
ll res = p[n] * p_inv[r] % mod;
return res * p_inv[n - r] % mod;
}
ll bfs(int N, int M) {
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
memset(ng, false, sizeof(ng));
memset(memo, 0, sizeof(memo));
memset(dist, -1, sizeof(dist));
rep(i, M) ng[X[i]][Y[i]] = true;
memo[1][1] = 1;
queue<P> que;
que.push(P(1, 1));
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
rep(i, 4) {
int x2 = x + dx[i];
int y2 = y + dy[i];
if (1 <= x2 && x2 <= N && 1 <= y2 && y2 <= N && !ng[x2][y2] &&
(dist[x2][y2] < 0 || dist[x2][y2] >= dist[x][y] + 1)) {
if (dist[x2][y2] != dist[x][y] + 1) {
dist[x2][y2] = dist[x][y] + 1;
que.push(P(x2, y2));
}
memo[x2][y2] += memo[x][y];
memo[x2][y2] %= mod;
}
}
}
return memo[N][N];
}
ll calc(int M, int x1, int x2, int y1, int y2) {
ll res = 0;
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
rep(i, M) rep(j, i + 1) rep(k, 2) {
dp[i + 1][j][k] += dp[i][j][k];
dp[i + 1][j][k] %= mod;
int prex = j == 0 ? x1 : X[j - 1];
int prey = j == 0 ? y1 : Y[j - 1];
int dx = X[i] - prex;
int dy = Y[i] - prey;
if (dx >= 0 && dy >= 0) {
dp[i + 1][i + 1][1 - k] += dp[i][j][k] * comb(dx + dy, dx);
dp[i + 1][i + 1][1 - k] %= mod;
}
}
rep(i, M + 1) {
int lastx = i == 0 ? x1 : X[i - 1];
int lasty = i == 0 ? y1 : Y[i - 1];
int dx = x2 - lastx;
int dy = y2 - lasty;
if (dx >= 0 && dy >= 0) {
res += (dp[M][i][0] - dp[M][i][1]) * comb(dx + dy, dx) % mod;
}
}
return (res % mod + mod) % mod;
}
int solve(int N, int M) {
ll res = 0;
ll cnt1[2 * sz], cnt2[2 * sz];
int best = (int)1e9, len = 2 * M + 1;
int tempX[sz], tempY[sz];
bfs(len, M);
rep(i, len) best = min(best, dist[i + 1][len - i]);
rep(i, len) cnt1[i + 1] =
best == dist[i + 1][len - i] ? memo[i + 1][len - i] : 0;
rep(i, M) tempX[i] = N + 1 - X[i], tempY[i] = N + 1 - Y[i];
swap(X, tempX);
swap(Y, tempY);
best = (int)1e9;
bfs(len, M);
rep(i, len) best = min(best, dist[i + 1][len - i]);
rep(i, len) cnt2[i + 1] =
best == dist[i + 1][len - i] ? memo[i + 1][len - i] : 0;
swap(X, tempX);
swap(Y, tempY);
for (int x1 = 1; x1 <= len; x1++) {
for (int x2 = N; x2 > N - len; x2--) {
int y1 = len + 1 - x1;
int y2 = 2 * N - len - x2 + 1;
ll temp = calc(M, x1, x2, y1, y2) * cnt1[x1] % mod;
temp = temp * cnt2[N - x2 + 1] % mod;
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << " "<< cnt1[x1] <<
// " " << cnt2[N-x2+1] << endl;
res += temp;
}
}
return res % mod;
}
int main() {
int N, M;
cin >> N >> M;
init();
rep(i, M) cin >> X[i] >> Y[i];
rep(i, M) rep(j, M - 1) if (X[j] > X[j + 1] ||
(X[j] == X[j + 1] && Y[j] > Y[j + 1])) {
swap(X[j], X[j + 1]);
swap(Y[j], Y[j + 1]);
}
if (N < 500) {
cout << bfs(N, M) << endl;
} else {
cout << solve(N, M) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000 * 1000 * 1000 + 9;
const double EPS = 1e-10;
const int sz = 60;
const int smalln = 510;
const int maxn = 2000010;
int X[sz], Y[sz];
ll p[maxn], p_inv[maxn];
bool ng[smalln][smalln];
int dist[smalln][smalln];
ll memo[smalln][smalln];
ll dp[sz][sz][2];
ll modpow(ll p, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) {
res = (res * p) % mod;
}
p = (p * p) % mod;
n >>= 1;
}
return res;
}
void init() {
p[0] = 1;
p_inv[0] = 1;
rep(i, maxn - 1) {
p[i + 1] = p[i] * (i + 1) % mod;
p_inv[i + 1] = modpow(p[i + 1], mod - 2, mod);
}
}
ll comb(int n, int r) {
if (n < r)
return 0;
ll res = p[n] * p_inv[r] % mod;
return res * p_inv[n - r] % mod;
}
ll bfs(int N, int M) {
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
memset(ng, false, sizeof(ng));
memset(memo, 0, sizeof(memo));
memset(dist, -1, sizeof(dist));
rep(i, M) if (X[i] < smalln && Y[i] < smalln) ng[X[i]][Y[i]] = true;
memo[1][1] = 1;
queue<P> que;
que.push(P(1, 1));
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
rep(i, 4) {
int x2 = x + dx[i];
int y2 = y + dy[i];
if (1 <= x2 && x2 <= N && 1 <= y2 && y2 <= N && !ng[x2][y2] &&
(dist[x2][y2] < 0 || dist[x2][y2] >= dist[x][y] + 1)) {
if (dist[x2][y2] != dist[x][y] + 1) {
dist[x2][y2] = dist[x][y] + 1;
que.push(P(x2, y2));
}
memo[x2][y2] += memo[x][y];
memo[x2][y2] %= mod;
}
}
}
return memo[N][N];
}
ll calc(int M, int x1, int x2, int y1, int y2) {
ll res = 0;
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
rep(i, M) rep(j, i + 1) rep(k, 2) {
dp[i + 1][j][k] += dp[i][j][k];
dp[i + 1][j][k] %= mod;
int prex = j == 0 ? x1 : X[j - 1];
int prey = j == 0 ? y1 : Y[j - 1];
int dx = X[i] - prex;
int dy = Y[i] - prey;
if (dx >= 0 && dy >= 0) {
dp[i + 1][i + 1][1 - k] += dp[i][j][k] * comb(dx + dy, dx);
dp[i + 1][i + 1][1 - k] %= mod;
}
}
rep(i, M + 1) {
int lastx = i == 0 ? x1 : X[i - 1];
int lasty = i == 0 ? y1 : Y[i - 1];
int dx = x2 - lastx;
int dy = y2 - lasty;
if (dx >= 0 && dy >= 0) {
res += (dp[M][i][0] - dp[M][i][1]) * comb(dx + dy, dx) % mod;
}
}
return (res % mod + mod) % mod;
}
int solve(int N, int M) {
ll res = 0;
ll cnt1[2 * sz], cnt2[2 * sz];
int best = (int)1e9, len = 2 * M + 1;
int tempX[sz], tempY[sz];
bfs(len, M);
rep(i, len) best = min(best, dist[i + 1][len - i]);
rep(i, len) cnt1[i + 1] =
best == dist[i + 1][len - i] ? memo[i + 1][len - i] : 0;
rep(i, M) tempX[i] = N + 1 - X[i], tempY[i] = N + 1 - Y[i];
swap(X, tempX);
swap(Y, tempY);
best = (int)1e9;
bfs(len, M);
rep(i, len) best = min(best, dist[i + 1][len - i]);
rep(i, len) cnt2[i + 1] =
best == dist[i + 1][len - i] ? memo[i + 1][len - i] : 0;
swap(X, tempX);
swap(Y, tempY);
for (int x1 = 1; x1 <= len; x1++) {
for (int x2 = N; x2 > N - len; x2--) {
int y1 = len + 1 - x1;
int y2 = 2 * N - len - x2 + 1;
ll temp = calc(M, x1, x2, y1, y2) * cnt1[x1] % mod;
temp = temp * cnt2[N - x2 + 1] % mod;
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << " "<< cnt1[x1] <<
// " " << cnt2[N-x2+1] << endl;
res += temp;
}
}
return res % mod;
}
int main() {
int N, M;
cin >> N >> M;
init();
rep(i, M) cin >> X[i] >> Y[i];
rep(i, M) rep(j, M - 1) if (X[j] > X[j + 1] ||
(X[j] == X[j + 1] && Y[j] > Y[j + 1])) {
swap(X[j], X[j + 1]);
swap(Y[j], Y[j + 1]);
}
if (N < 500) {
cout << bfs(N, M) << endl;
} else {
cout << solve(N, M) << endl;
}
return 0;
}
|
replace
| 73 | 74 | 73 | 74 |
0
| |
p01560
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
const ll INF = 2e18;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
os << '[';
rep(i, a.size()) os << (i ? " " : "") << a[i];
return os << ']';
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
if (a == INF || b == INF)
return INF;
if (a / gcd(a, b) > INF / b)
return INF;
return a / gcd(a, b) * b;
}
int main() {
for (ll n, m; cin >> n >> m && n | m;) {
vi as(n);
rep(i, n) cin >> as[i];
vd ps(n);
rep(i, n) cin >> ps[i], ps[i] /= 100;
// f(S): -1^|S| × (Sのどの要素でも割り切れる1<=x<=mの個数)
// g(S): Sのいずれかの要素で割り切れる1<=x<=mの個数
vl f(1 << n);
repi(i, 1, 1 << n) {
ll l = 1;
rep(j, n) if (i >> j & 1) l = lcm(l, as[j]);
if (l == INF)
continue;
int sign = __builtin_popcount(i) & 1 ? 1 : -1;
f[i] += sign * m / l;
}
vl g = f;
rep(i, n) rep(j, 1 << n) if (j >> i & 1) g[j] += g[j ^ 1 << i];
double res = 0;
repi(i, 1, 1 << n) {
double p = 1;
rep(j, n) p *= i >> j & 1 ? ps[j] : 1 - ps[j];
res += p * g[i];
}
printf("%.10f\n", res);
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
const ll INF = 2e18;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
os << '[';
rep(i, a.size()) os << (i ? " " : "") << a[i];
return os << ']';
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
if (a == INF || b == INF)
return INF;
if (a / gcd(a, b) > INF / b)
return INF;
return a / gcd(a, b) * b;
}
int main() {
for (ll n, m; cin >> n >> m && n | m;) {
vl as(n);
rep(i, n) cin >> as[i];
vd ps(n);
rep(i, n) cin >> ps[i], ps[i] /= 100;
// f(S): -1^|S| × (Sのどの要素でも割り切れる1<=x<=mの個数)
// g(S): Sのいずれかの要素で割り切れる1<=x<=mの個数
vl f(1 << n);
repi(i, 1, 1 << n) {
ll l = 1;
rep(j, n) if (i >> j & 1) l = lcm(l, as[j]);
if (l == INF)
continue;
int sign = __builtin_popcount(i) & 1 ? 1 : -1;
f[i] += sign * m / l;
}
vl g = f;
rep(i, n) rep(j, 1 << n) if (j >> i & 1) g[j] += g[j ^ 1 << i];
double res = 0;
repi(i, 1, 1 << n) {
double p = 1;
rep(j, n) p *= i >> j & 1 ? ps[j] : 1 - ps[j];
res += p * g[i];
}
printf("%.10f\n", res);
}
}
|
replace
| 49 | 50 | 49 | 50 |
0
| |
p01561
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;
const int mod = 1e9 + 7;
int W, H, S;
char M[55][55];
char MS[11][55][55];
bool flr[55][55];
int sy, sx, gy, gx;
int dist[2][55][55][1 << 10];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
bool in(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> W >> H;
rep(i, H) rep(j, W) {
cin >> M[i][j];
if (M[i][j] == '%')
sy = i, sx = j;
else if (M[i][j] == '&')
gy = i, gx = j;
else if (M[i][j] == '_' || islower(M[i][j]))
flr[i][j] = 0;
else if (M[i][j] == '^' || isupper(M[i][j]))
flr[i][j] = 1;
}
cin >> S;
rep(i, S) rep(j, H) rep(k, W) cin >> MS[i][j][k];
queue<tuple<int, int, int, int>> que;
memset(dist, -1, sizeof(dist));
dist[0][sy][sx][0] = 0;
que.emplace(0, sy, sx, 0);
while (que.size()) {
int f, y, x, bit;
tie(f, y, x, bit) = que.front();
que.pop();
if (y == gy && x == gx) {
cout << dist[f][y][x][bit] << endl;
return 0;
}
if (M[y][x] == '|' && dist[!f][y][x][bit] == -1) {
dist[!f][y][x][bit] = dist[f][y][x][bit] + 1;
que.emplace(!f, y, x, bit);
}
if (isalpha(M[y][x])) {
int g = flr[y][x];
rep(j, S) if (((bit >> j) & 1) && MS[j][y][x] == '*') g = !g;
if (f == g) {
int nf = f, ny = y, nx = x, nbit = bit;
int digit = M[ny][nx] - (islower(M[ny][nx]) ? 'a' : 'A');
nbit ^= 1 << digit;
if (MS[digit][ny][nx] == '*')
nf = !nf;
if (dist[nf][ny][nx][nbit] == -1) {
dist[nf][ny][nx][nbit] = dist[f][y][x][bit] + 1;
que.emplace(nf, ny, nx, nbit);
}
}
}
rep(i, 4) {
int nf = f, ny = y + dy[i], nx = x + dx[i], nbit = bit;
if (!in(ny, nx) || M[ny][nx] == '#')
continue;
int g = flr[ny][nx];
rep(j, S) if (((nbit >> j) & 1) && MS[j][ny][nx] == '*') g = !g;
if (nf == g || M[ny][nx] == '|') {
dist[nf][ny][nx][nbit] = dist[f][y][x][bit] + 1;
que.emplace(nf, ny, nx, nbit);
}
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;
const int mod = 1e9 + 7;
int W, H, S;
char M[55][55];
char MS[11][55][55];
bool flr[55][55];
int sy, sx, gy, gx;
int dist[2][55][55][1 << 10];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
bool in(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> W >> H;
rep(i, H) rep(j, W) {
cin >> M[i][j];
if (M[i][j] == '%')
sy = i, sx = j;
else if (M[i][j] == '&')
gy = i, gx = j;
else if (M[i][j] == '_' || islower(M[i][j]))
flr[i][j] = 0;
else if (M[i][j] == '^' || isupper(M[i][j]))
flr[i][j] = 1;
}
cin >> S;
rep(i, S) rep(j, H) rep(k, W) cin >> MS[i][j][k];
queue<tuple<int, int, int, int>> que;
memset(dist, -1, sizeof(dist));
dist[0][sy][sx][0] = 0;
que.emplace(0, sy, sx, 0);
while (que.size()) {
int f, y, x, bit;
tie(f, y, x, bit) = que.front();
que.pop();
if (y == gy && x == gx) {
cout << dist[f][y][x][bit] << endl;
return 0;
}
if (M[y][x] == '|' && dist[!f][y][x][bit] == -1) {
dist[!f][y][x][bit] = dist[f][y][x][bit] + 1;
que.emplace(!f, y, x, bit);
}
if (isalpha(M[y][x])) {
int g = flr[y][x];
rep(j, S) if (((bit >> j) & 1) && MS[j][y][x] == '*') g = !g;
if (f == g) {
int nf = f, ny = y, nx = x, nbit = bit;
int digit = M[ny][nx] - (islower(M[ny][nx]) ? 'a' : 'A');
nbit ^= 1 << digit;
if (MS[digit][ny][nx] == '*')
nf = !nf;
if (dist[nf][ny][nx][nbit] == -1) {
dist[nf][ny][nx][nbit] = dist[f][y][x][bit] + 1;
que.emplace(nf, ny, nx, nbit);
}
}
}
rep(i, 4) {
int nf = f, ny = y + dy[i], nx = x + dx[i], nbit = bit;
if (!in(ny, nx) || M[ny][nx] == '#' || ~dist[nf][ny][nx][nbit])
continue;
int g = flr[ny][nx];
rep(j, S) if (((nbit >> j) & 1) && MS[j][ny][nx] == '*') g = !g;
if (nf == g || M[ny][nx] == '|') {
dist[nf][ny][nx][nbit] = dist[f][y][x][bit] + 1;
que.emplace(nf, ny, nx, nbit);
}
}
}
cout << -1 << endl;
return 0;
}
|
replace
| 92 | 93 | 92 | 93 |
0
| |
p01561
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_H 55
#define MAX_W 55
#define MAX_S 15
struct State {
int d, x, y, s, f;
State(int d, int x, int y, int s, int f) : d(d), x(x), y(y), s(s), f(f) {}
};
char M[MAX_H][MAX_W];
bool visited[MAX_H][MAX_W][1 << MAX_S][2];
int getFloor(int x, int y, int f) {
if (M[y][x] == '|') {
return f;
}
if (M[y][x] == '_' || islower(M[y][x])) {
return 0;
}
return 1;
}
int main() {
int H, W, sx, sy, gx, gy, S;
char MS[MAX_H][MAX_W][MAX_S];
cin >> W >> H;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> M[i][j];
if (M[i][j] == '%') {
M[i][j] = '_';
sx = j;
sy = i;
}
if (M[i][j] == '&') {
M[i][j] = '_';
gx = j;
gy = i;
}
}
}
cin >> S;
for (int i = 0; i < S; i++) {
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
cin >> MS[j][k][i];
}
}
}
int ans = -1;
memset(visited, false, sizeof(visited));
visited[sy][sx][0][0] = true;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
queue<State> Q;
Q.push(State(0, sx, sy, 0, 0));
while (!Q.empty()) {
State s = Q.front();
Q.pop();
int x = s.x, y = s.y;
if (x == gx && y == gy) {
ans = s.d;
break;
}
if (islower(M[y][x])) {
int sw = M[y][x] - 'a';
int next = s.s ^ (1 << sw);
int nf = s.f;
if (MS[y][x][sw] == '*') {
nf = 1 - nf;
}
if (!visited[y][x][next][nf]) {
visited[y][x][next][nf] = true;
Q.push(State(s.d + 1, x, y, next, nf));
}
}
if (isupper(M[y][x])) {
int sw = M[y][x] - 'A';
int next = s.s ^ (1 << sw);
int nf = s.f;
if (MS[y][x][sw] == '*') {
nf = 1 - nf;
}
if (!visited[y][x][next][nf]) {
visited[y][x][next][nf] = true;
Q.push(State(s.d + 1, x, y, next, nf));
}
}
if (M[y][x] == '|') {
int next = 1 - s.f;
if (!visited[y][x][s.s][next]) {
visited[y][x][s.s][next] = true;
Q.push(State(s.d + 1, x, y, s.s, next));
}
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
int f = s.f;
if (0 > nx || W <= nx || 0 > ny || H <= ny) {
continue;
}
if (M[ny][nx] == '#') {
continue;
}
int nf = getFloor(nx, ny, f);
for (int j = 0; j < S; j++) {
if ((s.s >> j) & 1) {
if (MS[ny][nx][j] == '*') {
nf = 1 - nf;
}
}
}
if (f == nf && !visited[ny][nx][s.s][nf]) {
visited[ny][nx][s.s][nf] = true;
Q.push(State(s.d + 1, nx, ny, s.s, nf));
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_H 50
#define MAX_W 50
#define MAX_S 10
struct State {
int d, x, y, s, f;
State(int d, int x, int y, int s, int f) : d(d), x(x), y(y), s(s), f(f) {}
};
char M[MAX_H][MAX_W];
bool visited[MAX_H][MAX_W][1 << MAX_S][2];
int getFloor(int x, int y, int f) {
if (M[y][x] == '|') {
return f;
}
if (M[y][x] == '_' || islower(M[y][x])) {
return 0;
}
return 1;
}
int main() {
int H, W, sx, sy, gx, gy, S;
char MS[MAX_H][MAX_W][MAX_S];
cin >> W >> H;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> M[i][j];
if (M[i][j] == '%') {
M[i][j] = '_';
sx = j;
sy = i;
}
if (M[i][j] == '&') {
M[i][j] = '_';
gx = j;
gy = i;
}
}
}
cin >> S;
for (int i = 0; i < S; i++) {
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
cin >> MS[j][k][i];
}
}
}
int ans = -1;
memset(visited, false, sizeof(visited));
visited[sy][sx][0][0] = true;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
queue<State> Q;
Q.push(State(0, sx, sy, 0, 0));
while (!Q.empty()) {
State s = Q.front();
Q.pop();
int x = s.x, y = s.y;
if (x == gx && y == gy) {
ans = s.d;
break;
}
if (islower(M[y][x])) {
int sw = M[y][x] - 'a';
int next = s.s ^ (1 << sw);
int nf = s.f;
if (MS[y][x][sw] == '*') {
nf = 1 - nf;
}
if (!visited[y][x][next][nf]) {
visited[y][x][next][nf] = true;
Q.push(State(s.d + 1, x, y, next, nf));
}
}
if (isupper(M[y][x])) {
int sw = M[y][x] - 'A';
int next = s.s ^ (1 << sw);
int nf = s.f;
if (MS[y][x][sw] == '*') {
nf = 1 - nf;
}
if (!visited[y][x][next][nf]) {
visited[y][x][next][nf] = true;
Q.push(State(s.d + 1, x, y, next, nf));
}
}
if (M[y][x] == '|') {
int next = 1 - s.f;
if (!visited[y][x][s.s][next]) {
visited[y][x][s.s][next] = true;
Q.push(State(s.d + 1, x, y, s.s, next));
}
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
int f = s.f;
if (0 > nx || W <= nx || 0 > ny || H <= ny) {
continue;
}
if (M[ny][nx] == '#') {
continue;
}
int nf = getFloor(nx, ny, f);
for (int j = 0; j < S; j++) {
if ((s.s >> j) & 1) {
if (MS[ny][nx][j] == '*') {
nf = 1 - nf;
}
}
}
if (f == nf && !visited[ny][nx][s.s][nf]) {
visited[ny][nx][s.s][nf] = true;
Q.push(State(s.d + 1, nx, ny, s.s, nf));
}
}
}
cout << ans << endl;
return 0;
}
|
replace
| 4 | 7 | 4 | 7 |
MLE
| |
p01562
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-4, PI = acos(-1), INF = 1e9;
inline bool eq(double a, double b) { return abs(b - a) < EPS; }
struct Point {
double x, y;
Point(){};
Point(double x, double y) : x(x), y(y){};
Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); }
Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }
Point operator*(const double b) const { return Point(x * b, y * b); }
Point operator*(const Point &b) const {
return Point(x * b.x - y * b.y, x * b.y + y * b.x);
}
Point operator/(const double b) const { return Point(x / b, y / b); }
bool operator<(const Point &b) const { return x != b.x ? x < b.x : y < b.y; }
bool operator==(const Point &b) const { return eq(x, b.x) && eq(y, b.y); }
double norm() const { return x * x + y * y; }
double arg() { return atan2(x, y); }
double abs() const { return sqrt(norm()); }
Point rotate(double theta) {
return Point(cos(theta) * x - sin(theta) * y,
sin(theta) * x + cos(theta) * y);
}
Point rotate90() { return Point(-y, x); }
friend ostream &operator<<(ostream &os, Point &p) {
return os << "(" << p.x << "," << p.y << ")";
}
friend istream &operator>>(istream &is, Point &a) { return is >> a.x >> a.y; }
};
double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
int ccw(const Point &a, Point b, Point c) {
b = b - a, c = c - a;
if (cross(b, c) > EPS)
return +1;
if (cross(b, c) < -EPS)
return -1;
if (dot(b, c) < 0)
return +2;
if (b.norm() < c.norm())
return -2;
return 0;
}
struct Segment {
Point a, b;
Segment(){};
Segment(Point a, Point b) : a(a), b(b){};
friend ostream &operator<<(ostream &os, Segment &p) {
return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << ","
<< p.b.y << ")";
}
friend istream &operator>>(istream &is, Segment &a) {
return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y;
}
};
typedef vector<Point> Polygon;
double Area2(const Polygon &p) {
double A = 0;
for (int i = 0; i < p.size(); ++i) {
A += cross(p[i], p[(i + 1) % p.size()]);
}
return A;
}
bool Intersect(const Segment &s, const Point &p) {
return ccw(s.a, s.b, p) == 0;
}
bool Intersect(const Segment &s, const Segment &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
Point Crosspoint(const Segment &l, const Segment &m) {
double A = cross(l.b - l.a, m.b - m.a);
double B = cross(l.b - l.a, l.b - m.a);
if (abs(A) < EPS && abs(B) < EPS)
return m.a; // same line
return m.a + (m.b - m.a) * B / A;
}
double GetAngle(const Point &a, const Point &b) {
double tmp = min(1.0, max(-1.0, dot(a, b) / a.abs() / b.abs()));
double r = acos(tmp) * 180.0 / PI;
if (cross(a, b) < -EPS)
r = 360 - r;
return r;
}
bool merge_if_able(Segment &s1, Segment s2) {
if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS)
return false;
if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1)
return false;
if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2)
return false;
s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));
return true;
}
void merge_segments(vector<Segment> &segs) {
for (int i = 0; i < segs.size(); i++) {
if (segs[i].b < segs[i].a)
swap(segs[i].a, segs[i].b);
}
for (int i = 0; i < segs.size(); i++) {
for (int j = i + 1; j < segs.size(); j++) {
if (merge_if_able(segs[i], segs[j])) {
segs[j--] = segs.back(), segs.pop_back();
}
}
}
}
vector<vector<int>> SegmentArrangement(vector<Segment> &segs,
vector<Point> &ps) {
vector<vector<int>> g;
const int N = (int)segs.size();
for (int i = 0; i < N; i++) {
ps.emplace_back(segs[i].a);
ps.emplace_back(segs[i].b);
for (int j = i + 1; j < N; j++) {
const Point p1 = segs[i].b - segs[i].a;
const Point p2 = segs[j].b - segs[j].a;
if (cross(p1, p2) == 0)
continue;
if (Intersect(segs[i], segs[j])) {
ps.emplace_back(Crosspoint(segs[i], segs[j]));
}
}
}
sort(begin(ps), end(ps));
ps.erase(unique(begin(ps), end(ps)), end(ps));
const int M = (int)ps.size();
g.resize(M);
for (int i = 0; i < N; i++) {
vector<int> vec;
for (int j = 0; j < M; j++) {
if (Intersect(segs[i], ps[j])) {
vec.emplace_back(j);
}
}
for (int j = 1; j < vec.size(); j++) {
g[vec[j - 1]].push_back(vec[j]);
g[vec[j]].push_back(vec[j - 1]);
}
}
return (g);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<Segment> segs;
{
Point pv;
for (int i = 0; i < N; i++) {
Point curr;
cin >> curr;
if (i)
segs.emplace_back((Segment){pv, curr});
pv = curr;
}
}
vector<Point> ps;
merge_segments(segs);
auto g = SegmentArrangement(segs, ps);
Point s(INF, INF);
int t;
for (auto &p : ps)
s = min(s, p);
for (int i = 0; i < ps.size(); i++)
if (ps[i] == s)
t = i;
Polygon poly;
function<void(int, int)> dfs = [&](int idx, int par) {
if (idx == t)
return;
pair<double, int> nxt(INF, -1);
for (auto &to : g[idx]) {
double arg = GetAngle(ps[par] - ps[idx], ps[to] - ps[idx]);
if (to == par)
arg = 361;
nxt = min(nxt, make_pair(arg, to));
}
poly.emplace_back(ps[idx]);
dfs(nxt.second, idx);
};
double ret = 0;
for (auto &to : g[t]) {
poly.push_back(s);
dfs(to, t);
ret += max(0.0, Area2(poly));
poly.clear();
}
cout << fixed << setprecision(12) << ret * 0.5 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-5, PI = acos(-1), INF = 1e9;
inline bool eq(double a, double b) { return abs(b - a) < EPS; }
struct Point {
double x, y;
Point(){};
Point(double x, double y) : x(x), y(y){};
Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); }
Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }
Point operator*(const double b) const { return Point(x * b, y * b); }
Point operator*(const Point &b) const {
return Point(x * b.x - y * b.y, x * b.y + y * b.x);
}
Point operator/(const double b) const { return Point(x / b, y / b); }
bool operator<(const Point &b) const { return x != b.x ? x < b.x : y < b.y; }
bool operator==(const Point &b) const { return eq(x, b.x) && eq(y, b.y); }
double norm() const { return x * x + y * y; }
double arg() { return atan2(x, y); }
double abs() const { return sqrt(norm()); }
Point rotate(double theta) {
return Point(cos(theta) * x - sin(theta) * y,
sin(theta) * x + cos(theta) * y);
}
Point rotate90() { return Point(-y, x); }
friend ostream &operator<<(ostream &os, Point &p) {
return os << "(" << p.x << "," << p.y << ")";
}
friend istream &operator>>(istream &is, Point &a) { return is >> a.x >> a.y; }
};
double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
int ccw(const Point &a, Point b, Point c) {
b = b - a, c = c - a;
if (cross(b, c) > EPS)
return +1;
if (cross(b, c) < -EPS)
return -1;
if (dot(b, c) < 0)
return +2;
if (b.norm() < c.norm())
return -2;
return 0;
}
struct Segment {
Point a, b;
Segment(){};
Segment(Point a, Point b) : a(a), b(b){};
friend ostream &operator<<(ostream &os, Segment &p) {
return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << ","
<< p.b.y << ")";
}
friend istream &operator>>(istream &is, Segment &a) {
return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y;
}
};
typedef vector<Point> Polygon;
double Area2(const Polygon &p) {
double A = 0;
for (int i = 0; i < p.size(); ++i) {
A += cross(p[i], p[(i + 1) % p.size()]);
}
return A;
}
bool Intersect(const Segment &s, const Point &p) {
return ccw(s.a, s.b, p) == 0;
}
bool Intersect(const Segment &s, const Segment &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
Point Crosspoint(const Segment &l, const Segment &m) {
double A = cross(l.b - l.a, m.b - m.a);
double B = cross(l.b - l.a, l.b - m.a);
if (abs(A) < EPS && abs(B) < EPS)
return m.a; // same line
return m.a + (m.b - m.a) * B / A;
}
double GetAngle(const Point &a, const Point &b) {
double tmp = min(1.0, max(-1.0, dot(a, b) / a.abs() / b.abs()));
double r = acos(tmp) * 180.0 / PI;
if (cross(a, b) < -EPS)
r = 360 - r;
return r;
}
bool merge_if_able(Segment &s1, Segment s2) {
if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS)
return false;
if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1)
return false;
if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2)
return false;
s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));
return true;
}
void merge_segments(vector<Segment> &segs) {
for (int i = 0; i < segs.size(); i++) {
if (segs[i].b < segs[i].a)
swap(segs[i].a, segs[i].b);
}
for (int i = 0; i < segs.size(); i++) {
for (int j = i + 1; j < segs.size(); j++) {
if (merge_if_able(segs[i], segs[j])) {
segs[j--] = segs.back(), segs.pop_back();
}
}
}
}
vector<vector<int>> SegmentArrangement(vector<Segment> &segs,
vector<Point> &ps) {
vector<vector<int>> g;
const int N = (int)segs.size();
for (int i = 0; i < N; i++) {
ps.emplace_back(segs[i].a);
ps.emplace_back(segs[i].b);
for (int j = i + 1; j < N; j++) {
const Point p1 = segs[i].b - segs[i].a;
const Point p2 = segs[j].b - segs[j].a;
if (cross(p1, p2) == 0)
continue;
if (Intersect(segs[i], segs[j])) {
ps.emplace_back(Crosspoint(segs[i], segs[j]));
}
}
}
sort(begin(ps), end(ps));
ps.erase(unique(begin(ps), end(ps)), end(ps));
const int M = (int)ps.size();
g.resize(M);
for (int i = 0; i < N; i++) {
vector<int> vec;
for (int j = 0; j < M; j++) {
if (Intersect(segs[i], ps[j])) {
vec.emplace_back(j);
}
}
for (int j = 1; j < vec.size(); j++) {
g[vec[j - 1]].push_back(vec[j]);
g[vec[j]].push_back(vec[j - 1]);
}
}
return (g);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<Segment> segs;
{
Point pv;
for (int i = 0; i < N; i++) {
Point curr;
cin >> curr;
if (i)
segs.emplace_back((Segment){pv, curr});
pv = curr;
}
}
vector<Point> ps;
merge_segments(segs);
auto g = SegmentArrangement(segs, ps);
Point s(INF, INF);
int t;
for (auto &p : ps)
s = min(s, p);
for (int i = 0; i < ps.size(); i++)
if (ps[i] == s)
t = i;
Polygon poly;
function<void(int, int)> dfs = [&](int idx, int par) {
if (idx == t)
return;
pair<double, int> nxt(INF, -1);
for (auto &to : g[idx]) {
double arg = GetAngle(ps[par] - ps[idx], ps[to] - ps[idx]);
if (to == par)
arg = 361;
nxt = min(nxt, make_pair(arg, to));
}
poly.emplace_back(ps[idx]);
dfs(nxt.second, idx);
};
double ret = 0;
for (auto &to : g[t]) {
poly.push_back(s);
dfs(to, t);
ret += max(0.0, Area2(poly));
poly.clear();
}
cout << fixed << setprecision(12) << ret * 0.5 << endl;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p01564
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <array>
#include <climits>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;
template <typename Monoid, typename Operand> class link_cut_tree {
struct node_t {
node_t *left, *right, *per;
Monoid value, sum;
Operand lazy;
std::uint_least8_t b;
// isnotroot reverse haslazy
node_t()
: left(nil), right(nil), per(nullptr), value(Monoid()), sum(Monoid()),
b(0) {}
inline bool isroot() { return !(b & 4); }
Monoid reflect() {
if (b & 1) {
if (b & 2)
return ~(sum * lazy);
return sum * lazy;
}
if (b & 2)
return ~sum;
return sum;
}
void assign(Operand &data) {
if (b & 1)
lazy = lazy * data;
else {
lazy = data;
b |= 1;
}
}
void push() {
if (b & 2) {
std::swap(left, right);
left->b ^= 2;
right->b ^= 2;
value = ~value;
}
if (b & 1) {
value = value * lazy;
left->assign(lazy);
right->assign(lazy);
}
b &= ~3;
}
void propagate() {
if (per)
per->propagate();
push();
}
void splay() {
node_t *x = this, *pp;
while (!x->isroot()) {
if (per->isroot()) {
if (per->left == this) {
per->left = right;
per->recalc();
right = per;
} else {
per->right = left;
per->recalc();
left = per;
}
x = per;
per = per->per;
recalc();
break;
}
x = per->per;
pp = x->per;
if (per->left == this) {
if (x->left == per) {
x->left = per->right;
per->left = right;
per->right = x;
right = per;
} else {
x->right = left;
per->left = right;
right = per;
left = x;
}
} else {
if (x->right == per) {
x->right = per->left;
per->right = left;
per->left = x;
left = per;
} else {
x->left = right;
per->right = left;
left = per;
right = x;
}
}
x->recalc();
per->recalc();
recalc();
per = pp;
if (pp) {
if (pp->left == x) {
pp->left = this;
} else if (pp->right == x) {
pp->right = this;
}
}
}
x->b |= 4;
}
void expose(node_t *prev) {
splay();
right->b &= ~4;
right = prev;
recalc();
if (per)
per->expose(this);
else {
b &= ~4;
}
}
void recalc() {
sum = value;
if (left != nil) {
sum = left->reflect() + sum;
left->per = this;
}
if (right != nil) {
sum = sum + right->reflect();
right->per = this;
}
}
};
static node_t *nil;
std::vector<node_t> tree;
void expose(node_t *n) {
n->propagate();
scan();
n->expose(nil);
scan();
n->splay();
n->b &= ~4;
scan();
n->recalc();
}
struct vis {
int32 l, r, p, rev;
};
std::vector<vis> v;
public:
link_cut_tree(uint32 size) : tree(size) {}
link_cut_tree(std::vector<Monoid> &a) : tree(a.size()) {
for (uint32 i = 0; i < a.size(); ++i) {
tree[i].value = tree[i].sum = a[i];
}
}
void link(uint32 child, uint32 per) {
evert(child);
tree[child].per = &tree[per];
}
void cut(uint32 child) {
node_t *n = &tree[child];
expose(n);
n->left->per = nullptr;
n->left = nil;
n->sum = n->value;
}
void update(uint32 u, uint32 v, const Operand &data) {
evert(u);
expose(&tree[v]);
tree[v].lazy = data;
tree[v].b |= 1;
}
Monoid path(uint32 u, uint32 v) {
evert(u);
expose(&tree[v]);
scan();
return tree[v].sum;
}
void evert(uint32 v) {
scan();
expose(&tree[v]);
scan();
tree[v].b ^= 2;
}
int32 ch(node_t *n) {
if (!n)
return 100;
if (n == nil)
return -1;
return n - &tree[0];
}
void scan(void) {
v = std::vector<vis>(tree.size());
for (uint32 i = 0; i < tree.size(); ++i) {
v[i] = {ch(tree[i].left), ch(tree[i].right), ch(tree[i].per),
tree[i].b & 2};
}
}
};
template <typename Monoid, typename Operand>
typename link_cut_tree<Monoid, Operand>::node_t
*link_cut_tree<Monoid, Operand>::nil = [] {
nil = new link_cut_tree<Monoid, Operand>::node_t;
nil->left = nil;
nil->right = nil;
nil->per = nullptr;
nil->sum = Monoid();
nil->b = 0;
return nil;
}();
struct ass {
intl32 data;
ass(intl32 a = 0) : data(a) {}
ass operator*(const ass &other) { return other; }
};
struct douse {
intl32 all, r, l, sum, siz;
bool id;
douse() : id(1) {}
douse(int32 a, int32 lef, int32 rig, int32 s, int32 si)
: all(a), l(lef), r(rig), sum(s), siz(si), id(0) {}
douse operator~() {
douse ret = *this;
std::swap(ret.l, ret.r);
return ret;
}
douse operator+(const douse &other) {
if (id)
return other;
if (other.id)
return *this;
douse ret;
ret.all = std::max(std::max(all, other.all),
std::max(r + other.l, std::max(r, other.l)));
ret.r = std::max(other.r, r > 0 ? r + other.sum : other.sum);
ret.l = std::max(l, other.l > 0 ? sum + other.l : sum);
ret.sum = sum + other.sum;
ret.siz = siz + other.siz;
ret.id = 0;
return ret;
}
douse operator*(const ass &other) {
douse ret;
ret.all = ret.r = ret.l = other.data > 0 ? other.data * siz : other.data;
ret.sum = other.data * siz;
ret.siz = siz;
ret.id = id;
return ret;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
std::vector<douse> a(n);
int32 t1;
for (uint32 i = 0; i < n; ++i) {
std::cin >> t1;
a[i] = douse(t1, t1, t1, t1, 1);
}
link_cut_tree<douse, ass> L(a);
uint32 t2, t3;
for (uint32 i = 0; i < n - 1; ++i) {
std::cin >> t2 >> t3;
L.link(t2 - 1, t3 - 1);
L.scan();
}
uint32 t4;
for (uint32 i = 0; i < q; ++i) {
L.scan();
std::cin >> t2 >> t3 >> t4 >> t1;
if (t2 == 1) {
L.update(t3 - 1, t4 - 1, ass(t1));
} else {
std::cout << L.path(t3 - 1, t4 - 1).all << "\n";
}
}
return 0;
}
|
#include <algorithm>
#include <array>
#include <climits>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;
template <typename Monoid, typename Operand> class link_cut_tree {
struct node_t {
node_t *left, *right, *per;
Monoid value, sum;
Operand lazy;
std::uint_least8_t b;
// isnotroot reverse haslazy
node_t()
: left(nil), right(nil), per(nullptr), value(Monoid()), sum(Monoid()),
b(0) {}
inline bool isroot() { return !(b & 4); }
Monoid reflect() {
if (b & 1) {
if (b & 2)
return ~(sum * lazy);
return sum * lazy;
}
if (b & 2)
return ~sum;
return sum;
}
void assign(Operand &data) {
if (b & 1)
lazy = lazy * data;
else {
lazy = data;
b |= 1;
}
}
void push() {
if (b & 2) {
std::swap(left, right);
left->b ^= 2;
right->b ^= 2;
value = ~value;
}
if (b & 1) {
value = value * lazy;
left->assign(lazy);
right->assign(lazy);
}
b &= ~3;
}
void propagate() {
if (per)
per->propagate();
push();
}
void splay() {
node_t *x = this, *pp;
while (!x->isroot()) {
if (per->isroot()) {
if (per->left == this) {
per->left = right;
per->recalc();
right = per;
} else {
per->right = left;
per->recalc();
left = per;
}
x = per;
per = per->per;
recalc();
break;
}
x = per->per;
pp = x->per;
if (per->left == this) {
if (x->left == per) {
x->left = per->right;
per->left = right;
per->right = x;
right = per;
} else {
x->right = left;
per->left = right;
right = per;
left = x;
}
} else {
if (x->right == per) {
x->right = per->left;
per->right = left;
per->left = x;
left = per;
} else {
x->left = right;
per->right = left;
left = per;
right = x;
}
}
x->recalc();
per->recalc();
recalc();
per = pp;
if (pp) {
if (pp->left == x) {
pp->left = this;
} else if (pp->right == x) {
pp->right = this;
}
}
}
x->b |= 4;
}
void expose(node_t *prev) {
splay();
right->b &= ~4;
right = prev;
recalc();
if (per)
per->expose(this);
else {
b &= ~4;
}
}
void recalc() {
sum = value;
if (left != nil) {
sum = left->reflect() + sum;
left->per = this;
}
if (right != nil) {
sum = sum + right->reflect();
right->per = this;
}
}
};
static node_t *nil;
std::vector<node_t> tree;
void expose(node_t *n) {
n->propagate();
scan();
n->expose(nil);
scan();
n->splay();
n->b &= ~4;
scan();
n->recalc();
}
struct vis {
int32 l, r, p, rev;
};
std::vector<vis> v;
public:
link_cut_tree(uint32 size) : tree(size) {}
link_cut_tree(std::vector<Monoid> &a) : tree(a.size()) {
for (uint32 i = 0; i < a.size(); ++i) {
tree[i].value = tree[i].sum = a[i];
}
}
void link(uint32 child, uint32 per) {
evert(child);
tree[child].per = &tree[per];
}
void cut(uint32 child) {
node_t *n = &tree[child];
expose(n);
n->left->per = nullptr;
n->left = nil;
n->sum = n->value;
}
void update(uint32 u, uint32 v, const Operand &data) {
evert(u);
expose(&tree[v]);
tree[v].lazy = data;
tree[v].b |= 1;
}
Monoid path(uint32 u, uint32 v) {
evert(u);
expose(&tree[v]);
scan();
return tree[v].sum;
}
void evert(uint32 v) {
scan();
expose(&tree[v]);
scan();
tree[v].b ^= 2;
}
int32 ch(node_t *n) {
if (!n)
return 100;
if (n == nil)
return -1;
return n - &tree[0];
}
void scan(void) {
return;
v = std::vector<vis>(tree.size());
for (uint32 i = 0; i < tree.size(); ++i) {
v[i] = {ch(tree[i].left), ch(tree[i].right), ch(tree[i].per),
tree[i].b & 2};
}
}
};
template <typename Monoid, typename Operand>
typename link_cut_tree<Monoid, Operand>::node_t
*link_cut_tree<Monoid, Operand>::nil = [] {
nil = new link_cut_tree<Monoid, Operand>::node_t;
nil->left = nil;
nil->right = nil;
nil->per = nullptr;
nil->sum = Monoid();
nil->b = 0;
return nil;
}();
struct ass {
intl32 data;
ass(intl32 a = 0) : data(a) {}
ass operator*(const ass &other) { return other; }
};
struct douse {
intl32 all, r, l, sum, siz;
bool id;
douse() : id(1) {}
douse(int32 a, int32 lef, int32 rig, int32 s, int32 si)
: all(a), l(lef), r(rig), sum(s), siz(si), id(0) {}
douse operator~() {
douse ret = *this;
std::swap(ret.l, ret.r);
return ret;
}
douse operator+(const douse &other) {
if (id)
return other;
if (other.id)
return *this;
douse ret;
ret.all = std::max(std::max(all, other.all),
std::max(r + other.l, std::max(r, other.l)));
ret.r = std::max(other.r, r > 0 ? r + other.sum : other.sum);
ret.l = std::max(l, other.l > 0 ? sum + other.l : sum);
ret.sum = sum + other.sum;
ret.siz = siz + other.siz;
ret.id = 0;
return ret;
}
douse operator*(const ass &other) {
douse ret;
ret.all = ret.r = ret.l = other.data > 0 ? other.data * siz : other.data;
ret.sum = other.data * siz;
ret.siz = siz;
ret.id = id;
return ret;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
std::vector<douse> a(n);
int32 t1;
for (uint32 i = 0; i < n; ++i) {
std::cin >> t1;
a[i] = douse(t1, t1, t1, t1, 1);
}
link_cut_tree<douse, ass> L(a);
uint32 t2, t3;
for (uint32 i = 0; i < n - 1; ++i) {
std::cin >> t2 >> t3;
L.link(t2 - 1, t3 - 1);
L.scan();
}
uint32 t4;
for (uint32 i = 0; i < q; ++i) {
L.scan();
std::cin >> t2 >> t3 >> t4 >> t1;
if (t2 == 1) {
L.update(t3 - 1, t4 - 1, ass(t1));
} else {
std::cout << L.path(t3 - 1, t4 - 1).all << "\n";
}
}
return 0;
}
|
insert
| 218 | 218 | 218 | 219 |
TLE
| |
p01564
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <array>
#include <climits>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;
template <typename Monoid, typename Operand> class link_cut_tree {
struct node_t {
node_t *left, *right, *per;
Monoid value, sum;
Operand lazy;
std::uint_least8_t b;
// isnotroot reverse haslazy
node_t()
: left(nil), right(nil), per(nullptr), value(Monoid()), sum(Monoid()),
b(0) {}
Monoid reflect() {
if (b & 1) {
if (b & 2)
return ~(sum * lazy);
return sum * lazy;
}
if (b & 2)
return ~sum;
return sum;
}
void assign(Operand &data) {
if (b & 1)
lazy = lazy * data;
else {
lazy = data;
b |= 1;
}
}
void push() {
if (b & 2) {
std::swap(left, right);
left->b ^= 2;
right->b ^= 2;
value = ~value;
}
if (b & 1) {
value = value * lazy;
left->assign(lazy);
right->assign(lazy);
}
b &= ~3;
}
void propagate() {
if (per)
per->propagate();
push();
}
void splay() {
node_t *x = this, *pp;
while (x->b & 4) {
if (!(per->b & 4)) {
if (per->left == this) {
per->left = right;
per->recalc();
right = per;
} else {
per->right = left;
per->recalc();
left = per;
}
x = per;
per = per->per;
recalc();
break;
}
x = per->per;
pp = x->per;
if (per->left == this) {
if (x->left == per) {
x->left = per->right;
per->left = right;
per->right = x;
right = per;
} else {
x->right = left;
per->left = right;
right = per;
left = x;
}
} else {
if (x->right == per) {
x->right = per->left;
per->right = left;
per->left = x;
left = per;
} else {
x->left = right;
per->right = left;
left = per;
right = x;
}
}
x->recalc();
per->recalc();
recalc();
per = pp;
if (pp) {
if (pp->left == x) {
pp->left = this;
} else if (pp->right == x) {
pp->right = this;
}
}
}
x->b |= 4;
}
void expose(node_t *prev) {
splay();
right->b &= ~4;
right = prev;
recalc();
if (per)
per->expose(this);
else {
b &= ~4;
}
}
void recalc() {
sum = value;
if (left != nil) {
sum = left->reflect() + sum;
left->per = this;
}
if (right != nil) {
sum = sum + right->reflect();
right->per = this;
}
}
};
static node_t *nil;
std::vector<node_t> tree;
void expose(node_t *n) {
n->propagate();
n->expose(nil);
n->splay();
n->b &= ~4;
n->recalc();
}
struct vis {
int32 l, r, p, rev;
};
std::vector<vis> v;
public:
link_cut_tree(uint32 size) : tree(size) {}
link_cut_tree(std::vector<Monoid> &a) : tree(a.size()) {
for (uint32 i = 0; i < a.size(); ++i) {
tree[i].value = tree[i].sum = a[i];
}
}
void link(uint32 child, uint32 per) {
evert(child);
tree[child].per = &tree[per];
}
void cut(uint32 child) {
node_t *n = &tree[child];
expose(n);
n->left->per = nullptr;
n->left = nil;
n->sum = n->value;
}
void update(uint32 u, uint32 v, const Operand &data) {
evert(u);
expose(&tree[v]);
tree[v].lazy = data;
tree[v].b |= 1;
}
Monoid path(uint32 u, uint32 v) {
evert(u);
expose(&tree[v]);
return tree[v].sum;
}
void evert(uint32 v) {
expose(&tree[v]);
tree[v].b ^= 2;
}
int32 ch(node_t *n) {
if (!n)
return 100;
if (n == nil)
return -1;
return n - &tree[0];
}
void scan(void) {
v = std::vector<vis>(tree.size());
for (uint32 i = 0; i < tree.size(); ++i) {
v[i] = {ch(tree[i].left), ch(tree[i].right), ch(tree[i].per),
tree[i].b & 2};
}
}
};
template <typename Monoid, typename Operand>
typename link_cut_tree<Monoid, Operand>::node_t
*link_cut_tree<Monoid, Operand>::nil = [] {
nil = new link_cut_tree<Monoid, Operand>::node_t;
nil->left = nil;
nil->right = nil;
nil->per = nullptr;
nil->sum = Monoid();
nil->b = 0;
return nil;
}();
struct ass {
intl32 data;
ass(intl32 a = 0) : data(a) {}
ass operator*(const ass &other) { return other; }
};
struct douse {
intl32 all, l, r, sum, siz;
bool id;
douse() : id(1) {}
douse(int32 a, int32 lef, int32 rig, int32 s, int32 si)
: all(a), l(lef), r(rig), sum(s), siz(si), id(0) {}
douse operator~() { return douse(all, r, l, sum, siz); }
douse operator+(const douse &other) {
if (id)
return other;
if (other.id)
return *this;
douse ret;
ret.all = std::max(std::max(all, other.all),
std::max(r + other.l, std::max(r, other.l)));
ret.r = std::max(other.r, r > 0 ? r + other.sum : other.sum);
ret.l = std::max(l, other.l > 0 ? sum + other.l : sum);
ret.sum = sum + other.sum;
ret.siz = siz + other.siz;
ret.id = 0;
return ret;
}
douse operator*(const ass &other) {
douse ret;
ret.all = ret.r = ret.l = other.data > 0 ? other.data * siz : other.data;
ret.sum = other.data * siz;
ret.siz = siz;
ret.id = id;
return ret;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
std::vector<douse> a(n);
int32 t1;
for (uint32 i = 0; i < n; ++i) {
std::cin >> t1;
a[i] = douse(t1, t1, t1, t1, 1);
}
link_cut_tree<douse, ass> L(a);
uint32 t2, t3;
for (uint32 i = 0; i < n - 1; ++i) {
std::cin >> t2 >> t3;
L.link(t2 - 1, t3 - 1);
}
uint32 t4;
for (uint32 i = 0; i < q; ++i) {
L.scan();
std::cin >> t2 >> t3 >> t4 >> t1;
if (t2 == 1) {
L.update(t3 - 1, t4 - 1, ass(t1));
} else {
std::cout << L.path(t3 - 1, t4 - 1).all << "\n";
}
}
return 0;
}
|
#include <algorithm>
#include <array>
#include <climits>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;
template <typename Monoid, typename Operand> class link_cut_tree {
struct node_t {
node_t *left, *right, *per;
Monoid value, sum;
Operand lazy;
std::uint_least8_t b;
// isnotroot reverse haslazy
node_t()
: left(nil), right(nil), per(nullptr), value(Monoid()), sum(Monoid()),
b(0) {}
Monoid reflect() {
if (b & 1) {
if (b & 2)
return ~(sum * lazy);
return sum * lazy;
}
if (b & 2)
return ~sum;
return sum;
}
void assign(Operand &data) {
if (b & 1)
lazy = lazy * data;
else {
lazy = data;
b |= 1;
}
}
void push() {
if (b & 2) {
std::swap(left, right);
left->b ^= 2;
right->b ^= 2;
value = ~value;
}
if (b & 1) {
value = value * lazy;
left->assign(lazy);
right->assign(lazy);
}
b &= ~3;
}
void propagate() {
if (per)
per->propagate();
push();
}
void splay() {
node_t *x = this, *pp;
while (x->b & 4) {
if (!(per->b & 4)) {
if (per->left == this) {
per->left = right;
per->recalc();
right = per;
} else {
per->right = left;
per->recalc();
left = per;
}
x = per;
per = per->per;
recalc();
break;
}
x = per->per;
pp = x->per;
if (per->left == this) {
if (x->left == per) {
x->left = per->right;
per->left = right;
per->right = x;
right = per;
} else {
x->right = left;
per->left = right;
right = per;
left = x;
}
} else {
if (x->right == per) {
x->right = per->left;
per->right = left;
per->left = x;
left = per;
} else {
x->left = right;
per->right = left;
left = per;
right = x;
}
}
x->recalc();
per->recalc();
recalc();
per = pp;
if (pp) {
if (pp->left == x) {
pp->left = this;
} else if (pp->right == x) {
pp->right = this;
}
}
}
x->b |= 4;
}
void expose(node_t *prev) {
splay();
right->b &= ~4;
right = prev;
recalc();
if (per)
per->expose(this);
else {
b &= ~4;
}
}
void recalc() {
sum = value;
if (left != nil) {
sum = left->reflect() + sum;
left->per = this;
}
if (right != nil) {
sum = sum + right->reflect();
right->per = this;
}
}
};
static node_t *nil;
std::vector<node_t> tree;
void expose(node_t *n) {
n->propagate();
n->expose(nil);
n->splay();
n->b &= ~4;
n->recalc();
}
struct vis {
int32 l, r, p, rev;
};
std::vector<vis> v;
public:
link_cut_tree(uint32 size) : tree(size) {}
link_cut_tree(std::vector<Monoid> &a) : tree(a.size()) {
for (uint32 i = 0; i < a.size(); ++i) {
tree[i].value = tree[i].sum = a[i];
}
}
void link(uint32 child, uint32 per) {
evert(child);
tree[child].per = &tree[per];
}
void cut(uint32 child) {
node_t *n = &tree[child];
expose(n);
n->left->per = nullptr;
n->left = nil;
n->sum = n->value;
}
void update(uint32 u, uint32 v, const Operand &data) {
evert(u);
expose(&tree[v]);
tree[v].lazy = data;
tree[v].b |= 1;
}
Monoid path(uint32 u, uint32 v) {
evert(u);
expose(&tree[v]);
return tree[v].sum;
}
void evert(uint32 v) {
expose(&tree[v]);
tree[v].b ^= 2;
}
int32 ch(node_t *n) {
if (!n)
return 100;
if (n == nil)
return -1;
return n - &tree[0];
}
void scan(void) {
v = std::vector<vis>(tree.size());
for (uint32 i = 0; i < tree.size(); ++i) {
v[i] = {ch(tree[i].left), ch(tree[i].right), ch(tree[i].per),
tree[i].b & 2};
}
}
};
template <typename Monoid, typename Operand>
typename link_cut_tree<Monoid, Operand>::node_t
*link_cut_tree<Monoid, Operand>::nil = [] {
nil = new link_cut_tree<Monoid, Operand>::node_t;
nil->left = nil;
nil->right = nil;
nil->per = nullptr;
nil->sum = Monoid();
nil->b = 0;
return nil;
}();
struct ass {
intl32 data;
ass(intl32 a = 0) : data(a) {}
ass operator*(const ass &other) { return other; }
};
struct douse {
intl32 all, l, r, sum, siz;
bool id;
douse() : id(1) {}
douse(int32 a, int32 lef, int32 rig, int32 s, int32 si)
: all(a), l(lef), r(rig), sum(s), siz(si), id(0) {}
douse operator~() { return douse(all, r, l, sum, siz); }
douse operator+(const douse &other) {
if (id)
return other;
if (other.id)
return *this;
douse ret;
ret.all = std::max(std::max(all, other.all),
std::max(r + other.l, std::max(r, other.l)));
ret.r = std::max(other.r, r > 0 ? r + other.sum : other.sum);
ret.l = std::max(l, other.l > 0 ? sum + other.l : sum);
ret.sum = sum + other.sum;
ret.siz = siz + other.siz;
ret.id = 0;
return ret;
}
douse operator*(const ass &other) {
douse ret;
ret.all = ret.r = ret.l = other.data > 0 ? other.data * siz : other.data;
ret.sum = other.data * siz;
ret.siz = siz;
ret.id = id;
return ret;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
std::vector<douse> a(n);
int32 t1;
for (uint32 i = 0; i < n; ++i) {
std::cin >> t1;
a[i] = douse(t1, t1, t1, t1, 1);
}
link_cut_tree<douse, ass> L(a);
uint32 t2, t3;
for (uint32 i = 0; i < n - 1; ++i) {
std::cin >> t2 >> t3;
L.link(t2 - 1, t3 - 1);
}
uint32 t4;
for (uint32 i = 0; i < q; ++i) {
// L.scan();
std::cin >> t2 >> t3 >> t4 >> t1;
if (t2 == 1) {
L.update(t3 - 1, t4 - 1, ass(t1));
} else {
std::cout << L.path(t3 - 1, t4 - 1).all << "\n";
}
}
return 0;
}
|
replace
| 286 | 287 | 286 | 287 |
TLE
| |
p01564
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9 + 100000;
const double INF = 1e12, EPS = 1e-9;
template <class T> struct SegTree {
T *ans, *sum, *left, *right, *lazy;
int n;
SegTree(int size = 1000000) {
for (n = 1; n < size; n *= 2)
;
ans = new T[2 * n - 1];
sum = new T[2 * n - 1];
left = new T[2 * n - 1];
right = new T[2 * n - 1];
lazy = new T[2 * n - 1];
rep(i, 2 * n - 1) sum[i] = ans[i] = right[i] = left[i] = 0, lazy[i] = inf;
}
~SegTree() {
delete ans;
delete sum;
delete left;
delete right;
delete lazy;
}
inline void fix(int i) {
int l = i * 2 + 1, r = i * 2 + 2;
ans[i] = max(max(ans[l], ans[r]), left[r] + right[l]);
sum[i] = sum[l] + sum[r];
left[i] = max(sum[l] + left[r], left[l]);
right[i] = max(sum[r] + right[l], right[r]);
}
inline void lazyfix(int k, int l, int r) {
if (lazy[k] == inf)
return;
update(l, (l + r) / 2, lazy[k], k * 2 + 1, l, (l + r) / 2);
update((l + r) / 2, r, lazy[k], k * 2 + 2, (l + r) / 2, r);
lazy[k] = inf;
}
void init() {
for (int i = n - 2; i >= 0; i--)
fix(i);
}
inline void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }
inline void update(int a, int b, T x, int k, int l, int r) {
if (b <= l || a >= r)
return;
if (a <= l && r <= b) {
lazy[k] = x;
left[k] = right[k] = ans[k] = x >= 0 ? (r - l) * x : x;
sum[k] = (r - l) * x;
return;
}
lazyfix(k, l, r);
int lch = k * 2 + 1, rch = k * 2 + 2;
update(a, b, x, lch, l, (l + r) / 2);
update(a, b, x, rch, (l + r) / 2, r);
fix(k);
}
#define LEFT a, b, k * 2 + 1, l, (l + r) / 2
#define RIGHT a, b, k * 2 + 2, (l + r) / 2, r
inline T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return ans[k];
lazyfix(k, l, r);
int vl = query(LEFT);
int vr = query(RIGHT);
int vll = queryRight(LEFT);
int vrr = queryLeft(RIGHT);
return max(max(vl, vr), vll + vrr);
}
inline T querySum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return sum[k];
lazyfix(k, l, r);
int vl = querySum(LEFT);
int vr = querySum(RIGHT);
return vl + vr;
}
inline T queryRight(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return right[k];
lazyfix(k, l, r);
int R = queryRight(RIGHT);
int rsum = querySum(RIGHT);
int lr = queryRight(LEFT);
return max(R, rsum + lr);
}
inline T queryLeft(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return left[k];
lazyfix(k, l, r);
int L = queryLeft(LEFT);
int lsum = querySum(LEFT);
int rl = queryLeft(RIGHT);
return max(L, lsum + rl);
}
#undef LEFT
#undef RIGHT
};
const int MX = 200000;
const int MX_L = 18;
SegTree<int> *st[1000];
int st_root[1000];
int st_sz;
int n, q;
int w[MX];
int sz[MX];
int parent[MX_L][MX];
int depth[MX];
int which[MX];
vector<vi> e;
void size_rec(int, int);
void heavy_light_rec(int, int, int);
void segtest();
void fix(int a, int b, int c) {
while (1) {
if (which[a] < 0) {
w[a] = c;
if (a == b)
break;
a = parent[0][a];
} else {
int v = st_root[which[a]];
int base = depth[v];
if (depth[v] <= depth[b])
v = b;
int da = depth[a] - base;
int dv = depth[v] - base;
st[which[a]]->update(dv, da + 1, c);
if (v == b)
break;
a = parent[0][v];
}
}
}
pi calc(int a, int b) {
int res = -inf, resleft = -inf;
while (1) {
if (which[a] < 0) {
res = max(res, max(w[a], resleft > -inf ? w[a] + resleft : -inf));
resleft = max(w[a], resleft > -inf ? w[a] + resleft : -inf);
if (a == b)
break;
a = parent[0][a];
} else {
int v = st_root[which[a]];
int base = depth[v];
if (depth[v] <= depth[b])
v = b;
int da = depth[a] - base;
int dv = depth[v] - base;
int m = st[which[a]]->n;
int Q = st[which[a]]->query(dv, da + 1, 0, 0, m);
int L = st[which[a]]->queryLeft(dv, da + 1, 0, 0, m);
int R = st[which[a]]->queryRight(dv, da + 1, 0, 0, m);
int S = st[which[a]]->querySum(dv, da + 1, 0, 0, m);
res = max(res, max(Q, resleft > -inf ? R + resleft : -inf));
resleft = max(resleft > -inf ? S + resleft : -inf, L);
if (v == b)
break;
a = parent[0][v];
}
}
return mp(res, resleft);
}
void run() {
scanf("%d%d", &n, &q);
rep(i, n) scanf("%d", w + i);
e.resize(n);
rep(i, n - 1) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
e[a].pb(b);
e[b].pb(a);
}
memset(which, -1, sizeof(which));
size_rec(0, 0);
heavy_light_rec(0, 0, -1);
rep(i, MX_L - 1) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];
while (q--) {
int t, aa, bb, c, v;
scanf("%d%d%d%d", &t, &aa, &bb, &c);
aa--;
bb--;
if (depth[aa] > depth[bb])
swap(aa, bb);
int a = aa, b = bb;
int d = depth[b] - depth[a];
rep(i, MX_L) if (d & 1 << i) b = parent[i][b];
if (a == b)
v = a;
else {
for (int i = MX_L - 1; i >= 0; i--)
if (parent[i][a] != parent[i][b]) {
a = parent[i][a];
b = parent[i][b];
}
v = parent[0][a];
}
if (t == 1) {
fix(aa, v, c);
fix(bb, v, c);
} else {
if (v == aa) {
printf("%d\n", calc(bb, aa).first);
} else {
pi x = calc(aa, a), y = calc(bb, v);
int ans = max(max(x.first, y.first), x.second + y.second);
printf("%d\n", ans);
}
}
}
}
void size_rec(int c, int p) {
sz[c] = 1;
depth[c] = c == p ? 0 : depth[p] + 1;
parent[0][c] = p;
rep(i, e[c].size()) if (e[c][i] != p) {
size_rec(e[c][i], c);
sz[c] += sz[e[c][i]];
}
}
void heavy_light_rec(int c, int p, int root) {
bool found = 0;
rep(i, e[c].size()) {
int to = e[c][i];
if (to == p)
continue;
if (2 * sz[to] >= sz[c]) {
heavy_light_rec(to, c, root < 0 ? c : root);
found = 1;
} else
heavy_light_rec(to, c, -1);
}
if (!found && root >= 0) {
int size = depth[c] - depth[root] + 1;
SegTree<int> *t = st[st_sz] = new SegTree<int>(size);
st_root[st_sz] = root;
for (int v = c, k = size - 1 + t->n - 1;; v = parent[0][v], k--) {
which[v] = st_sz;
t->left[k] = t->right[k] = t->ans[k] = w[v];
t->sum[k] = w[v];
if (v == root)
break;
}
t->init();
st_sz++;
}
}
long long esp_org, esp_new;
int main() {
const int size = 128 * 1024 * 1024;
void *p = malloc(size);
esp_new = (long long)p + size - 1;
__asm__("mov %%rsp, %0" : "=r"(esp_org) : :);
__asm__("mov %0, %%rsp" : : "r"(esp_new) :);
run();
__asm__("mov %0, %%rsp" : : "r"(esp_org) :);
free(p);
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9 + 100000;
const double INF = 1e12, EPS = 1e-9;
template <class T> struct SegTree {
T *ans, *sum, *left, *right, *lazy;
int n;
SegTree(int size = 1000000) {
for (n = 1; n < size; n *= 2)
;
ans = new T[2 * n - 1];
sum = new T[2 * n - 1];
left = new T[2 * n - 1];
right = new T[2 * n - 1];
lazy = new T[2 * n - 1];
rep(i, 2 * n - 1) sum[i] = ans[i] = right[i] = left[i] = 0, lazy[i] = inf;
}
~SegTree() {
delete ans;
delete sum;
delete left;
delete right;
delete lazy;
}
inline void fix(int i) {
int l = i * 2 + 1, r = i * 2 + 2;
ans[i] = max(max(ans[l], ans[r]), left[r] + right[l]);
sum[i] = sum[l] + sum[r];
left[i] = max(sum[l] + left[r], left[l]);
right[i] = max(sum[r] + right[l], right[r]);
}
inline void lazyfix(int k, int l, int r) {
if (lazy[k] == inf)
return;
update(l, (l + r) / 2, lazy[k], k * 2 + 1, l, (l + r) / 2);
update((l + r) / 2, r, lazy[k], k * 2 + 2, (l + r) / 2, r);
lazy[k] = inf;
}
void init() {
for (int i = n - 2; i >= 0; i--)
fix(i);
}
inline void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }
inline void update(int a, int b, T x, int k, int l, int r) {
if (b <= l || a >= r)
return;
if (a <= l && r <= b) {
lazy[k] = x;
left[k] = right[k] = ans[k] = x >= 0 ? (r - l) * x : x;
sum[k] = (r - l) * x;
return;
}
lazyfix(k, l, r);
int lch = k * 2 + 1, rch = k * 2 + 2;
update(a, b, x, lch, l, (l + r) / 2);
update(a, b, x, rch, (l + r) / 2, r);
fix(k);
}
#define LEFT a, b, k * 2 + 1, l, (l + r) / 2
#define RIGHT a, b, k * 2 + 2, (l + r) / 2, r
inline T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return ans[k];
lazyfix(k, l, r);
int vl = query(LEFT);
int vr = query(RIGHT);
int vll = queryRight(LEFT);
int vrr = queryLeft(RIGHT);
return max(max(vl, vr), vll + vrr);
}
inline T querySum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return sum[k];
lazyfix(k, l, r);
int vl = querySum(LEFT);
int vr = querySum(RIGHT);
return vl + vr;
}
inline T queryRight(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return right[k];
lazyfix(k, l, r);
int R = queryRight(RIGHT);
int rsum = querySum(RIGHT);
int lr = queryRight(LEFT);
return max(R, rsum + lr);
}
inline T queryLeft(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b)
return left[k];
lazyfix(k, l, r);
int L = queryLeft(LEFT);
int lsum = querySum(LEFT);
int rl = queryLeft(RIGHT);
return max(L, lsum + rl);
}
#undef LEFT
#undef RIGHT
};
const int MX = 200000;
const int MX_L = 18;
SegTree<int> *st[MX];
int st_root[MX];
int st_sz;
int n, q;
int w[MX];
int sz[MX];
int parent[MX_L][MX];
int depth[MX];
int which[MX];
vector<vi> e;
void size_rec(int, int);
void heavy_light_rec(int, int, int);
void segtest();
void fix(int a, int b, int c) {
while (1) {
if (which[a] < 0) {
w[a] = c;
if (a == b)
break;
a = parent[0][a];
} else {
int v = st_root[which[a]];
int base = depth[v];
if (depth[v] <= depth[b])
v = b;
int da = depth[a] - base;
int dv = depth[v] - base;
st[which[a]]->update(dv, da + 1, c);
if (v == b)
break;
a = parent[0][v];
}
}
}
pi calc(int a, int b) {
int res = -inf, resleft = -inf;
while (1) {
if (which[a] < 0) {
res = max(res, max(w[a], resleft > -inf ? w[a] + resleft : -inf));
resleft = max(w[a], resleft > -inf ? w[a] + resleft : -inf);
if (a == b)
break;
a = parent[0][a];
} else {
int v = st_root[which[a]];
int base = depth[v];
if (depth[v] <= depth[b])
v = b;
int da = depth[a] - base;
int dv = depth[v] - base;
int m = st[which[a]]->n;
int Q = st[which[a]]->query(dv, da + 1, 0, 0, m);
int L = st[which[a]]->queryLeft(dv, da + 1, 0, 0, m);
int R = st[which[a]]->queryRight(dv, da + 1, 0, 0, m);
int S = st[which[a]]->querySum(dv, da + 1, 0, 0, m);
res = max(res, max(Q, resleft > -inf ? R + resleft : -inf));
resleft = max(resleft > -inf ? S + resleft : -inf, L);
if (v == b)
break;
a = parent[0][v];
}
}
return mp(res, resleft);
}
void run() {
scanf("%d%d", &n, &q);
rep(i, n) scanf("%d", w + i);
e.resize(n);
rep(i, n - 1) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
e[a].pb(b);
e[b].pb(a);
}
memset(which, -1, sizeof(which));
size_rec(0, 0);
heavy_light_rec(0, 0, -1);
rep(i, MX_L - 1) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];
while (q--) {
int t, aa, bb, c, v;
scanf("%d%d%d%d", &t, &aa, &bb, &c);
aa--;
bb--;
if (depth[aa] > depth[bb])
swap(aa, bb);
int a = aa, b = bb;
int d = depth[b] - depth[a];
rep(i, MX_L) if (d & 1 << i) b = parent[i][b];
if (a == b)
v = a;
else {
for (int i = MX_L - 1; i >= 0; i--)
if (parent[i][a] != parent[i][b]) {
a = parent[i][a];
b = parent[i][b];
}
v = parent[0][a];
}
if (t == 1) {
fix(aa, v, c);
fix(bb, v, c);
} else {
if (v == aa) {
printf("%d\n", calc(bb, aa).first);
} else {
pi x = calc(aa, a), y = calc(bb, v);
int ans = max(max(x.first, y.first), x.second + y.second);
printf("%d\n", ans);
}
}
}
}
void size_rec(int c, int p) {
sz[c] = 1;
depth[c] = c == p ? 0 : depth[p] + 1;
parent[0][c] = p;
rep(i, e[c].size()) if (e[c][i] != p) {
size_rec(e[c][i], c);
sz[c] += sz[e[c][i]];
}
}
void heavy_light_rec(int c, int p, int root) {
bool found = 0;
rep(i, e[c].size()) {
int to = e[c][i];
if (to == p)
continue;
if (2 * sz[to] >= sz[c]) {
heavy_light_rec(to, c, root < 0 ? c : root);
found = 1;
} else
heavy_light_rec(to, c, -1);
}
if (!found && root >= 0) {
int size = depth[c] - depth[root] + 1;
SegTree<int> *t = st[st_sz] = new SegTree<int>(size);
st_root[st_sz] = root;
for (int v = c, k = size - 1 + t->n - 1;; v = parent[0][v], k--) {
which[v] = st_sz;
t->left[k] = t->right[k] = t->ans[k] = w[v];
t->sum[k] = w[v];
if (v == root)
break;
}
t->init();
st_sz++;
}
}
long long esp_org, esp_new;
int main() {
const int size = 128 * 1024 * 1024;
void *p = malloc(size);
esp_new = (long long)p + size - 1;
__asm__("mov %%rsp, %0" : "=r"(esp_org) : :);
__asm__("mov %0, %%rsp" : : "r"(esp_new) :);
run();
__asm__("mov %0, %%rsp" : : "r"(esp_org) :);
free(p);
return 0;
}
|
replace
| 138 | 140 | 138 | 140 |
-11
| |
p01564
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
struct HLDT {
vector<vector<int>> graph;
// vid:????§£????????????????????????????????§?????£?¶????
vector<int> vid, head, heavy, par, depth, inv;
HLDT() {}
HLDT(int n)
: graph(n), vid(n, -1), head(n), heavy(n, -1), par(n), depth(n), inv(n) {}
void add_edge(int u, int v) {
graph[u].push_back(v);
graph[v].push_back(u);
}
void build() {
dfs(0, -1);
bfs();
}
int dfs(int u, int p) {
par[u] = p;
int subsz = 1, max_chsz = 0;
for (int v : graph[u]) {
if (v == p)
continue;
depth[v] = depth[u] + 1;
int chsz = dfs(v, u);
subsz += chsz;
if (max_chsz < chsz)
max_chsz = chsz, heavy[u] = v;
}
return subsz;
}
void bfs() {
int id = 0;
queue<int> que; // ????§£?????????????????????
que.push(0);
while (!que.empty()) {
int h = que.front();
que.pop();
for (int i = h; i != -1;
i = heavy[i]) { // ????????????Heavy
// edge????????£?????????????????????
vid[i] = id++;
inv[vid[i]] = i;
head[i] = h;
for (int j : graph[i]) {
if (j != par[i] && j != heavy[i])
que.push(j);
}
}
}
}
void for_each(int u, int v, vector<tuple<int, int, bool>> &lr) {
if (vid[u] < vid[v]) {
lr.emplace_back(max(vid[head[v]], vid[u]), vid[v], true); // ?????????
if (head[u] != head[v])
for_each(u, par[head[v]], lr);
} else { // ??????????????????????????§????????????????????§????????????????????????
lr.emplace_back(max(vid[head[u]], vid[v]), vid[u], false); // ?????????
if (head[u] != head[v])
for_each(par[head[u]], v, lr);
}
}
int lca(int u, int v) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] == head[v])
return u;
return lca(u, par[head[v]]);
}
int distance(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
};
struct State {
int sum, mxs, lmxs, rmxs;
State() : sum(0), mxs(-inf), lmxs(-inf), rmxs(-inf) {}
State(int sum, int mxs, int lmxs, int rmxs)
: sum(sum), mxs(mxs), lmxs(lmxs), rmxs(rmxs) {}
State(int c, int sz) {
int d = (c > 0 ? c * sz : c);
sum = c * sz, mxs = lmxs = rmxs = d;
}
State operator+(State s) {
State res;
res.sum = sum + s.sum;
res.mxs = max(mxs, s.mxs, rmxs + s.lmxs);
res.lmxs = max(lmxs, sum + s.lmxs);
res.rmxs = max(rmxs + s.sum, s.rmxs);
return res;
}
State operator~() {
State res = *this;
swap(res.lmxs, res.rmxs);
return res;
}
};
// Lazy-evaluated Segment Tree
struct SegmentTree {
vector<State> sum;
vector<int> lazy;
int sz;
SegmentTree() {}
SegmentTree(int n) {
sz = 1;
while (sz < n)
sz *= 2;
sum.resize(2 * sz - 1, State());
lazy.resize(2 * sz - 1, -inf);
}
inline void lazy_evaluate(int k, int l, int r) {
if (lazy[k] == -inf)
return;
sum[k] = State(lazy[k], r - l);
if (k < sz) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
lazy[k] = -inf;
}
void update(int a, int b, int x, int k, int l, int r) {
lazy_evaluate(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] = x;
lazy_evaluate(k, l, r);
return;
}
update(a, b, x, 2 * k + 1, l, (l + r) / 2);
update(a, b, x, 2 * k + 2, (l + r) / 2, r);
sum[k] = sum[2 * k + 1] + sum[2 * k + 2];
}
void update(int a, int b, int x) { update(a, b, x, 0, 0, sz); }
State query(int a, int b, int k, int l, int r) {
lazy_evaluate(k, l, r);
if (r <= a || b <= l)
return State();
if (a <= l && r <= b)
return sum[k];
return query(a, b, 2 * k + 1, l, (l + r) / 2) +
query(a, b, 2 * k + 2, (l + r) / 2, r);
}
State query(int a, int b) { return query(a, b, 0, 0, sz); }
};
int n, q;
vector<int> w;
HLDT hldt;
SegmentTree seg;
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> n >> q;
w.resize(n);
hldt = HLDT(n);
seg = SegmentTree(n);
rep(i, n) cin >> w[i];
rep(i, n - 1) {
int s, e;
cin >> s >> e;
--s, --e;
hldt.add_edge(s, e);
}
hldt.build();
rep(i, n) {
vector<tuple<int, int, bool>> lr;
hldt.for_each(i, i, lr);
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
seg.update(x, y + 1, w[i]);
}
}
rep(i, q) {
int t, a, b, c;
cin >> t >> a >> b >> c;
--a, --b;
vector<tuple<int, int, bool>> lr;
hldt.for_each(a, b, lr);
if (t == 1) {
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
seg.update(x, y + 1, c);
}
} else if (t == 2) {
State ds, us;
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
if (z)
ds = seg.query(x, y + 1) + ds;
else
us = seg.query(x, y + 1) + us;
}
cout << (~us + ds).mxs << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
struct HLDT {
vector<vector<int>> graph;
// vid:????§£????????????????????????????????§?????£?¶????
vector<int> vid, head, heavy, par, depth, inv;
HLDT() {}
HLDT(int n)
: graph(n), vid(n, -1), head(n), heavy(n, -1), par(n), depth(n), inv(n) {}
void add_edge(int u, int v) {
graph[u].push_back(v);
graph[v].push_back(u);
}
void build() {
dfs(0, -1);
bfs();
}
int dfs(int u, int p) {
par[u] = p;
int subsz = 1, max_chsz = 0;
for (int v : graph[u]) {
if (v == p)
continue;
depth[v] = depth[u] + 1;
int chsz = dfs(v, u);
subsz += chsz;
if (max_chsz < chsz)
max_chsz = chsz, heavy[u] = v;
}
return subsz;
}
void bfs() {
int id = 0;
queue<int> que; // ????§£?????????????????????
que.push(0);
while (!que.empty()) {
int h = que.front();
que.pop();
for (int i = h; i != -1;
i = heavy[i]) { // ????????????Heavy
// edge????????£?????????????????????
vid[i] = id++;
inv[vid[i]] = i;
head[i] = h;
for (int j : graph[i]) {
if (j != par[i] && j != heavy[i])
que.push(j);
}
}
}
}
void for_each(int u, int v, vector<tuple<int, int, bool>> &lr) {
if (vid[u] < vid[v]) {
lr.emplace_back(max(vid[head[v]], vid[u]), vid[v], true); // ?????????
if (head[u] != head[v])
for_each(u, par[head[v]], lr);
} else { // ??????????????????????????§????????????????????§????????????????????????
lr.emplace_back(max(vid[head[u]], vid[v]), vid[u], false); // ?????????
if (head[u] != head[v])
for_each(par[head[u]], v, lr);
}
}
int lca(int u, int v) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] == head[v])
return u;
return lca(u, par[head[v]]);
}
int distance(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
};
struct State {
int sum, mxs, lmxs, rmxs;
State() : sum(0), mxs(-inf), lmxs(-inf), rmxs(-inf) {}
State(int sum, int mxs, int lmxs, int rmxs)
: sum(sum), mxs(mxs), lmxs(lmxs), rmxs(rmxs) {}
State(int c, int sz) {
int d = (c > 0 ? c * sz : c);
sum = c * sz, mxs = lmxs = rmxs = d;
}
State operator+(State s) {
State res;
res.sum = sum + s.sum;
res.mxs = max(mxs, s.mxs, rmxs + s.lmxs);
res.lmxs = max(lmxs, sum + s.lmxs);
res.rmxs = max(rmxs + s.sum, s.rmxs);
return res;
}
State operator~() {
State res = *this;
swap(res.lmxs, res.rmxs);
return res;
}
};
// Lazy-evaluated Segment Tree
struct SegmentTree {
vector<State> sum;
vector<int> lazy;
int sz;
SegmentTree() {}
SegmentTree(int n) {
sz = 1;
while (sz < n)
sz *= 2;
sum.resize(2 * sz - 1, State());
lazy.resize(2 * sz - 1, -inf);
}
inline void lazy_evaluate(int k, int l, int r) {
if (lazy[k] == -inf)
return;
sum[k] = State(lazy[k], r - l);
if (r - l > 1) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
lazy[k] = -inf;
}
void update(int a, int b, int x, int k, int l, int r) {
lazy_evaluate(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] = x;
lazy_evaluate(k, l, r);
return;
}
update(a, b, x, 2 * k + 1, l, (l + r) / 2);
update(a, b, x, 2 * k + 2, (l + r) / 2, r);
sum[k] = sum[2 * k + 1] + sum[2 * k + 2];
}
void update(int a, int b, int x) { update(a, b, x, 0, 0, sz); }
State query(int a, int b, int k, int l, int r) {
lazy_evaluate(k, l, r);
if (r <= a || b <= l)
return State();
if (a <= l && r <= b)
return sum[k];
return query(a, b, 2 * k + 1, l, (l + r) / 2) +
query(a, b, 2 * k + 2, (l + r) / 2, r);
}
State query(int a, int b) { return query(a, b, 0, 0, sz); }
};
int n, q;
vector<int> w;
HLDT hldt;
SegmentTree seg;
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> n >> q;
w.resize(n);
hldt = HLDT(n);
seg = SegmentTree(n);
rep(i, n) cin >> w[i];
rep(i, n - 1) {
int s, e;
cin >> s >> e;
--s, --e;
hldt.add_edge(s, e);
}
hldt.build();
rep(i, n) {
vector<tuple<int, int, bool>> lr;
hldt.for_each(i, i, lr);
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
seg.update(x, y + 1, w[i]);
}
}
rep(i, q) {
int t, a, b, c;
cin >> t >> a >> b >> c;
--a, --b;
vector<tuple<int, int, bool>> lr;
hldt.for_each(a, b, lr);
if (t == 1) {
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
seg.update(x, y + 1, c);
}
} else if (t == 2) {
State ds, us;
for (auto p : lr) {
int x, y;
bool z;
tie(x, y, z) = p;
if (z)
ds = seg.query(x, y + 1) + ds;
else
us = seg.query(x, y + 1) + us;
}
cout << (~us + ds).mxs << endl;
}
}
return 0;
}
|
replace
| 135 | 136 | 135 | 136 |
-6
|
free(): invalid pointer
|
p01564
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid, int)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
const OperatorMonoid OM0)
: f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n)
sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], lazy[k], len);
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
template <typename G> struct CentroidPathDecomposition {
struct Centroid {
int ParIndex, ParDepth, Deep;
vector<int> node;
Centroid(int idx, int dep, int deep)
: ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size() { return (node.size()); }
inline int &operator[](int k) { return (node[k]); }
inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }
};
const G &graph;
vector<int> SubTreeSize, NextPath;
vector<int> TreeIndex, TreeDepth;
vector<Centroid> Centroids;
void BuildSubTreeSize() {
stack<pair<int, int>> s;
s.emplace(0, -1);
while (!s.empty()) {
auto p = s.top();
s.pop();
if (~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for (auto &to : graph[p.first]) {
if (p.second == to)
continue;
SubTreeSize[p.first] += SubTreeSize[to];
if (NextPath[p.first] == -1 ||
SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for (auto &to : graph[p.first]) {
if (p.second != to)
s.emplace(to, p.first);
}
}
}
}
void BuildPath() {
stack<pair<int, int>> s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while (!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();
for (auto &to : graph[p.first]) {
if (p.second == to)
continue;
if (to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int)Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],
Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
virtual void Build() {
BuildSubTreeSize();
BuildPath();
}
inline size_t size() { return (Centroids.size()); }
inline pair<int, int> Information(int idx) {
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k) { return (Centroids[k]); }
inline int LCA(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while (TreeIdxA != TreeIdxB) {
if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b,
const function<void(int, int, int)> &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while (TreeIdxA != TreeIdxB) {
if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(const G &g) : graph(g) {
int SZ = g.size();
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
template <typename G> struct TreeArray : CentroidPathDecomposition<G> {
using CPD = CentroidPathDecomposition<G>;
TreeArray(const G &g) : CPD(g) {}
vector<int> index;
void Build() {
CPD::Build();
int ptr = 0;
for (auto ¢roid : CPD::Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a) {
auto p = CPD::Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function<void(int, int)> &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
while (TreeIdxA != TreeIdxB) {
if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
}
inline vector<tuple<int, int, bool>> get_path(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
vector<tuple<int, int, bool>> vs, ws;
while (TreeIdxA != TreeIdxB) {
if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1,
true);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1,
false);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
bool rev = true;
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB), rev = false;
vs.emplace_back(index[TreeIdxA] + TreeDepthA,
index[TreeIdxA] + TreeDepthB + 1, !rev);
reverse(begin(ws), end(ws));
copy(begin(ws), end(ws), back_inserter(vs));
return (vs);
}
};
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
using int64 = long long;
const int64 INF = 1LL << 60;
struct node {
int64 left, right, ans, all;
};
int main() {
int N, Q, S[100000];
scanf("%d %d", &N, &Q);
for (int i = 0; i < N; i++) {
scanf("%d", &S[i]);
}
UnWeightedGraph g(N);
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
--x, --y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
TreeArray<UnWeightedGraph> cpd(g);
cpd.Build();
node np = (node){-INF, -INF, -INF, 0};
auto f = [](const node &x, const node &y) {
node z;
z.all = x.all + y.all;
z.left = max(x.left, x.all + y.left);
z.right = max(y.right, y.all + x.right);
z.ans = max(x.right + y.left, max(x.ans, y.ans));
return z;
};
auto gg = [](const node &x, int64 y, int len) {
node z;
z.all = y * len;
z.ans = z.left = z.right = max(y, z.all);
return z;
};
auto h = [&](int64 x, int64 y) { return y; };
LazySegmentTree<node, int64> seg(N, f, gg, h, np, -INF);
for (int i = 0; i < N; i++) {
seg.set(cpd.get(i), (node){S[i], S[i], S[i], S[i]});
}
seg.build();
for (int i = 0; i < Q; i++) {
int t, x, y, z;
scanf("%d %d %d %d", &t, &x, &y, &z);
--x, --y;
if (t == 1) {
cpd.query(x, y, [&](int a, int b) { seg.update(a, b, z); });
} else {
auto l = np, r = np;
for (auto &p : cpd.get_path(x, y)) {
int a, b;
bool c;
tie(a, b, c) = p;
if (c)
r = f(seg.query(a, b), r);
else
l = f(l, seg.query(a, b));
}
swap(l.left, l.right);
printf("%lld\n", f(l, r).ans);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid, int)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
const OperatorMonoid OM0)
: f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n)
sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], lazy[k], len);
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
template <typename G> struct CentroidPathDecomposition {
struct Centroid {
int ParIndex, ParDepth, Deep;
vector<int> node;
Centroid(int idx, int dep, int deep)
: ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size() { return (node.size()); }
inline int &operator[](int k) { return (node[k]); }
inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }
};
const G &graph;
vector<int> SubTreeSize, NextPath;
vector<int> TreeIndex, TreeDepth;
vector<Centroid> Centroids;
void BuildSubTreeSize() {
stack<pair<int, int>> s;
s.emplace(0, -1);
while (!s.empty()) {
auto p = s.top();
s.pop();
if (~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for (auto &to : graph[p.first]) {
if (p.second == to)
continue;
SubTreeSize[p.first] += SubTreeSize[to];
if (NextPath[p.first] == -1 ||
SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for (auto &to : graph[p.first]) {
if (p.second != to)
s.emplace(to, p.first);
}
}
}
}
void BuildPath() {
stack<pair<int, int>> s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while (!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();
for (auto &to : graph[p.first]) {
if (p.second == to)
continue;
if (to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int)Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],
Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
virtual void Build() {
BuildSubTreeSize();
BuildPath();
}
inline size_t size() { return (Centroids.size()); }
inline pair<int, int> Information(int idx) {
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k) { return (Centroids[k]); }
inline int LCA(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while (TreeIdxA != TreeIdxB) {
if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b,
const function<void(int, int, int)> &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while (TreeIdxA != TreeIdxB) {
if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(const G &g) : graph(g) {
int SZ = g.size();
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
template <typename G> struct TreeArray : CentroidPathDecomposition<G> {
using CPD = CentroidPathDecomposition<G>;
TreeArray(const G &g) : CPD(g) {}
vector<int> index;
void Build() {
CPD::Build();
int ptr = 0;
for (auto ¢roid : CPD::Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a) {
auto p = CPD::Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function<void(int, int)> &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
while (TreeIdxA != TreeIdxB) {
if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
}
inline vector<tuple<int, int, bool>> get_path(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
vector<tuple<int, int, bool>> vs, ws;
while (TreeIdxA != TreeIdxB) {
if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1,
true);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1,
false);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
bool rev = true;
if (TreeDepthA > TreeDepthB)
swap(TreeDepthA, TreeDepthB), rev = false;
vs.emplace_back(index[TreeIdxA] + TreeDepthA,
index[TreeIdxA] + TreeDepthB + 1, !rev);
reverse(begin(ws), end(ws));
copy(begin(ws), end(ws), back_inserter(vs));
return (vs);
}
};
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
using int64 = long long;
const int64 INF = 1LL << 60;
struct node {
int64 left, right, ans, all;
};
int main() {
int N, Q, S[200000];
scanf("%d %d", &N, &Q);
for (int i = 0; i < N; i++) {
scanf("%d", &S[i]);
}
UnWeightedGraph g(N);
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
--x, --y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
TreeArray<UnWeightedGraph> cpd(g);
cpd.Build();
node np = (node){-INF, -INF, -INF, 0};
auto f = [](const node &x, const node &y) {
node z;
z.all = x.all + y.all;
z.left = max(x.left, x.all + y.left);
z.right = max(y.right, y.all + x.right);
z.ans = max(x.right + y.left, max(x.ans, y.ans));
return z;
};
auto gg = [](const node &x, int64 y, int len) {
node z;
z.all = y * len;
z.ans = z.left = z.right = max(y, z.all);
return z;
};
auto h = [&](int64 x, int64 y) { return y; };
LazySegmentTree<node, int64> seg(N, f, gg, h, np, -INF);
for (int i = 0; i < N; i++) {
seg.set(cpd.get(i), (node){S[i], S[i], S[i], S[i]});
}
seg.build();
for (int i = 0; i < Q; i++) {
int t, x, y, z;
scanf("%d %d %d %d", &t, &x, &y, &z);
--x, --y;
if (t == 1) {
cpd.query(x, y, [&](int a, int b) { seg.update(a, b, z); });
} else {
auto l = np, r = np;
for (auto &p : cpd.get_path(x, y)) {
int a, b;
bool c;
tie(a, b, c) = p;
if (c)
r = f(seg.query(a, b), r);
else
l = f(l, seg.query(a, b));
}
swap(l.left, l.right);
printf("%lld\n", f(l, r).ans);
}
}
}
|
replace
| 309 | 310 | 309 | 310 |
0
| |
p01568
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
/* 幾何の基本 */
#include <complex>
typedef long double ld;
typedef complex<ld> Point;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const ld eps = 1e-12, pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps)
return true;
if (lhs.real() > rhs.real() + eps)
return false;
return lhs.imag() < rhs.imag();
}
} // namespace std
// 点の入力
Point input_point() {
ld x, y;
cin >> x >> y;
return Point(x, y);
}
// 誤差つき等号判定
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }
// 内積
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }
// 外積
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }
// 直線の定義
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
Point operator[](const int _num) const {
if (_num == 0)
return a;
else if (_num == 1)
return b;
else
assert(false);
}
};
// 円の定義
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW
int ccw(const Point &a, const Point &b, const Point &c) {
const Point nb(b - a);
const Point nc(c - a);
if (cross(nb, nc) > eps)
return 1; // a,b,cが反時計周りの順に並ぶ
if (cross(nb, nc) < -eps)
return -1; // a,b,cが時計周りの順に並ぶ
if (dot(nb, nc) < 0)
return 2; // c,a,bの順に直線に並ぶ
if (norm(nb) < norm(nc))
return -2; // a,b,cの順に直線に並ぶ
return 0; // a,c,bの順に直線に並ぶ
}
/* 交差判定 */
// 直線と直線の交差判定
bool isis_ll(const Line &l, const Line &m) {
return !eq(cross(l.b - l.a, m.b - m.a), 0);
}
// 直線と線分の交差判定
bool isis_ls(const Line &l, const Line &s) {
return isis_ll(l, s) &&
(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
// 線分と線分の交差判定
bool isis_ss(const Line &s, const Line &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
// 点の直線上判定
bool isis_lp(const Line &l, const Point &p) {
return (abs(cross(l.b - p, l.a - p)) < eps);
}
// 点の線分上判定
bool isis_sp(const Line &s, const Point &p) {
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}
// 垂線の足
Point proj(const Line &l, const Point &p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
// 直線と直線の交点
Point is_ll(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// 直線と点の距離
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }
// 直線と直線の距離
ld dist_ll(const Line &l, const Line &m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
// 直線と線分の距離
ld dist_ls(const Line &l, const Line &s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
// 線分と点の距離
ld dist_sp(const Line &s, const Point &p) {
Point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
// 線分と線分の距離
ld dist_ss(const Line &s, const Line &t) {
if (isis_ss(s, t))
return 0;
return min(
{dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}
// 直線と直線の二等分線のベクトル
Line bisection(const Line &s, const Line &t) {
const Point laglanju(is_ll(s, t));
const Point avec =
!(abs(laglanju - s[0])) < eps ? s[0] - laglanju : s[1] - laglanju;
const Point bvec =
!(abs(laglanju - t[0])) < eps ? t[0] - laglanju : t[1] - laglanju;
return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) /
(abs(avec) + abs(bvec)));
}
/* 円 */
// 円と円の交点
vector<Point> is_cc(const Circle &c1, const Circle &c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
// 円と直線の交点
vector<Point> is_lc(const Circle &c, const Line &l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
// 円と線分の距離
vector<Point> is_sc(const Circle &c, const Line &l) {
vector<Point> v = is_lc(c, l), res;
for (Point p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
// 円と点の接線
vector<Line> tangent_cp(const Circle &c, const Point &p) {
vector<Line> ret;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
ret.push_back(Line(p, p + v1));
if (l < eps)
return ret;
ret.push_back(Line(p, p + v2));
return ret;
}
// 円と円の接線
vector<Line> tangent_cc(const Circle &c1, const Circle &c2) {
vector<Line> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
ret.push_back(Line(q1, q1 + v));
ret.push_back(Line(q2, q2 + v));
}
return ret;
}
/* 多角形 */
typedef vector<Point> Polygon;
// 面積
ld area(const Polygon &p) {
ld res = 0;
int n = p.size();
REP(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
// 多角形の回転方向
bool is_counter_clockwise(const Polygon &poly) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];
angle += arg((c - b) / (b - a));
}
return angle > eps;
}
// 円の内外判定
// 0 => out
// 1 => on
// 2 => in
int is_in_polygon(const Polygon &poly, const Point &p) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n];
if (isis_sp(Line(a, b), p))
return 1;
angle += arg((b - p) / (a - p));
}
return eq(angle, 0) ? 0 : 2;
}
// 円の内外判定2 高速
enum { OUT, ON, IN };
int convex_contains(const Polygon &P, const Point &p) {
const int n = P.size();
Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point
int a = 0, b = n;
while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]
int c = (a + b) / 2;
if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg
if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0)
b = c;
else
a = c;
} else {
if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0)
a = c;
else
b = c;
}
}
b %= n;
if (cross(P[a] - p, P[b] - p) < 0)
return 0;
if (cross(P[a] - p, P[b] - p) > 0)
return 2;
return 1;
}
// 凸包
// 点や線を返すことも有り得るので注意
Polygon convex_hull(vector<Point> ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
Polygon ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
// 凸カット
vector<Polygon> convex_cut(const Polygon &ps, const Line &l) {
int n = ps.size();
Polygon Q;
Polygon R;
REP(i, n) {
Point A = ps[i], B = ps[(i + 1) % n];
Line m = Line(A, B);
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) != 1)
R.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {
Q.push_back(is_ll(l, m));
R.push_back(is_ll(l, m));
}
}
const vector<Polygon> polys{Q, R};
return polys;
}
/* アレンジメント */
void add_point(vector<Point> &ps, const Point p) {
for (Point q : ps)
if (abs(q - p) < eps)
return;
ps.push_back(p);
}
typedef double Weight;
struct Edge {
int from, to;
Weight weight;
int id;
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g, const int from, const int to, const Weight weight,
const int id) {
g[from].push_back(Edge{from, to, weight, id});
}
Graph segment_arrangement(const vector<Line> &s,
const vector<pair<Point, bool>> &p) {
int n = p.size(), m = s.size();
int id = 0;
Graph g(n);
REP(i, m) {
vector<pair<ld, int>> vec;
REP(j, n)
if (isis_sp(s[i], p[j].first))
vec.emplace_back(abs(s[i].a - p[j].first), j);
sort(ALL(vec));
REP(j, vec.size() - 1) {
int from = vec[j].second, to = vec[j + 1].second;
add_edge(g, from, to, abs(p[from].first - p[to].first), id);
add_edge(g, to, from, abs(p[from].first - p[to].first), id++);
}
}
return g;
}
// Graph sennbunn_arrangement(const vector<Line>&s) {
// vector<Point>crss;
// for (int i = 0; i < s.size(); ++i) {
// for (int j = i + 1; j < s.size(); ++j) {
// if (isis_ss( s[i], s[j])) {
// crss.push_back(is_ll( s[i], s[j]));
// }
// }
// }
// for (int i = 0; i <s.size(); ++i) {
// crss.push_back( s[i][0]);
// crss.push_back( s[i][1]);
// }
// return segment_arrangement(s, crss);
// }
// Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {
// int n = p.size(), m = c.size();
// Graph g(n);
// REP(i, m) {
// vector<pair<ld, int>> vec;
// REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)
// vec.emplace_back(arg(c[i].p - p[j]), j);
// sort(ALL(vec));
// REP(j, vec.size() - 1) {
// int from = vec[j].second, to = vec[j + 1].second;
// ld angle = vec[j + 1].first - vec[j].first;
// add_edge(g, from, to, angle * c[i].r);
// }
// if (vec.size() >= 2) {
// int from = vec.back().second, to = vec.front().first;
// ld angle = vec.front().first - vec.back().first;
// add_edge(g, from, to, angle * c[i].r);
// }
// }
// return g;
// }
/* 双対グラフ */
// 線分集合は既にアレンジメントされていなければならない.
// 内側の円は時計回りで,外側の円は反時計回りで得られる.
// 変数 polygon は,vector<int> で表される多角形の集合であり,
// vector<int> で表される
// 多角形のi番目は,その頂点の頂点集合pにおける番号である.
vector<vector<int>> polygon;
vector<int> seg2p[1024][1024];
// Graph dual_graph(const vector<Line> &s, const vector<Point> &p) {
// int N = p.size();
// polygon.clear();
// REP(i, 1024) REP(j, 1024) seg2p[i][j].clear();
// vector<vector<tuple<ld, int, bool>>> tup(N);
// REP(i, s.size()) {
// int a = -1, b = -1;
// REP(j, N) if (abs(s[i].a - p[j]) < eps) a = j;
// REP(j, N) if (abs(s[i].b - p[j]) < eps) b = j;
// assert(a >= 0 && b >= 0);
// tup[a].emplace_back(arg(s[i].b - s[i].a), b, false);
// tup[b].emplace_back(arg(s[i].a - s[i].b), a, false);
// }
// REP(i, N) sort(ALL(tup[i]));
// REP(i, N) {
// REP(j, tup[i].size()) {
// ld angle; int pos = j, from = i, to; bool flag;
// tie(angle, to, flag) = tup[i][j];
// if (flag) continue;
// vector<int> ps;
// while (!flag) {
// ps.push_back(from);
// get<2>(tup[from][pos]) = true;
// seg2p[from][to].push_back(polygon.size());
// seg2p[to][from].push_back(polygon.size());
// angle += pi + eps;
// if (angle > pi) angle -= 2 * pi;
// auto it = lower_bound(ALL(tup[to]), make_tuple(angle,
//0, false)); if (it == tup[to].end()) it = tup[to].begin(); from = to;
//tie(angle, to, flag) = *it; pos = it - tup[from].begin();
// }
// polygon.push_back(ps);
// }
// }
// Graph g(polygon.size());
// REP(i, N) REP(j, i) {
// if (seg2p[i][j].size() == 2) {
// int from = seg2p[i][j][0], to = seg2p[i][j][1];
// g[from].push_back(Edge{ from, to });
// g[to].push_back(Edge{ to, from });
// }
// }
// return g;
// }
/* ビジュアライザ */
const ld zoom = 25;
const ld centerX = 6;
const ld centerY = 5;
void change_color(const int r, const int g, const int b) {
fprintf(stderr, "c.strokeStyle = 'rgb(%d, %d, %d)';\n", r, g, b);
}
int cordx(Point p) { return 400 + zoom * (p.real() - centerX); }
int cordy(Point p) { return 400 - zoom * (p.imag() - centerY); }
#define cord(p) cordx(p), cordy(p)
void draw_point(const Point &p) {
fprintf(stderr, "circle(%d, %d, %d)\n", cord(p), 2);
}
void draw_segment(const Line &l) {
fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(l.a), cord(l.b));
}
void draw_line(const Line &l) {
Point v = l.b - l.a;
Line m(l.a - v * Point(1e4, 0), l.b + v * Point(1e4, 0));
fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(m.a), cord(m.b));
}
void draw_polygon(const Polygon &p) {
int n = p.size();
REP(i, n) draw_segment(Line(p[i], p[(i + 1) % n]));
}
void draw_circle(const Circle c) {
fprintf(stderr, "circle(%d, %d, %d)\n", cord(c.p), (int)(zoom * c.r));
}
int startid;
int goalid;
bool dfs0(const Graph &g, const int now, vector<bool> &came,
vector<bool> &close, const vector<bool> &bulbs) {
came[now] = true;
for (auto e : g[now]) {
if (!came[e.to]) {
if (e.to == startid)
return false;
else {
if (bulbs[e.to]) {
close[e.to] = true;
} else {
if (!dfs0(g, e.to, came, close, bulbs)) {
return false;
}
}
}
}
}
return true;
}
double dfs1(const Graph &g, const int now, vector<bool> &came,
vector<bool> &useedges, const vector<bool> &close) {
came[now] = true;
double ans = 0;
for (auto e : g[now]) {
if (!useedges[e.id]) {
ans += e.weight;
useedges[e.id] = true;
if (close[e.to])
continue;
else {
if (!came[e.to])
ans += dfs1(g, e.to, came, useedges, close);
}
}
}
return ans;
}
int main() {
double sum = 0;
Graph g;
int sx, sy, gx, gy;
Point start;
Point goal;
vector<pair<Point, bool>> crss;
vector<bool> bulbs;
{
int N, M;
cin >> N >> M;
vector<Line> ls;
for (int i = 0; i < N; ++i) {
int asx, asy, dx, dy;
cin >> asx >> asy >> dx >> dy;
ls.push_back(Line(Point(asx, asy), Point(dx, dy)));
sum += (abs(Point(asx, asy) - Point(dx, dy)));
}
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < ls.size(); ++j) {
bool ok = false;
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (ls[i][k] == ls[j][l]) {
ok = true;
crss.push_back(make_pair(is_ll(ls[i], ls[j]), false));
break;
}
}
if (ok)
break;
}
if (!ok && isis_ss(ls[i], ls[j])) {
crss.push_back(make_pair(is_ll(ls[i], ls[j]), false));
}
}
}
for (int i = 0; i < ls.size(); ++i) {
crss.push_back(make_pair(ls[i][0], false));
crss.push_back(make_pair(ls[i][1], false));
}
vector<Point> bs;
for (int i = 0; i < M; ++i) {
int x, y;
cin >> x >> y;
Point p(x, y);
bs.push_back(p);
}
cin >> sx >> sy >> gx >> gy;
start = Point(sx, sy);
goal = Point(gx, gy);
crss.push_back(make_pair(start, false));
crss.push_back(make_pair(goal, false));
sort(crss.begin(), crss.end());
crss.erase(unique(crss.begin(), crss.end()), crss.end());
for (int i = 0; i < M; ++i) {
auto it =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == bs[i]; });
if (it == crss.end()) {
crss.push_back(make_pair(bs[i], true));
} else {
(*it).second = true;
}
}
for (int i = 0; i < crss.size(); ++i) {
bulbs.push_back(crss[i].second);
}
g = segment_arrangement(ls, crss);
}
auto startit =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == start; });
startid = startit - crss.begin();
auto goalit =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == goal; });
goalid = goalit - crss.begin();
vector<bool> close(crss.size(), false);
vector<bool> came(crss.size(), false);
if (dfs0(g, goalid, came, close, bulbs)) {
vector<bool> acame(crss.size(), false);
vector<bool> useedges(80000, false);
double ans = dfs1(g, startid, acame, useedges, close);
cout << fixed << setprecision(10) << sum - ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
|
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
/* 幾何の基本 */
#include <complex>
typedef long double ld;
typedef complex<ld> Point;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const ld eps = 1e-12, pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps)
return true;
if (lhs.real() > rhs.real() + eps)
return false;
return lhs.imag() < rhs.imag();
}
} // namespace std
// 点の入力
Point input_point() {
ld x, y;
cin >> x >> y;
return Point(x, y);
}
// 誤差つき等号判定
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }
// 内積
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }
// 外積
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }
// 直線の定義
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
Point operator[](const int _num) const {
if (_num == 0)
return a;
else if (_num == 1)
return b;
else
assert(false);
}
};
// 円の定義
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW
int ccw(const Point &a, const Point &b, const Point &c) {
const Point nb(b - a);
const Point nc(c - a);
if (cross(nb, nc) > eps)
return 1; // a,b,cが反時計周りの順に並ぶ
if (cross(nb, nc) < -eps)
return -1; // a,b,cが時計周りの順に並ぶ
if (dot(nb, nc) < 0)
return 2; // c,a,bの順に直線に並ぶ
if (norm(nb) < norm(nc))
return -2; // a,b,cの順に直線に並ぶ
return 0; // a,c,bの順に直線に並ぶ
}
/* 交差判定 */
// 直線と直線の交差判定
bool isis_ll(const Line &l, const Line &m) {
return !eq(cross(l.b - l.a, m.b - m.a), 0);
}
// 直線と線分の交差判定
bool isis_ls(const Line &l, const Line &s) {
return isis_ll(l, s) &&
(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
// 線分と線分の交差判定
bool isis_ss(const Line &s, const Line &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
// 点の直線上判定
bool isis_lp(const Line &l, const Point &p) {
return (abs(cross(l.b - p, l.a - p)) < eps);
}
// 点の線分上判定
bool isis_sp(const Line &s, const Point &p) {
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}
// 垂線の足
Point proj(const Line &l, const Point &p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
// 直線と直線の交点
Point is_ll(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// 直線と点の距離
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }
// 直線と直線の距離
ld dist_ll(const Line &l, const Line &m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
// 直線と線分の距離
ld dist_ls(const Line &l, const Line &s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
// 線分と点の距離
ld dist_sp(const Line &s, const Point &p) {
Point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
// 線分と線分の距離
ld dist_ss(const Line &s, const Line &t) {
if (isis_ss(s, t))
return 0;
return min(
{dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}
// 直線と直線の二等分線のベクトル
Line bisection(const Line &s, const Line &t) {
const Point laglanju(is_ll(s, t));
const Point avec =
!(abs(laglanju - s[0])) < eps ? s[0] - laglanju : s[1] - laglanju;
const Point bvec =
!(abs(laglanju - t[0])) < eps ? t[0] - laglanju : t[1] - laglanju;
return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) /
(abs(avec) + abs(bvec)));
}
/* 円 */
// 円と円の交点
vector<Point> is_cc(const Circle &c1, const Circle &c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
// 円と直線の交点
vector<Point> is_lc(const Circle &c, const Line &l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
// 円と線分の距離
vector<Point> is_sc(const Circle &c, const Line &l) {
vector<Point> v = is_lc(c, l), res;
for (Point p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
// 円と点の接線
vector<Line> tangent_cp(const Circle &c, const Point &p) {
vector<Line> ret;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
ret.push_back(Line(p, p + v1));
if (l < eps)
return ret;
ret.push_back(Line(p, p + v2));
return ret;
}
// 円と円の接線
vector<Line> tangent_cc(const Circle &c1, const Circle &c2) {
vector<Line> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
ret.push_back(Line(q1, q1 + v));
ret.push_back(Line(q2, q2 + v));
}
return ret;
}
/* 多角形 */
typedef vector<Point> Polygon;
// 面積
ld area(const Polygon &p) {
ld res = 0;
int n = p.size();
REP(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
// 多角形の回転方向
bool is_counter_clockwise(const Polygon &poly) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];
angle += arg((c - b) / (b - a));
}
return angle > eps;
}
// 円の内外判定
// 0 => out
// 1 => on
// 2 => in
int is_in_polygon(const Polygon &poly, const Point &p) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n];
if (isis_sp(Line(a, b), p))
return 1;
angle += arg((b - p) / (a - p));
}
return eq(angle, 0) ? 0 : 2;
}
// 円の内外判定2 高速
enum { OUT, ON, IN };
int convex_contains(const Polygon &P, const Point &p) {
const int n = P.size();
Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point
int a = 0, b = n;
while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]
int c = (a + b) / 2;
if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg
if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0)
b = c;
else
a = c;
} else {
if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0)
a = c;
else
b = c;
}
}
b %= n;
if (cross(P[a] - p, P[b] - p) < 0)
return 0;
if (cross(P[a] - p, P[b] - p) > 0)
return 2;
return 1;
}
// 凸包
// 点や線を返すことも有り得るので注意
Polygon convex_hull(vector<Point> ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
Polygon ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
// 凸カット
vector<Polygon> convex_cut(const Polygon &ps, const Line &l) {
int n = ps.size();
Polygon Q;
Polygon R;
REP(i, n) {
Point A = ps[i], B = ps[(i + 1) % n];
Line m = Line(A, B);
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) != 1)
R.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {
Q.push_back(is_ll(l, m));
R.push_back(is_ll(l, m));
}
}
const vector<Polygon> polys{Q, R};
return polys;
}
/* アレンジメント */
void add_point(vector<Point> &ps, const Point p) {
for (Point q : ps)
if (abs(q - p) < eps)
return;
ps.push_back(p);
}
typedef double Weight;
struct Edge {
int from, to;
Weight weight;
int id;
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g, const int from, const int to, const Weight weight,
const int id) {
g[from].push_back(Edge{from, to, weight, id});
}
Graph segment_arrangement(const vector<Line> &s,
const vector<pair<Point, bool>> &p) {
int n = p.size(), m = s.size();
int id = 0;
Graph g(n);
REP(i, m) {
vector<pair<ld, int>> vec;
REP(j, n)
if (isis_sp(s[i], p[j].first))
vec.emplace_back(abs(s[i].a - p[j].first), j);
sort(ALL(vec));
REP(j, vec.size() - 1) {
int from = vec[j].second, to = vec[j + 1].second;
add_edge(g, from, to, abs(p[from].first - p[to].first), id);
add_edge(g, to, from, abs(p[from].first - p[to].first), id++);
}
}
return g;
}
// Graph sennbunn_arrangement(const vector<Line>&s) {
// vector<Point>crss;
// for (int i = 0; i < s.size(); ++i) {
// for (int j = i + 1; j < s.size(); ++j) {
// if (isis_ss( s[i], s[j])) {
// crss.push_back(is_ll( s[i], s[j]));
// }
// }
// }
// for (int i = 0; i <s.size(); ++i) {
// crss.push_back( s[i][0]);
// crss.push_back( s[i][1]);
// }
// return segment_arrangement(s, crss);
// }
// Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {
// int n = p.size(), m = c.size();
// Graph g(n);
// REP(i, m) {
// vector<pair<ld, int>> vec;
// REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)
// vec.emplace_back(arg(c[i].p - p[j]), j);
// sort(ALL(vec));
// REP(j, vec.size() - 1) {
// int from = vec[j].second, to = vec[j + 1].second;
// ld angle = vec[j + 1].first - vec[j].first;
// add_edge(g, from, to, angle * c[i].r);
// }
// if (vec.size() >= 2) {
// int from = vec.back().second, to = vec.front().first;
// ld angle = vec.front().first - vec.back().first;
// add_edge(g, from, to, angle * c[i].r);
// }
// }
// return g;
// }
/* 双対グラフ */
// 線分集合は既にアレンジメントされていなければならない.
// 内側の円は時計回りで,外側の円は反時計回りで得られる.
// 変数 polygon は,vector<int> で表される多角形の集合であり,
// vector<int> で表される
// 多角形のi番目は,その頂点の頂点集合pにおける番号である.
vector<vector<int>> polygon;
vector<int> seg2p[1024][1024];
// Graph dual_graph(const vector<Line> &s, const vector<Point> &p) {
// int N = p.size();
// polygon.clear();
// REP(i, 1024) REP(j, 1024) seg2p[i][j].clear();
// vector<vector<tuple<ld, int, bool>>> tup(N);
// REP(i, s.size()) {
// int a = -1, b = -1;
// REP(j, N) if (abs(s[i].a - p[j]) < eps) a = j;
// REP(j, N) if (abs(s[i].b - p[j]) < eps) b = j;
// assert(a >= 0 && b >= 0);
// tup[a].emplace_back(arg(s[i].b - s[i].a), b, false);
// tup[b].emplace_back(arg(s[i].a - s[i].b), a, false);
// }
// REP(i, N) sort(ALL(tup[i]));
// REP(i, N) {
// REP(j, tup[i].size()) {
// ld angle; int pos = j, from = i, to; bool flag;
// tie(angle, to, flag) = tup[i][j];
// if (flag) continue;
// vector<int> ps;
// while (!flag) {
// ps.push_back(from);
// get<2>(tup[from][pos]) = true;
// seg2p[from][to].push_back(polygon.size());
// seg2p[to][from].push_back(polygon.size());
// angle += pi + eps;
// if (angle > pi) angle -= 2 * pi;
// auto it = lower_bound(ALL(tup[to]), make_tuple(angle,
//0, false)); if (it == tup[to].end()) it = tup[to].begin(); from = to;
//tie(angle, to, flag) = *it; pos = it - tup[from].begin();
// }
// polygon.push_back(ps);
// }
// }
// Graph g(polygon.size());
// REP(i, N) REP(j, i) {
// if (seg2p[i][j].size() == 2) {
// int from = seg2p[i][j][0], to = seg2p[i][j][1];
// g[from].push_back(Edge{ from, to });
// g[to].push_back(Edge{ to, from });
// }
// }
// return g;
// }
/* ビジュアライザ */
const ld zoom = 25;
const ld centerX = 6;
const ld centerY = 5;
void change_color(const int r, const int g, const int b) {
fprintf(stderr, "c.strokeStyle = 'rgb(%d, %d, %d)';\n", r, g, b);
}
int cordx(Point p) { return 400 + zoom * (p.real() - centerX); }
int cordy(Point p) { return 400 - zoom * (p.imag() - centerY); }
#define cord(p) cordx(p), cordy(p)
void draw_point(const Point &p) {
fprintf(stderr, "circle(%d, %d, %d)\n", cord(p), 2);
}
void draw_segment(const Line &l) {
fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(l.a), cord(l.b));
}
void draw_line(const Line &l) {
Point v = l.b - l.a;
Line m(l.a - v * Point(1e4, 0), l.b + v * Point(1e4, 0));
fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(m.a), cord(m.b));
}
void draw_polygon(const Polygon &p) {
int n = p.size();
REP(i, n) draw_segment(Line(p[i], p[(i + 1) % n]));
}
void draw_circle(const Circle c) {
fprintf(stderr, "circle(%d, %d, %d)\n", cord(c.p), (int)(zoom * c.r));
}
int startid;
int goalid;
bool dfs0(const Graph &g, const int now, vector<bool> &came,
vector<bool> &close, const vector<bool> &bulbs) {
came[now] = true;
for (auto e : g[now]) {
if (!came[e.to]) {
if (e.to == startid)
return false;
else {
if (bulbs[e.to]) {
close[e.to] = true;
} else {
if (!dfs0(g, e.to, came, close, bulbs)) {
return false;
}
}
}
}
}
return true;
}
double dfs1(const Graph &g, const int now, vector<bool> &came,
vector<bool> &useedges, const vector<bool> &close) {
came[now] = true;
double ans = 0;
for (auto e : g[now]) {
if (!useedges[e.id]) {
ans += e.weight;
useedges[e.id] = true;
if (close[e.to])
continue;
else {
if (!came[e.to])
ans += dfs1(g, e.to, came, useedges, close);
}
}
}
return ans;
}
int main() {
double sum = 0;
Graph g;
int sx, sy, gx, gy;
Point start;
Point goal;
vector<pair<Point, bool>> crss;
vector<bool> bulbs;
{
int N, M;
cin >> N >> M;
vector<Line> ls;
for (int i = 0; i < N; ++i) {
int asx, asy, dx, dy;
cin >> asx >> asy >> dx >> dy;
ls.push_back(Line(Point(asx, asy), Point(dx, dy)));
sum += (abs(Point(asx, asy) - Point(dx, dy)));
}
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < ls.size(); ++j) {
bool ok = false;
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (ls[i][k] == ls[j][l]) {
ok = true;
crss.push_back(make_pair(ls[i][k], false));
break;
}
}
if (ok)
break;
}
if (!ok && isis_ss(ls[i], ls[j])) {
crss.push_back(make_pair(is_ll(ls[i], ls[j]), false));
}
}
}
for (int i = 0; i < ls.size(); ++i) {
crss.push_back(make_pair(ls[i][0], false));
crss.push_back(make_pair(ls[i][1], false));
}
vector<Point> bs;
for (int i = 0; i < M; ++i) {
int x, y;
cin >> x >> y;
Point p(x, y);
bs.push_back(p);
}
cin >> sx >> sy >> gx >> gy;
start = Point(sx, sy);
goal = Point(gx, gy);
crss.push_back(make_pair(start, false));
crss.push_back(make_pair(goal, false));
sort(crss.begin(), crss.end());
crss.erase(unique(crss.begin(), crss.end()), crss.end());
for (int i = 0; i < M; ++i) {
auto it =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == bs[i]; });
if (it == crss.end()) {
crss.push_back(make_pair(bs[i], true));
} else {
(*it).second = true;
}
}
for (int i = 0; i < crss.size(); ++i) {
bulbs.push_back(crss[i].second);
}
g = segment_arrangement(ls, crss);
}
auto startit =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == start; });
startid = startit - crss.begin();
auto goalit =
find_if(crss.begin(), crss.end(),
[=](const pair<Point, bool> &a) { return a.first == goal; });
goalid = goalit - crss.begin();
vector<bool> close(crss.size(), false);
vector<bool> came(crss.size(), false);
if (dfs0(g, goalid, came, close, bulbs)) {
vector<bool> acame(crss.size(), false);
vector<bool> useedges(80000, false);
double ans = dfs1(g, startid, acame, useedges, close);
cout << fixed << setprecision(10) << sum - ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
|
replace
| 591 | 592 | 591 | 592 |
0
| |
p01570
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
int n;
scanf("%d", &n);
static vector<int> G[160000], G_rev[160000];
static int cost[160000];
rep(u, n) {
int v;
scanf("%d%d", cost + u, &v);
if (v != -1) {
G[v].push_back(u);
G_rev[u].push_back(v);
}
}
vector<int> topo; // topological order
queue<int> Q;
rep(u, n) if (G_rev[u].empty()) {
Q.push(u);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
topo.push_back(v);
rep(i, G[v].size()) Q.push(G[v][i]);
}
}
ll ans = 0;
for (int i = (int)topo.size() - 1; i >= 0; i--) {
int u = topo[i];
vector<int> C(G[u].size()); // 頂点 u の子たちの cost
rep(j, G[u].size()) C[j] = cost[G[u][j]];
sort(C.begin(), C.end());
ll sum = 0;
rep(j, C.size()) {
ans += sum;
sum += C[j];
}
cost[u] += sum;
}
printf("%lld\n", ans);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
int n;
scanf("%d", &n);
static vector<int> G[1000000], G_rev[1000000];
static int cost[1000000];
rep(u, n) {
int v;
scanf("%d%d", cost + u, &v);
if (v != -1) {
G[v].push_back(u);
G_rev[u].push_back(v);
}
}
vector<int> topo; // topological order
queue<int> Q;
rep(u, n) if (G_rev[u].empty()) {
Q.push(u);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
topo.push_back(v);
rep(i, G[v].size()) Q.push(G[v][i]);
}
}
ll ans = 0;
for (int i = (int)topo.size() - 1; i >= 0; i--) {
int u = topo[i];
vector<int> C(G[u].size()); // 頂点 u の子たちの cost
rep(j, G[u].size()) C[j] = cost[G[u][j]];
sort(C.begin(), C.end());
ll sum = 0;
rep(j, C.size()) {
ans += sum;
sum += C[j];
}
cost[u] += sum;
}
printf("%lld\n", ans);
return 0;
}
|
replace
| 13 | 15 | 13 | 15 |
0
| |
p01570
|
C++
|
Runtime Error
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[200000];
bool root[200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[1200000];
bool root[1200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
replace
| 30 | 32 | 30 | 32 |
0
| |
p01570
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[1200000];
bool root[1200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
while (n > 560000)
;
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[1200000];
bool root[1200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
while (n > 1000000)
;
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
replace
| 37 | 38 | 37 | 38 |
TLE
| |
p01570
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[1200000];
bool root[1200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
while (n > 300000)
;
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef vector<int> Edges;
typedef vector<Edges> Graph;
int n;
ll len[1200000];
bool root[1200000];
Graph g;
vector<int> order;
int main() {
while (scanf("%d", &n) > 0) {
while (n > 2000000)
;
MEMSET(root, false);
g = Graph(n);
REP(i, n) {
int p;
scanf("%lld %d", &len[i], &p);
if (p != -1) {
g[p].push_back(i);
} else {
root[i] = true;
}
}
REP(i, n) {
if (!root[i]) {
continue;
}
queue<int> que;
que.push(i);
while (!que.empty()) {
int from = que.front();
que.pop();
order.push_back(from);
FORIT(it, g[from]) {
int to = *it;
que.push(to);
}
}
}
reverse(order.begin(), order.end());
ll ans = 0;
REP(i, n) {
int from = order[i];
vector<int> lens;
FORIT(it, g[from]) { lens.push_back(len[*it]); }
sort(lens.begin(), lens.end());
ll sum = 0;
FORIT(it, lens) {
ans += sum;
sum += *it;
}
len[from] += sum;
}
printf("%lld\n", ans);
}
}
|
replace
| 37 | 38 | 37 | 38 |
TLE
| |
p01570
|
C++
|
Runtime Error
|
#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
for (int i = 0; i < g[v].size(); i++)
l[v] += DFS(g[v][i]);
return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int b;
scanf("%d%d", &l[i], &b);
if (b != -1) {
g[b].push_back(i);
vis[i] = 1;
}
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
S[0] = i;
stage[0] = i;
top = 0;
while (top != -1) {
int cur = S[top];
if (stage[top] == g[cur].size()) {
top--;
if (top != -1) {
l[S[top]] += l[S[top + 1]];
// printf("%d-%d\n",S[top],S[top+1]);
}
} else {
S[top + 1] = g[cur][stage[top]];
stage[top]++;
stage[++top] = 0;
}
}
}
for (int i = 0; i < n; i++)
sort(g[i].begin(), g[i].end(), cmp);
for (int i = 0; i < n; i++) {
sum = 0;
for (int j = 0; j < g[i].size(); j++) {
ans += sum;
sum += l[g[i][j]];
}
}
// for (int i=0;i<n;i++) printf("%d ",l[i]);
// printf("\n");
printf("%lld\n", ans);
return 0;
}
|
#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
for (int i = 0; i < g[v].size(); i++)
l[v] += DFS(g[v][i]);
return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int b;
scanf("%d%d", &l[i], &b);
if (b != -1) {
g[b].push_back(i);
vis[i] = 1;
}
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
S[0] = i;
stage[0] = 0;
top = 0;
while (top != -1) {
int cur = S[top];
if (stage[top] == g[cur].size()) {
top--;
if (top != -1) {
l[S[top]] += l[S[top + 1]];
// printf("%d-%d\n",S[top],S[top+1]);
}
} else {
S[top + 1] = g[cur][stage[top]];
stage[top]++;
stage[++top] = 0;
}
}
}
for (int i = 0; i < n; i++)
sort(g[i].begin(), g[i].end(), cmp);
for (int i = 0; i < n; i++) {
sum = 0;
for (int j = 0; j < g[i].size(); j++) {
ans += sum;
sum += l[g[i][j]];
}
}
// for (int i=0;i<n;i++) printf("%d ",l[i]);
// printf("\n");
printf("%lld\n", ans);
return 0;
}
|
replace
| 27 | 28 | 27 | 28 |
0
| |
p01571
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int edit_distance(const string &str1, const string &str2) {
int dp[1000][1000] = {};
REP(i, str1.size() + 1) dp[i][0] = i;
REP(i, str2.size() + 1) dp[0][i] = i;
REP(i, str1.size()) REP(j, str2.size()) {
dp[i + 1][j + 1] = min(min(dp[i][j + 1], dp[i + 1][j]) + 1,
dp[i][j] + (str1[i] == str2[j] ? 0 : 1));
}
return dp[str1.size()][str2.size()];
}
typedef int Weight;
struct Edge {
int src, dst;
Weight cap, cost;
int rev;
Edge(int src, int dst, Weight cap, Weight cost, int rev = -1)
: src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void add_edge(Graph &G, int src, int dst, Weight cap, Weight cost) {
G[src].push_back(Edge(src, dst, cap, cost, G[dst].size()));
G[dst].push_back(Edge(dst, src, 0, -cost, G[src].size() - 1));
}
Weight min_cost_flow(Graph &G, int s, int t, Weight f) {
typedef pair<Weight, int> P;
Weight res = 0;
int V = G.size();
vector<Weight> h(V, 0);
vector<int> prevv(V);
vector<int> preve(V);
if (s == t)
return 0;
bool start = true;
while (f > 0) {
vector<Weight> dist(V, INF);
if (start) {
start = false;
bool update = true;
dist[s] = 0;
while (update) {
update = false;
REP(v, V) {
if (dist[v] == INF)
continue;
REP(i, G[v].size()) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
dist[e.dst] = dist[v] + e.cost;
prevv[e.dst] = v;
preve[e.dst] = i;
update = true;
}
}
}
}
} else {
priority_queue<P, vector<P>, greater<P>> que;
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
REP(i, G[v].size()) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
prevv[e.dst] = v;
preve[e.dst] = i;
que.push(P(dist[e.dst], e.dst));
}
}
}
}
if (dist[t] == INF)
return -1;
REP(v, V) h[v] += dist[v];
Weight d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
int N, M;
while (cin >> N >> M && N) {
map<string, int> words;
cin.ignore(); // '\n'
REP(i, N) {
string line;
getline(cin, line);
string w;
stringstream ss(line);
while (ss >> w) {
words[w] += 1;
}
}
vector<string> ws;
FORIT(it, words) ws.push_back(it->first);
vector<string> dict;
REP(i, M) {
string w;
cin >> w;
dict.push_back(w);
}
N = ws.size();
int S = N + M;
int T = S + 1;
Graph G(T + 1);
REP(i, N) { add_edge(G, S, i, 1, 0); }
REP(i, M) { add_edge(G, N + i, T, 1, 0); }
REP(i, N) REP(j, M) {
add_edge(G, i, N + j, 1, words[ws[i]] * edit_distance(ws[i], dict[j]));
}
cout << min_cost_flow(G, S, T, N) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int edit_distance(const string &str1, const string &str2) {
int dp[21][21] = {};
REP(i, str1.size() + 1) dp[i][0] = i;
REP(i, str2.size() + 1) dp[0][i] = i;
REP(i, str1.size()) REP(j, str2.size()) {
dp[i + 1][j + 1] = min(min(dp[i][j + 1], dp[i + 1][j]) + 1,
dp[i][j] + (str1[i] == str2[j] ? 0 : 1));
}
return dp[str1.size()][str2.size()];
}
typedef int Weight;
struct Edge {
int src, dst;
Weight cap, cost;
int rev;
Edge(int src, int dst, Weight cap, Weight cost, int rev = -1)
: src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void add_edge(Graph &G, int src, int dst, Weight cap, Weight cost) {
G[src].push_back(Edge(src, dst, cap, cost, G[dst].size()));
G[dst].push_back(Edge(dst, src, 0, -cost, G[src].size() - 1));
}
Weight min_cost_flow(Graph &G, int s, int t, Weight f) {
typedef pair<Weight, int> P;
Weight res = 0;
int V = G.size();
vector<Weight> h(V, 0);
vector<int> prevv(V);
vector<int> preve(V);
if (s == t)
return 0;
bool start = true;
while (f > 0) {
vector<Weight> dist(V, INF);
if (start) {
start = false;
bool update = true;
dist[s] = 0;
while (update) {
update = false;
REP(v, V) {
if (dist[v] == INF)
continue;
REP(i, G[v].size()) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
dist[e.dst] = dist[v] + e.cost;
prevv[e.dst] = v;
preve[e.dst] = i;
update = true;
}
}
}
}
} else {
priority_queue<P, vector<P>, greater<P>> que;
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
REP(i, G[v].size()) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
prevv[e.dst] = v;
preve[e.dst] = i;
que.push(P(dist[e.dst], e.dst));
}
}
}
}
if (dist[t] == INF)
return -1;
REP(v, V) h[v] += dist[v];
Weight d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
int N, M;
while (cin >> N >> M && N) {
map<string, int> words;
cin.ignore(); // '\n'
REP(i, N) {
string line;
getline(cin, line);
string w;
stringstream ss(line);
while (ss >> w) {
words[w] += 1;
}
}
vector<string> ws;
FORIT(it, words) ws.push_back(it->first);
vector<string> dict;
REP(i, M) {
string w;
cin >> w;
dict.push_back(w);
}
N = ws.size();
int S = N + M;
int T = S + 1;
Graph G(T + 1);
REP(i, N) { add_edge(G, S, i, 1, 0); }
REP(i, M) { add_edge(G, N + i, T, 1, 0); }
REP(i, N) REP(j, M) {
add_edge(G, i, N + j, 1, words[ws[i]] * edit_distance(ws[i], dict[j]));
}
cout << min_cost_flow(G, S, T, N) << endl;
}
return 0;
}
|
replace
| 38 | 39 | 38 | 39 |
TLE
| |
p01591
|
C++
|
Runtime Error
|
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define REP(i, b, n) for (int i = b; i < n; i++)
typedef long double D;
typedef pair<D, D> P;
#define sq(x) ((x) * (x))
typedef vector<D> Vector;
typedef vector<Vector> Matrix;
const D EPS = 1e-8;
Vector gauss_jordan(const Matrix &A, const Vector &b) {
int n = A.size();
Matrix B(n, Vector(n + 1));
rep(i, n) {
rep(j, n) { B[i][j] = A[i][j]; }
}
rep(i, n) { B[i][n] = b[i]; }
rep(i, n) {
int pivot = i;
REP(j, i, n) {
if (abs(B[j][i]) > abs(B[pivot][i])) {
pivot = j;
}
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS)
return Vector();
REP(j, i + 1, n + 1) B[i][j] /= B[i][i];
rep(j, n) {
if (i != j) {
REP(k, i + 1, n + 1) { B[j][k] -= B[j][i] * B[i][k]; }
}
}
}
Vector x(n);
rep(i, n) x[i] = B[i][n];
return x;
}
int main() {
int n;
cin >> n;
vector<P> vp(4);
rep(i, n) cin >> vp[i].first >> vp[i].second;
Matrix M;
rep(i, 3) {
Vector V(3);
M.push_back(V);
}
Vector V(3);
rep(i, n) {
M[0][0] += 2 * vp[i].first * vp[i].first;
M[0][1] += 2 * vp[i].first * vp[i].second;
M[0][2] += 2 * vp[i].first;
V[0] -= 2 * vp[i].first * (sq(vp[i].first) + sq(vp[i].second));
M[1][0] += 2 * vp[i].second * vp[i].first;
M[1][1] += 2 * vp[i].second * vp[i].second;
M[1][2] += 2 * vp[i].second;
V[1] -= 2 * vp[i].second * (sq(vp[i].first) + sq(vp[i].second));
M[2][0] += 2 * vp[i].first;
M[2][1] += 2 * vp[i].second;
M[2][2] += 2;
V[2] -= 2 * (sq(vp[i].first) + sq(vp[i].second));
}
V = gauss_jordan(M, V);
printf("%.12Lf %.12Lf %.12Lf\n", V[0], V[1], V[2]);
return 0;
}
|
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define REP(i, b, n) for (int i = b; i < n; i++)
typedef long double D;
typedef pair<D, D> P;
#define sq(x) ((x) * (x))
typedef vector<D> Vector;
typedef vector<Vector> Matrix;
const D EPS = 1e-8;
Vector gauss_jordan(const Matrix &A, const Vector &b) {
int n = A.size();
Matrix B(n, Vector(n + 1));
rep(i, n) {
rep(j, n) { B[i][j] = A[i][j]; }
}
rep(i, n) { B[i][n] = b[i]; }
rep(i, n) {
int pivot = i;
REP(j, i, n) {
if (abs(B[j][i]) > abs(B[pivot][i])) {
pivot = j;
}
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS)
return Vector();
REP(j, i + 1, n + 1) B[i][j] /= B[i][i];
rep(j, n) {
if (i != j) {
REP(k, i + 1, n + 1) { B[j][k] -= B[j][i] * B[i][k]; }
}
}
}
Vector x(n);
rep(i, n) x[i] = B[i][n];
return x;
}
int main() {
int n;
cin >> n;
vector<P> vp(n);
rep(i, n) cin >> vp[i].first >> vp[i].second;
Matrix M;
rep(i, 3) {
Vector V(3);
M.push_back(V);
}
Vector V(3);
rep(i, n) {
M[0][0] += 2 * vp[i].first * vp[i].first;
M[0][1] += 2 * vp[i].first * vp[i].second;
M[0][2] += 2 * vp[i].first;
V[0] -= 2 * vp[i].first * (sq(vp[i].first) + sq(vp[i].second));
M[1][0] += 2 * vp[i].second * vp[i].first;
M[1][1] += 2 * vp[i].second * vp[i].second;
M[1][2] += 2 * vp[i].second;
V[1] -= 2 * vp[i].second * (sq(vp[i].first) + sq(vp[i].second));
M[2][0] += 2 * vp[i].first;
M[2][1] += 2 * vp[i].second;
M[2][2] += 2;
V[2] -= 2 * (sq(vp[i].first) + sq(vp[i].second));
}
V = gauss_jordan(M, V);
printf("%.12Lf %.12Lf %.12Lf\n", V[0], V[1], V[2]);
return 0;
}
|
replace
| 58 | 59 | 58 | 59 |
0
| |
p01591
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define tri(v) ((v) * (v) * (v))
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
using namespace std;
const int maxn = 1e4 + 100;
double x[maxn], y[maxn];
const double inf = 1e100;
int main() {
int n, i;
scanf("%d", &n);
double sumx, sumy, sqrx, sqry, sqrxy, triy, trix;
double sumxy = 0, xxy = 0, xyy = 0;
sumx = sumy = sqrx = sqry = sqrxy = 0;
trix = triy = 0;
for (i = 0; i < n; ++i) {
scanf("%lf%lf", &x[i], &y[i]);
sumxy += (x[i] * y[i]);
sumx += x[i];
sumy += y[i];
xyy += x[i] * sqr(y[i]);
xxy += sqr(x[i]) * y[i];
sqrx += sqr(x[i]);
sqry += sqr(y[i]);
sqrxy += (sqr(x[i]) + sqr(y[i]));
triy += tri(y[i]);
trix += tri(x[i]);
}
double C = n * sqrx - sumx * sumx;
double D = n * sumxy - sumx * sumy;
double E = n * trix + n * xyy - sqrxy * sumx;
double G = n * sqry - sumy * sumy;
double H = n * xxy + n * triy - sqrxy * sumy;
double a, b, c;
a = (H * D - E * G) / (C * G - sqr(D));
b = (H * C - E * D) / (sqr(D) - G * C);
c = -(sqrxy + a * sumx + b * sumy) / n;
printf("%.3f %.3f %.3f\n", a, b, c);
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define tri(v) ((v) * (v) * (v))
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
using namespace std;
const int maxn = 4e4 + 100;
double x[maxn], y[maxn];
const double inf = 1e100;
int main() {
int n, i;
scanf("%d", &n);
double sumx, sumy, sqrx, sqry, sqrxy, triy, trix;
double sumxy = 0, xxy = 0, xyy = 0;
sumx = sumy = sqrx = sqry = sqrxy = 0;
trix = triy = 0;
for (i = 0; i < n; ++i) {
scanf("%lf%lf", &x[i], &y[i]);
sumxy += (x[i] * y[i]);
sumx += x[i];
sumy += y[i];
xyy += x[i] * sqr(y[i]);
xxy += sqr(x[i]) * y[i];
sqrx += sqr(x[i]);
sqry += sqr(y[i]);
sqrxy += (sqr(x[i]) + sqr(y[i]));
triy += tri(y[i]);
trix += tri(x[i]);
}
double C = n * sqrx - sumx * sumx;
double D = n * sumxy - sumx * sumy;
double E = n * trix + n * xyy - sqrxy * sumx;
double G = n * sqry - sumy * sumy;
double H = n * xxy + n * triy - sqrxy * sumy;
double a, b, c;
a = (H * D - E * G) / (C * G - sqr(D));
b = (H * C - E * D) / (sqr(D) - G * C);
c = -(sqrxy + a * sumx + b * sumy) / n;
printf("%.3f %.3f %.3f\n", a, b, c);
return 0;
}
|
replace
| 35 | 36 | 35 | 36 |
0
| |
p01600
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
ll dp[1001][1001], K[1001][1001];
int main(void) {
int n;
cin >> n;
vector<pii> v(n);
rep(i, n) cin >> v[i].x >> v[i].y;
fill(dp[0], dp[1001], INT_MAX);
fill(K[0], K[1001], -1);
rep(i, 1001) dp[i][i] = 0, K[i][i] = i;
for (int w = 1; w < n; w++) {
for (int i = 0; i + w <= n; i++) {
for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
K[i][i + w] = s;
}
}
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
ll dp[1002][1002], K[1002][1002];
int main(void) {
int n;
cin >> n;
vector<pii> v(n);
rep(i, n) cin >> v[i].x >> v[i].y;
fill(dp[0], dp[1001], INT_MAX);
fill(K[0], K[1001], -1);
rep(i, 1001) dp[i][i] = 0, K[i][i] = i;
for (int w = 1; w < n; w++) {
for (int i = 0; i + w <= n; i++) {
for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
K[i][i + w] = s;
}
}
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
|
replace
| 24 | 25 | 24 | 25 |
0
| |
p01601
|
C++
|
Runtime Error
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<bool> kai(1001, false);
for (int i = 1; i < 10; i++) {
kai[i] = true;
}
for (int i = 1; i < 10; i++) {
kai[10 * i + i] = true;
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 10; j++) {
kai[101 * i + 10 * j] = true;
kai[1001 * i + 110 * j] = true;
}
}
int ans = n;
int ans2 = n;
while (1) {
if (kai[ans]) {
cout << ans << endl;
break;
}
if (kai[ans2]) {
cout << ans2 << endl;
break;
}
if (ans2 >= 10001)
ans2 = n;
ans--;
ans2++;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<bool> kai(10001, false);
for (int i = 1; i < 10; i++) {
kai[i] = true;
}
for (int i = 1; i < 10; i++) {
kai[10 * i + i] = true;
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 10; j++) {
kai[101 * i + 10 * j] = true;
kai[1001 * i + 110 * j] = true;
}
}
int ans = n;
int ans2 = n;
while (1) {
if (kai[ans]) {
cout << ans << endl;
break;
}
if (kai[ans2]) {
cout << ans2 << endl;
break;
}
if (ans2 >= 10001)
ans2 = n;
ans--;
ans2++;
}
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
-6
|
malloc(): corrupted top size
|
p01601
|
C++
|
Time Limit Exceeded
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int floor_num(int n) {
int ret = 0, i;
if (n == 0) {
return 1;
} else if (n < 0) {
ret++;
}
for (i = 0; n != 0; i++) {
n /= 10;
}
ret += i;
return ret;
}
char *int_to_str(int n) {
int i, tmp, len;
char *str;
if (n == 0) {
str = (char *)malloc(sizeof(char) * 2);
strcpy(str, "0");
}
len = floor_num(n) + 1;
str = (char *)malloc(sizeof(char) * len);
str[len - 1] = '\0';
if (n < 0) {
n = -n;
str[0] = '-';
for (i = len - 2; i > 0; i--) {
str[i] = '0' + n % 10;
n /= 10;
}
} else {
for (i = len - 2; i >= 0; i--) {
str[i] = '0' + n % 10;
n /= 10;
}
}
return str;
}
bool mirror_judge(int n) {
int len, i;
char *str;
str = int_to_str(n);
len = strlen(str);
if (len % 2 == 0) {
for (i = 0; i < len / 2; i++) {
if (str[len / 2 + i] != str[len / 2 - 1 - i]) {
break;
}
}
if (i == len / 2) {
return true;
}
} else {
for (i = 0; i < len / 2; i++) {
if (str[len / 2 + 1 + i] != str[len / 2 - 1 - i]) {
break;
}
}
if (i == len / 2) {
return true;
}
}
return false;
}
int main(void) {
int i, j, n, ans, a1, a2;
scanf("%d", &n);
for (i = n;; i++) {
if (mirror_judge(i)) {
break;
}
}
a1 = i;
for (i = n;; i--) {
if (mirror_judge(i)) {
break;
}
}
a2 = i;
if (a1 - n >= n - a2) {
ans = a2;
} else {
ans = a1;
}
printf("%d\n", ans);
while (1)
;
return 0;
}
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int floor_num(int n) {
int ret = 0, i;
if (n == 0) {
return 1;
} else if (n < 0) {
ret++;
}
for (i = 0; n != 0; i++) {
n /= 10;
}
ret += i;
return ret;
}
char *int_to_str(int n) {
int i, tmp, len;
char *str;
if (n == 0) {
str = (char *)malloc(sizeof(char) * 2);
strcpy(str, "0");
}
len = floor_num(n) + 1;
str = (char *)malloc(sizeof(char) * len);
str[len - 1] = '\0';
if (n < 0) {
n = -n;
str[0] = '-';
for (i = len - 2; i > 0; i--) {
str[i] = '0' + n % 10;
n /= 10;
}
} else {
for (i = len - 2; i >= 0; i--) {
str[i] = '0' + n % 10;
n /= 10;
}
}
return str;
}
bool mirror_judge(int n) {
int len, i;
char *str;
str = int_to_str(n);
len = strlen(str);
if (len % 2 == 0) {
for (i = 0; i < len / 2; i++) {
if (str[len / 2 + i] != str[len / 2 - 1 - i]) {
break;
}
}
if (i == len / 2) {
return true;
}
} else {
for (i = 0; i < len / 2; i++) {
if (str[len / 2 + 1 + i] != str[len / 2 - 1 - i]) {
break;
}
}
if (i == len / 2) {
return true;
}
}
return false;
}
int main(void) {
int i, j, n, ans, a1, a2;
scanf("%d", &n);
for (i = n;; i++) {
if (mirror_judge(i)) {
break;
}
}
a1 = i;
for (i = n;; i--) {
if (mirror_judge(i)) {
break;
}
}
a2 = i;
if (a1 - n >= n - a2) {
ans = a2;
} else {
ans = a1;
}
printf("%d\n", ans);
return 0;
}
|
delete
| 94 | 96 | 94 | 94 |
TLE
| |
p01602
|
C++
|
Runtime Error
|
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
char c;
int x;
cin >> c >> x;
bool flag = true;
if (c == ')' || n == 1) {
cout << "NO" << endl;
return 0;
}
stack<int> st;
st.push(x);
for (int i = 1; i < n; i++) {
cin >> c >> x;
int y = st.top();
st.pop();
if (c == '(') {
st.push(x + y);
} else {
int z = y - x;
if (z < 0) {
flag = false;
break;
} else {
st.push(z);
}
}
}
if (st.top() >= 1) {
flag = false;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
char c;
int x;
cin >> c >> x;
bool flag = true;
if (c == ')' || n == 1) {
cout << "NO" << endl;
return 0;
}
stack<int> st;
st.push(x);
for (int i = 1; i < n; i++) {
cin >> c >> x;
int y = st.top();
st.pop();
if (c == '(') {
st.push(x + y);
} else {
int z = y - x;
if (z < 0) {
flag = false;
break;
} else {
st.push(z);
}
}
}
if (flag && st.top() >= 1) {
flag = false;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
replace
| 34 | 35 | 34 | 35 |
0
| |
p01606
|
C++
|
Runtime Error
|
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<int> ans(N + 1, 0);
ans[0] = W;
for (int i = 1; i <= W / 2; ++i) {
++ans[i + 1];
}
for (int i = W / 2 + 1; i < W; ++i) {
for (int j = 3; j * i <= N; ++j) {
++ans[j * i - W + 1];
--ans[j * i - W + 1 + W - i];
}
}
for (int i = 1; i <= N - W + 2; ++i) {
ans[i] += ans[i - 1];
}
cout << ans[1];
for (int i = 2; i <= N - W + 1; ++i)
cout << " " << ans[i];
cout << endl;
return 0;
}
|
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<int> ans(N + 2, 0);
ans[0] = W;
for (int i = 1; i <= W / 2; ++i) {
++ans[i + 1];
}
for (int i = W / 2 + 1; i < W; ++i) {
for (int j = 3; j * i <= N; ++j) {
++ans[j * i - W + 1];
--ans[j * i - W + 1 + W - i];
}
}
for (int i = 1; i <= N - W + 2; ++i) {
ans[i] += ans[i - 1];
}
cout << ans[1];
for (int i = 2; i <= N - W + 1; ++i)
cout << " " << ans[i];
cout << endl;
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p01606
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
inline vector<int> factorize(int x) {
vector<int> ret;
for (int i = 1; i * i <= x; i++) {
ret.push_back(i);
if (i * i != x)
ret.push_back(x / i);
}
return (ret);
}
int main() {
int N, W;
int cnt = 0;
bool manage[100001] = {0};
scanf("%d %d", &N, &W);
for (int i = 1; i <= W - 1; i++) {
manage[i] = true;
cnt++;
}
for (int i = W; i <= N; i++) {
vector<int> v;
if (i - W) {
v = factorize(i - W);
for (int j = 0; j < v.size(); j++) {
int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
if (mini == 1 || maxi - mini >= 1)
continue;
else {
cnt -= manage[v[j]];
manage[v[j]] = false;
}
}
}
v = factorize(i);
for (int j = 0; j < v.size(); j++) {
int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
if (mini == 1 || maxi - mini >= 1) {
cnt += !manage[v[j]];
manage[v[j]] = true;
}
}
printf("%d%c", cnt, " \n"[i == N]);
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
inline vector<int> factorize(int x) {
vector<int> ret;
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
ret.push_back(i);
if (i * i != x)
ret.push_back(x / i);
}
}
return (ret);
}
int main() {
int N, W;
int cnt = 0;
bool manage[100001] = {0};
scanf("%d %d", &N, &W);
for (int i = 1; i <= W - 1; i++) {
manage[i] = true;
cnt++;
}
for (int i = W; i <= N; i++) {
vector<int> v;
if (i - W) {
v = factorize(i - W);
for (int j = 0; j < v.size(); j++) {
int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
if (mini == 1 || maxi - mini >= 1)
continue;
else {
cnt -= manage[v[j]];
manage[v[j]] = false;
}
}
}
v = factorize(i);
for (int j = 0; j < v.size(); j++) {
int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
if (mini == 1 || maxi - mini >= 1) {
cnt += !manage[v[j]];
manage[v[j]] = true;
}
}
printf("%d%c", cnt, " \n"[i == N]);
}
return (0);
}
|
replace
| 8 | 11 | 8 | 13 |
TLE
| |
p01606
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
vector<int> a(n + 2, 0);
for (int i = 1; i <= w / 2; ++i)
++a[i + 1];
for (int i = w / 2 + 1; i < w; ++i) {
for (int j = 3 * i; j <= n; j += i) {
++a[j - w + 1];
--a[j - i + 1];
}
}
int ret = w;
for (int i = 1; i <= n - w + 1; ++i) {
ret += a[i];
cout << ret;
if (i < n - w + 1)
cout << ' ';
else
cout << endl;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
vector<int> a(2 * n, 0);
for (int i = 1; i <= w / 2; ++i)
++a[i + 1];
for (int i = w / 2 + 1; i < w; ++i) {
for (int j = 3 * i; j <= n; j += i) {
++a[j - w + 1];
--a[j - i + 1];
}
}
int ret = w;
for (int i = 1; i <= n - w + 1; ++i) {
ret += a[i];
cout << ret;
if (i < n - w + 1)
cout << ' ';
else
cout << endl;
}
return 0;
}
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p01606
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) \
for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1ull << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void uniq(T &c) {
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
}
template <class T> string to_s(const T &a) {
ostringstream os;
os << a;
return os.str();
}
template <class T> T to_T(const string &s) {
istringstream is(s);
T res;
is >> res;
return res;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
os << "( " << p.first << ", " << p.second << " )";
return os;
}
template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n)
cout << deli;
}
while (br--)
cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
foreach (it, c) {
cout << *it;
if (++it != c.end())
cout << deli;
--it;
}
while (br--)
cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1)
cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--)
cout << endl;
}
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T> void input(T *a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
void fix_pre(int n) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(10);
}
void fast_io() {
cin.tie(0);
ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)
bool in_rect(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
typedef long long ll;
typedef pair<int, int> pint;
// y(v): v>^< y(^): ^>v<
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const double PI = acos(-1.0);
int main() {
int n, w;
scanf("%d%d", &n, &w);
const int m = ten(5);
static vector<int> div[m + 100];
for (int i = 1; i < w; ++i)
for (int j = 2 * i; j <= n; j += i)
div[j].pb(i);
static int cnt[m + 100];
for (int i = 1; i <= w; ++i)
rep(j, div[i].size())++ cnt[div[i][j]];
int num = w;
vector<int> res;
for (int i = 1; i <= n - w + 1; ++i) {
res.pb(num);
if (cnt[i] < 2)
--num; // i
rep(j, div[i].size()) if (cnt[div[i][j]]-- == 2)-- num;
++num; // i + w
rep(j, div[i + w].size()) if (++cnt[div[i + w][j]] == 2 &&
div[i + w][j] <= i)++ num;
}
rep(i, n) printf("%d%c", res[i], i + 1 == n ? '\n' : ' ');
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) \
for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1ull << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void uniq(T &c) {
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
}
template <class T> string to_s(const T &a) {
ostringstream os;
os << a;
return os.str();
}
template <class T> T to_T(const string &s) {
istringstream is(s);
T res;
is >> res;
return res;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
os << "( " << p.first << ", " << p.second << " )";
return os;
}
template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n)
cout << deli;
}
while (br--)
cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
foreach (it, c) {
cout << *it;
if (++it != c.end())
cout << deli;
--it;
}
while (br--)
cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1)
cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--)
cout << endl;
}
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T> void input(T *a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
void fix_pre(int n) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(10);
}
void fast_io() {
cin.tie(0);
ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)
bool in_rect(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
typedef long long ll;
typedef pair<int, int> pint;
// y(v): v>^< y(^): ^>v<
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const double PI = acos(-1.0);
int main() {
int n, w;
scanf("%d%d", &n, &w);
const int m = ten(5);
static vector<int> div[m + 100];
for (int i = 1; i < w; ++i)
for (int j = 2 * i; j <= n; j += i)
div[j].pb(i);
static int cnt[m + 100];
for (int i = 1; i <= w; ++i)
rep(j, div[i].size())++ cnt[div[i][j]];
int num = w;
vector<int> res;
for (int i = 1; i <= n - w + 1; ++i) {
res.pb(num);
if (cnt[i] < 2)
--num; // i
rep(j, div[i].size()) if (cnt[div[i][j]]-- == 2)-- num;
++num; // i + w
rep(j, div[i + w].size()) if (++cnt[div[i + w][j]] == 2 &&
div[i + w][j] <= i)++ num;
}
rep(i, res.size()) printf("%d%c", res[i], i + 1 == res.size() ? '\n' : ' ');
}
|
replace
| 162 | 163 | 162 | 163 |
0
| |
p01606
|
C++
|
Runtime Error
|
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<int> ans(N + W, 0);
ans[0] = W;
for (int i = 1; i <= W / 2; ++i) {
++ans[i + 1];
}
for (int i = W / 2 + 1; i < W; ++i) {
for (int j = 3; j * i <= N; ++j) {
++ans[j * i - W + 1];
--ans[j * i - W + 1 + W - i];
}
}
for (int i = 1; i <= N - W + 2; ++i) {
ans[i] += ans[i - 1];
}
cout << ans[1];
for (int i = 2; i <= N - W + 1; ++i)
cout << " " << ans[i];
cout << endl;
return 0;
}
|
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<int> ans(2 * (N + W), 0);
ans[0] = W;
for (int i = 1; i <= W / 2; ++i) {
++ans[i + 1];
}
for (int i = W / 2 + 1; i < W; ++i) {
for (int j = 3; j * i <= N; ++j) {
++ans[j * i - W + 1];
--ans[j * i - W + 1 + W - i];
}
}
for (int i = 1; i <= N - W + 2; ++i) {
ans[i] += ans[i - 1];
}
cout << ans[1];
for (int i = 2; i <= N - W + 1; ++i)
cout << " " << ans[i];
cout << endl;
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p01609
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long double ld;
const ld eps = 1e-9;
vector<ld> quadraticEquation(ld a, ld b, ld c) {
ld d = b * b - 4 * a * c;
if (abs(a) < eps)
return {-c / b};
if (abs(d) < eps)
return {(-b + sqrt(d)) / (2 * a)};
if (0 < d) {
int sign = (b >= 0) ? 1 : -1;
ld x1 = (-b - sign * sqrt(d)) / (2.0 * a), x2 = c / (a * x1);
return {x1, x2};
}
return {};
}
ld simpson(ld x0, ld x1, function<ld(ld)> f) {
const int n = 0.5e6;
const ld dx = (x1 - x0) / n;
ld a = f(x0) + f(x0 + dx * n);
for (int i = 1; i < n; i += 2)
a += 4.0 * f(x0 + dx * i);
for (int i = 2; i < n; i += 2)
a += 2.0 * f(x0 + dx * i);
return a * dx / 3.0;
}
ld W, H, N;
ld a[55], p[55], q[55];
ld f(int i, ld x) { return a[i] * (x - p[i]) * (x - p[i]) + q[i]; }
bool inside(ld x, ld y) {
if (x < -eps || W < x - eps)
return false;
if (y < -eps || H < y - eps)
return false;
return true;
}
ld solve() {
vector<ld> es = {0, W};
rep(i, N) {
auto xs = quadraticEquation(a[i], -a[i] * 2 * p[i],
q[i] + a[i] * p[i] * p[i]); // on x axis
for (ld x : xs)
if (inside(x, f(i, x)))
es.push_back(x);
rep(j, i) {
ld aa = a[i] - a[j];
ld bb = -a[i] * 2 * p[i] - (-a[j] * 2 * p[j]);
ld cc = (q[i] + a[i] * p[i] * p[i]) - (q[j] + a[j] * p[j] * p[j]);
auto cs = quadraticEquation(aa, bb, cc);
for (ld x : cs) {
if (inside(x, f(i, x))) {
es.push_back(x);
}
}
}
}
sort(es.begin(), es.end());
ld ans = 0;
rep(i, es.size() - 1) {
ld x1 = es[i], x2 = es[i + 1];
ld m = (x1 + x2) / 2;
int idx = -1;
ld maxy = -1e9;
rep(j, N) {
if (maxy < f(j, m)) {
maxy = f(j, m);
idx = j;
}
}
if (maxy > -eps) {
ans += simpson(x1, x2, [&](ld x) {
ld dy = a[idx] * 2 * (x - p[idx]);
return sqrt(1 + dy * dy);
});
}
}
return ans;
}
int main() {
while (cin >> W >> H >> N) {
rep(i, N) cin >> a[i] >> p[i] >> q[i];
printf("%.10lf\n", (double)solve());
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long double ld;
const ld eps = 1e-9;
vector<ld> quadraticEquation(ld a, ld b, ld c) {
ld d = b * b - 4 * a * c;
if (abs(a) < eps)
return {-c / b};
if (abs(d) < eps)
return {(-b + sqrt(d)) / (2 * a)};
if (0 < d) {
int sign = (b >= 0) ? 1 : -1;
ld x1 = (-b - sign * sqrt(d)) / (2.0 * a), x2 = c / (a * x1);
return {x1, x2};
}
return {};
}
ld simpson(ld x0, ld x1, function<ld(ld)> f) {
const int n = 1e5;
const ld dx = (x1 - x0) / n;
ld a = f(x0) + f(x0 + dx * n);
for (int i = 1; i < n; i += 2)
a += 4.0 * f(x0 + dx * i);
for (int i = 2; i < n; i += 2)
a += 2.0 * f(x0 + dx * i);
return a * dx / 3.0;
}
ld W, H, N;
ld a[55], p[55], q[55];
ld f(int i, ld x) { return a[i] * (x - p[i]) * (x - p[i]) + q[i]; }
bool inside(ld x, ld y) {
if (x < -eps || W < x - eps)
return false;
if (y < -eps || H < y - eps)
return false;
return true;
}
ld solve() {
vector<ld> es = {0, W};
rep(i, N) {
auto xs = quadraticEquation(a[i], -a[i] * 2 * p[i],
q[i] + a[i] * p[i] * p[i]); // on x axis
for (ld x : xs)
if (inside(x, f(i, x)))
es.push_back(x);
rep(j, i) {
ld aa = a[i] - a[j];
ld bb = -a[i] * 2 * p[i] - (-a[j] * 2 * p[j]);
ld cc = (q[i] + a[i] * p[i] * p[i]) - (q[j] + a[j] * p[j] * p[j]);
auto cs = quadraticEquation(aa, bb, cc);
for (ld x : cs) {
if (inside(x, f(i, x))) {
es.push_back(x);
}
}
}
}
sort(es.begin(), es.end());
ld ans = 0;
rep(i, es.size() - 1) {
ld x1 = es[i], x2 = es[i + 1];
ld m = (x1 + x2) / 2;
int idx = -1;
ld maxy = -1e9;
rep(j, N) {
if (maxy < f(j, m)) {
maxy = f(j, m);
idx = j;
}
}
if (maxy > -eps) {
ans += simpson(x1, x2, [&](ld x) {
ld dy = a[idx] * 2 * (x - p[idx]);
return sqrt(1 + dy * dy);
});
}
}
return ans;
}
int main() {
while (cin >> W >> H >> N) {
rep(i, N) cin >> a[i] >> p[i] >> q[i];
printf("%.10lf\n", (double)solve());
}
}
|
replace
| 21 | 22 | 21 | 22 |
TLE
| |
p01609
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// 2次方程式 ax^2 + bx + c = 0の解 (重解を一つにまとめる)
vector<double> quadratic(double a, double b, double c) {
// assert(a != 0);
if (abs(a) < EPS) {
if (abs(b) < EPS)
return vector<double>();
return vector<double>(1, -c / b);
}
double D = b * b - 4 * a * c;
if (D < 0)
return vector<double>();
if (D == 0)
return vector<double>(1, -b / (2.0 * a));
// |b| >> |ac|の時の桁落ちを避けるために
// x_1 = (-b-sign(b)*sqrt(D))/(2*a), x_2 = c / (a*x_1)を利用する
vector<double> res;
int sign = (b >= 0) ? 1 : -1;
double x1 = (-b - sign * sqrt(D)) / (2.0 * a);
double x2 = c / (a * x1);
res.push_back(x1);
res.push_back(x2);
return res;
}
double f(double a, double b, double c, double x) {
return a * x * x + b * x + c;
}
double g(double A, double B, double x) {
return sqrt(1 + (A * x + B) * (A * x + B));
}
int main() {
double W, H;
int N;
while (cin >> W >> H >> N) {
double a[50], b[50], c[50];
vector<double> inter_point;
REP(i, N) {
double p, q;
cin >> a[i] >> p >> q;
b[i] = -2 * a[i] * p;
c[i] = a[i] * p * p + q;
vector<double> v = quadratic(a[i], b[i], c[i]);
REP(i, v.size()) {
double x = v[i];
x = max(0.0, x);
x = min(W, x);
bool ok = true;
REP(i, inter_point.size())
if (abs(x - inter_point[i]) < EPS) ok = false;
if (ok)
inter_point.insert(
lower_bound(inter_point.begin(), inter_point.end(), x), x);
}
}
REP(i, N) FOR(j, i + 1, N) {
vector<double> v = quadratic(a[i] - a[j], b[i] - b[j], c[i] - c[j]);
REP(k, v.size()) {
double x = v[k];
x = max(0.0, x);
x = min(W, x);
bool ok = true;
REP(i, inter_point.size())
if (abs(x - inter_point[i]) < EPS) ok = false;
if (ok)
inter_point.insert(
lower_bound(inter_point.begin(), inter_point.end(), x), x);
}
}
double ans = 0;
REP(i, inter_point.size() - 1) {
double lb = inter_point[i], ub = inter_point[i + 1];
double mid = (lb + ub) / 2;
int idx = 0;
REP(j, N) if (f(a[idx], b[idx], c[idx], mid) < f(a[j], b[j], c[j], mid)) {
idx = j;
}
assert(f(a[idx], b[idx], c[idx], lb + EPS) >= -EPS);
assert(f(a[idx], b[idx], c[idx], ub - EPS) >= -EPS);
// printf("[%lf, %lf] idx = %d\n", lb, ub, idx);
double A = 2 * a[idx];
double B = b[idx];
const int N = 10000;
double delta = (ub - lb) / N;
double S = 0;
S += g(A, B, lb) + g(A, B, ub);
for (int i = 1; i < N; i += 2) {
S += 4.0 * g(A, B, lb + delta * i);
}
for (int i = 2; i < N; i += 2) {
S += 2.0 * g(A, B, lb + delta * i);
}
ans += S * delta / 3.0;
}
assert(!isnan(ans));
printf("%.16lf\n", ans);
}
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// 2次方程式 ax^2 + bx + c = 0の解 (重解を一つにまとめる)
vector<double> quadratic(double a, double b, double c) {
// assert(a != 0);
if (abs(a) < EPS) {
if (abs(b) < EPS)
return vector<double>();
return vector<double>(1, -c / b);
}
double D = b * b - 4 * a * c;
if (D < 0)
return vector<double>();
if (D == 0)
return vector<double>(1, -b / (2.0 * a));
// |b| >> |ac|の時の桁落ちを避けるために
// x_1 = (-b-sign(b)*sqrt(D))/(2*a), x_2 = c / (a*x_1)を利用する
vector<double> res;
int sign = (b >= 0) ? 1 : -1;
double x1 = (-b - sign * sqrt(D)) / (2.0 * a);
double x2 = c / (a * x1);
res.push_back(x1);
res.push_back(x2);
return res;
}
double f(double a, double b, double c, double x) {
return a * x * x + b * x + c;
}
double g(double A, double B, double x) {
return sqrt(1 + (A * x + B) * (A * x + B));
}
int main() {
double W, H;
int N;
while (cin >> W >> H >> N) {
double a[50], b[50], c[50];
vector<double> inter_point;
REP(i, N) {
double p, q;
cin >> a[i] >> p >> q;
b[i] = -2 * a[i] * p;
c[i] = a[i] * p * p + q;
vector<double> v = quadratic(a[i], b[i], c[i]);
REP(i, v.size()) {
double x = v[i];
x = max(0.0, x);
x = min(W, x);
bool ok = true;
REP(i, inter_point.size())
if (abs(x - inter_point[i]) < EPS) ok = false;
if (ok)
inter_point.insert(
lower_bound(inter_point.begin(), inter_point.end(), x), x);
}
}
REP(i, N) FOR(j, i + 1, N) {
vector<double> v = quadratic(a[i] - a[j], b[i] - b[j], c[i] - c[j]);
REP(k, v.size()) {
double x = v[k];
x = max(0.0, x);
x = min(W, x);
bool ok = true;
REP(i, inter_point.size())
if (abs(x - inter_point[i]) < EPS) ok = false;
if (ok)
inter_point.insert(
lower_bound(inter_point.begin(), inter_point.end(), x), x);
}
}
double ans = 0;
REP(i, inter_point.size() - 1) {
double lb = inter_point[i], ub = inter_point[i + 1];
double mid = (lb + ub) / 2;
int idx = 0;
REP(j, N) if (f(a[idx], b[idx], c[idx], mid) < f(a[j], b[j], c[j], mid)) {
idx = j;
}
if (!(f(a[idx], b[idx], c[idx], lb + EPS) >= -EPS))
continue;
if (!(f(a[idx], b[idx], c[idx], ub - EPS) >= -EPS))
continue;
// printf("[%lf, %lf] idx = %d\n", lb, ub, idx);
double A = 2 * a[idx];
double B = b[idx];
const int N = 10000;
double delta = (ub - lb) / N;
double S = 0;
S += g(A, B, lb) + g(A, B, ub);
for (int i = 1; i < N; i += 2) {
S += 4.0 * g(A, B, lb + delta * i);
}
for (int i = 2; i < N; i += 2) {
S += 2.0 * g(A, B, lb + delta * i);
}
ans += S * delta / 3.0;
}
assert(!isnan(ans));
printf("%.16lf\n", ans);
}
return 0;
}
|
replace
| 114 | 116 | 114 | 118 |
0
| |
p01610
|
C++
|
Time Limit Exceeded
|
#include <cstdio>
using namespace std;
char cmd[7] = {"UFRDBL"};
int dy[6][6][8] = {{{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, -1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 2, 0, 0, 0, 0},
{0, -1, 0, 1, 2, 0, 0, 0},
{0, -2, -1, 0, 1, 0, 0, 0},
{0, 0, -2, -1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, -2, 0, 1, 3, 0},
{0, 0, 0, -2, -1, 0, 1, 0},
{0, 0, 0, -2, 0, 0, 0, 0},
{0, 0, 0, -2, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, -1, 0, 0, 0, 0, 0}},
{{0, 0, 3, 2, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{3, 0, 0, 0, 0, -2, 0, 1},
{2, 0, 0, 0, 0, -3, -1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, -2, -3, 0, 0, 0, 0}},
{{0, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0},
{0, 1, 2, 0, 0, 0, 0, -1},
{-1, 0, 2, 0, 0, 0, 0, -3},
{0, 0, -1, 0, 0, 0, 0, 0},
{0, 0, -3, 0, 0, 0, 0, 0}}};
int dx[6 /*コマンド数*/][6 /*縦*/][8 /*横*/] = {{{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, -1, 0, 0, 0, 0},
{6, 6, -2, -2, -2, -2, -2, -2},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 1, 0, 0, 0, 0},
{0, 2, 1, 0, -1, 0, 0, 0},
{0, 1, 0, -1, -2, 0, 0, 0},
{0, 0, -1, -2, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, -3, 0},
{0, 0, 0, 0, 0, -1, -3, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 2, -6, -6},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, -1, 0, 0, 0, 0}},
{{0, 0, -2, -3, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, -3, 1, 0},
{3, 0, 0, 0, 0, -2, 0, -1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 2, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, -5},
{0, -1, 0, 0, 0, 0, 0, -5},
{0, 0, 5, 0, 0, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 0, 0}}};
void turn(char table[6][9], int icmd) {
char tmp[6][9], i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 8; j++) {
tmp[i + dy[icmd][i][j]][j + dx[icmd][i][j]] = table[i][j];
}
tmp[i][j] = '\0';
}
for (i = 0; i < 6; i++) {
for (j = 0; j < 8; j++) {
table[i][j] = tmp[i][j];
}
table[i][j] = '\0';
}
}
int main(void) {
int i;
char c, table[6][9] = {{"..rr...."}, {"..rr...."}, {"ggyybbww"},
{"ggyybbww"}, {"..oo...."}, {"..oo...."}};
while ((c = getchar()) != '\n') {
for (i = 0; i < 6; i++) {
if (cmd[i] == c) {
turn(table, i);
}
}
}
for (i = 0; i < 6; i++) {
printf("%s\n", table[i]);
}
while (1)
;
return 0;
}
|
#include <cstdio>
using namespace std;
char cmd[7] = {"UFRDBL"};
int dy[6][6][8] = {{{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, -1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 2, 0, 0, 0, 0},
{0, -1, 0, 1, 2, 0, 0, 0},
{0, -2, -1, 0, 1, 0, 0, 0},
{0, 0, -2, -1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, -2, 0, 1, 3, 0},
{0, 0, 0, -2, -1, 0, 1, 0},
{0, 0, 0, -2, 0, 0, 0, 0},
{0, 0, 0, -2, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, -1, 0, 0, 0, 0, 0}},
{{0, 0, 3, 2, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{3, 0, 0, 0, 0, -2, 0, 1},
{2, 0, 0, 0, 0, -3, -1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, -2, -3, 0, 0, 0, 0}},
{{0, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0},
{0, 1, 2, 0, 0, 0, 0, -1},
{-1, 0, 2, 0, 0, 0, 0, -3},
{0, 0, -1, 0, 0, 0, 0, 0},
{0, 0, -3, 0, 0, 0, 0, 0}}};
int dx[6 /*コマンド数*/][6 /*縦*/][8 /*横*/] = {{{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, -1, 0, 0, 0, 0},
{6, 6, -2, -2, -2, -2, -2, -2},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 1, 0, 0, 0, 0},
{0, 2, 1, 0, -1, 0, 0, 0},
{0, 1, 0, -1, -2, 0, 0, 0},
{0, 0, -1, -2, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, -3, 0},
{0, 0, 0, 0, 0, -1, -3, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 2, -6, -6},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, -1, 0, 0, 0, 0}},
{{0, 0, -2, -3, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, -3, 1, 0},
{3, 0, 0, 0, 0, -2, 0, -1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 2, 0, 0, 0, 0}},
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, -5},
{0, -1, 0, 0, 0, 0, 0, -5},
{0, 0, 5, 0, 0, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 0, 0}}};
void turn(char table[6][9], int icmd) {
char tmp[6][9], i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 8; j++) {
tmp[i + dy[icmd][i][j]][j + dx[icmd][i][j]] = table[i][j];
}
tmp[i][j] = '\0';
}
for (i = 0; i < 6; i++) {
for (j = 0; j < 8; j++) {
table[i][j] = tmp[i][j];
}
table[i][j] = '\0';
}
}
int main(void) {
int i;
char c, table[6][9] = {{"..rr...."}, {"..rr...."}, {"ggyybbww"},
{"ggyybbww"}, {"..oo...."}, {"..oo...."}};
while ((c = getchar()) != '\n') {
for (i = 0; i < 6; i++) {
if (cmd[i] == c) {
turn(table, i);
}
}
}
for (i = 0; i < 6; i++) {
printf("%s\n", table[i]);
}
return 0;
}
|
delete
| 110 | 112 | 110 | 110 |
TLE
| |
p01612
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#include <cstdio>
#include <queue>
#include <set>
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
using namespace std;
bool one(double x) { return x + 1e-9 > 1.0; }
int main() {
const int n = getInt();
const int m = getInt();
vector<vector<int>> v(n);
vector<vector<int>> r(n);
vector<int> intmp;
vector<int> in(n);
vector<int> out(n);
vector<pair<int, int>> e(m);
map<int, map<int, int>> memo;
REP(i, m) {
const int a = e[i].first = getInt() - 1;
const int b = e[i].second = getInt() - 1;
v[a].push_back(b);
in[b]++;
r[b].push_back(a);
out[a]++;
memo[a][b] = memo[b][a] = i;
}
intmp = in;
vector<int> dist(n);
queue<int> q;
REP(i, n) if (in[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
for (const int next : v[pos]) {
dist[next] = max(dist[next], dist[pos] + 1);
--in[next];
if (in[next] == 0)
q.push(next);
}
}
const int mx = *max_element(dist.begin(), dist.end());
vector<int> chain(m);
vector<int> valid(m);
REP(i, n) if (dist[i] == mx) { valid[i] = 1; }
REP(i, n) if (out[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
for (const int next : r[pos]) {
if (dist[pos] == dist[next] + 1) {
if (valid[pos]) {
valid[next] = 1;
chain[memo[pos][next]] = 1;
}
}
--out[next];
if (out[next] == 0)
q.push(next);
}
}
vector<double> wp(n);
vector<double> w(m);
in = intmp;
int cnt = 0;
REP(i, n) if (in[i] == 0 && valid[i]) { cnt++; }
REP(i, n) if (in[i] == 0 && valid[i]) { wp[i] = 1.0 / cnt; }
REP(i, n) if (in[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
int cnt = 0;
for (const int next : v[pos])
if (chain[memo[pos][next]])
cnt++;
for (const int next : v[pos]) {
if (chain[memo[pos][next]]) {
wp[next] += wp[pos] / cnt;
w[memo[pos][next]] += wp[pos] / cnt;
}
--in[next];
if (in[next] == 0)
q.push(next);
}
}
bool b = false;
REP(i, m) {
if (one(w[i])) {
printf("%d\n", i + 1);
b = true;
}
}
if (!b)
puts("-1");
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#include <cstdio>
#include <queue>
#include <set>
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
using namespace std;
bool one(double x) { return x + 1e-9 > 1.0; }
int main() {
const int n = getInt();
const int m = getInt();
vector<vector<int>> v(n);
vector<vector<int>> r(n);
vector<int> intmp;
vector<int> in(n);
vector<int> out(n);
vector<pair<int, int>> e(m);
map<int, map<int, int>> memo;
REP(i, m) {
const int a = e[i].first = getInt() - 1;
const int b = e[i].second = getInt() - 1;
v[a].push_back(b);
in[b]++;
r[b].push_back(a);
out[a]++;
memo[a][b] = memo[b][a] = i;
}
intmp = in;
vector<int> dist(n);
queue<int> q;
REP(i, n) if (in[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
for (const int next : v[pos]) {
dist[next] = max(dist[next], dist[pos] + 1);
--in[next];
if (in[next] == 0)
q.push(next);
}
}
const int mx = *max_element(dist.begin(), dist.end());
vector<int> chain(m);
vector<int> valid(n);
REP(i, n) if (dist[i] == mx) { valid[i] = 1; }
REP(i, n) if (out[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
for (const int next : r[pos]) {
if (dist[pos] == dist[next] + 1) {
if (valid[pos]) {
valid[next] = 1;
chain[memo[pos][next]] = 1;
}
}
--out[next];
if (out[next] == 0)
q.push(next);
}
}
vector<double> wp(n);
vector<double> w(m);
in = intmp;
int cnt = 0;
REP(i, n) if (in[i] == 0 && valid[i]) { cnt++; }
REP(i, n) if (in[i] == 0 && valid[i]) { wp[i] = 1.0 / cnt; }
REP(i, n) if (in[i] == 0) { q.push(i); }
while (q.size()) {
const int pos = q.front();
q.pop();
int cnt = 0;
for (const int next : v[pos])
if (chain[memo[pos][next]])
cnt++;
for (const int next : v[pos]) {
if (chain[memo[pos][next]]) {
wp[next] += wp[pos] / cnt;
w[memo[pos][next]] += wp[pos] / cnt;
}
--in[next];
if (in[next] == 0)
q.push(next);
}
}
bool b = false;
REP(i, m) {
if (one(w[i])) {
printf("%d\n", i + 1);
b = true;
}
}
if (!b)
puts("-1");
return 0;
}
|
replace
| 63 | 64 | 63 | 64 |
0
| |
p01620
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<char> cmap;
char getrev(char c, int kagi) {
int rst;
if (c >= 'a')
rst = c - 'a';
else
rst = c - 'A' + ('Z' - 'A' + 1);
if (rst - kagi < 0)
return cmap[rst - kagi + cmap.size()];
else
return cmap[rst - kagi];
}
int main() {
for (int i = 'a'; i < 'z' + 1; i++)
cmap.pb(i);
for (int i = 'A'; i < 'Z' + 1; i++)
cmap.pb(i);
while (1) {
int n;
cin >> n;
int klist[100];
REP(i, n) { cin >> klist[i]; }
string ango;
cin >> ango;
int nowkagi = 0;
REP(i, ango.size()) {
cout << getrev(ango[i], klist[nowkagi]);
if (nowkagi == n - 1)
nowkagi = 0;
else
nowkagi++;
}
cout << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<char> cmap;
char getrev(char c, int kagi) {
int rst;
if (c >= 'a')
rst = c - 'a';
else
rst = c - 'A' + ('Z' - 'A' + 1);
if (rst - kagi < 0)
return cmap[rst - kagi + cmap.size()];
else
return cmap[rst - kagi];
}
int main() {
for (int i = 'a'; i < 'z' + 1; i++)
cmap.pb(i);
for (int i = 'A'; i < 'Z' + 1; i++)
cmap.pb(i);
while (1) {
int n;
cin >> n;
if (n == 0)
break;
int klist[100];
REP(i, n) { cin >> klist[i]; }
string ango;
cin >> ango;
int nowkagi = 0;
REP(i, ango.size()) {
cout << getrev(ango[i], klist[nowkagi]);
if (nowkagi == n - 1)
nowkagi = 0;
else
nowkagi++;
}
cout << endl;
}
}
|
insert
| 43 | 43 | 43 | 45 |
TLE
| |
p01622
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define INF 0x33433433
int n;
int rmin;
int rmax;
int rsum;
int wsum;
int r[114514];
int w[114514];
bool G[114514];
void SubsetSum(int N, int *as, int n) {
int maxsum = 1000;
int sum = 0;
fill(G, G + N + 1, false);
G[0] = true;
for (int i = 1; i <= n; i++) {
int lim = max(min(maxsum, N - as[i]), 0);
for (int j = lim; j >= lim - 1000; j--) {
G[j + as[i]] |= G[j];
}
sum += as[i];
maxsum = max(maxsum, sum);
}
}
int main() {
while (1) {
scanf("%d", &n);
if (n == 0)
return 0;
rsum = 0;
wsum = 0;
rmax = -1;
rmin = INF;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &r[i], &w[i]);
rsum += r[i];
wsum += w[i];
rmax = max(rmax, r[i]);
rmin = min(rmin, r[i]);
}
/*if (n > 3) {
printf("%d\n", rsum+wsum);
continue;
}*/
int leftover;
if (rmax >= rsum / 2 + rsum % 2) {
leftover = rmax * 2 - rsum;
} else {
if (rmin < rsum / 3 + (rsum % 3 != 0)) {
printf("%d\n", rsum + wsum);
continue;
}
assert(rsum % 3 == 0); // r = 0 mod 3 if the program reaches here
leftover = rsum / 3;
}
bool should_move = false; // remove w[i] from w if r[i] == rmax
for (int i = 1; i <= n; i++) {
if (r[i] == rmax)
should_move = true;
if (should_move) {
r[i] = r[i + 1];
w[i] = w[i + 1];
}
}
// printf("wsum: %d\n", wsum);
// printf("rsum: %d\n", rsum);
// printf("rmax: %d\n", rmax);
// printf("leftover: %d\n", leftover);
SubsetSum(leftover, w, n - 1); // put wi as many as possible
int ans = -1;
for (int i = leftover; i >= 0; i--) {
if (G[i]) {
// printf("i: %d\n", i);
ans = rsum + wsum + leftover - i;
break;
}
}
assert(ans != -1);
printf("%d\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define INF 0x33433433
int n;
int rmin;
int rmax;
int rsum;
int wsum;
int r[114514];
int w[114514];
bool G[114514];
void SubsetSum(int N, int *as, int n) {
int maxsum = 1000;
int sum = 0;
fill(G, G + N + 1, false);
G[0] = true;
for (int i = 1; i <= n; i++) {
int lim = max(min(maxsum, N - as[i]), 1000);
for (int j = lim; j >= lim - 1000; j--) {
G[j + as[i]] |= G[j];
}
sum += as[i];
maxsum = max(maxsum, sum);
}
}
int main() {
while (1) {
scanf("%d", &n);
if (n == 0)
return 0;
rsum = 0;
wsum = 0;
rmax = -1;
rmin = INF;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &r[i], &w[i]);
rsum += r[i];
wsum += w[i];
rmax = max(rmax, r[i]);
rmin = min(rmin, r[i]);
}
/*if (n > 3) {
printf("%d\n", rsum+wsum);
continue;
}*/
int leftover;
if (rmax >= rsum / 2 + rsum % 2) {
leftover = rmax * 2 - rsum;
} else {
if (rmin < rsum / 3 + (rsum % 3 != 0)) {
printf("%d\n", rsum + wsum);
continue;
}
assert(rsum % 3 == 0); // r = 0 mod 3 if the program reaches here
leftover = rsum / 3;
}
bool should_move = false; // remove w[i] from w if r[i] == rmax
for (int i = 1; i <= n; i++) {
if (r[i] == rmax)
should_move = true;
if (should_move) {
r[i] = r[i + 1];
w[i] = w[i + 1];
}
}
// printf("wsum: %d\n", wsum);
// printf("rsum: %d\n", rsum);
// printf("rmax: %d\n", rmax);
// printf("leftover: %d\n", leftover);
SubsetSum(leftover, w, n - 1); // put wi as many as possible
int ans = -1;
for (int i = leftover; i >= 0; i--) {
if (G[i]) {
// printf("i: %d\n", i);
ans = rsum + wsum + leftover - i;
break;
}
}
assert(ans != -1);
printf("%d\n", ans);
}
}
|
replace
| 21 | 22 | 21 | 22 |
0
| |
p01622
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
int N;
struct Book {
int page;
int write;
};
Book book[1000];
int dp[1000 * 1000 * 30];
int sum = 0;
int deal = 0;
int solve() {
memset(dp, 0, sizeof(dp));
int s = book[N - 1].page * 2 - sum;
// cout << endl<<s << endl<<endl;
dp[0] = -1;
rep(i, 0, N - 1) {
rep(j, 1, s + 1) {
if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
dp[j - book[i].write] <= i && dp[j] == 0)
dp[j] = i + 1;
// cout << dp[j] << "/";
}
// cout << endl << endl;
}
for (int i = s; i >= 0; i--)
if (dp[i] != 0) {
return (s - i);
}
}
int main(void) {
while (true) {
cin >> N;
if (N == 0)
return (0);
int max_n = 0, max_i = 0;
sum = 0;
deal = 0;
rep(i, 0, N) {
cin >> book[i].page >> book[i].write;
sum += book[i].page;
deal += book[i].write;
if (book[i].page > max_n)
max_n = book[i].page, max_i = i;
}
if (sum >= max_n * 2)
cout << sum + deal << endl;
else {
Book tmp = book[max_i];
book[max_i] = book[N - 1];
book[N - 1] = tmp;
cout << sum + deal + solve() << endl;
}
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
int N;
struct Book {
int page;
int write;
};
Book book[1000];
int dp[1000 * 1000 * 3];
int sum = 0;
int deal = 0;
int solve() {
memset(dp, 0, sizeof(dp));
int s = book[N - 1].page * 2 - sum;
// cout << endl<<s << endl<<endl;
dp[0] = -1;
rep(i, 0, N - 1) {
rep(j, 1, s + 1) {
if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
dp[j - book[i].write] <= i && dp[j] == 0)
dp[j] = i + 1;
// cout << dp[j] << "/";
}
// cout << endl << endl;
}
for (int i = s; i >= 0; i--)
if (dp[i] != 0) {
return (s - i);
}
}
int main(void) {
while (true) {
cin >> N;
if (N == 0)
return (0);
int max_n = 0, max_i = 0;
sum = 0;
deal = 0;
rep(i, 0, N) {
cin >> book[i].page >> book[i].write;
sum += book[i].page;
deal += book[i].write;
if (book[i].page > max_n)
max_n = book[i].page, max_i = i;
}
if (sum >= max_n * 2)
cout << sum + deal << endl;
else {
Book tmp = book[max_i];
book[max_i] = book[N - 1];
book[N - 1] = tmp;
cout << sum + deal + solve() << endl;
}
}
}
|
replace
| 17 | 18 | 17 | 18 |
MLE
| |
p01622
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
while (true) {
cin >> n;
s1 = 0;
s2 = 0;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a >> b;
x[i] = make_pair(a, b);
s1 += a;
s2 += b;
}
sort(x, x + n);
if (x[n - 1].first * 2 <= s1) {
cout << s1 + s2 << endl;
} else {
for (int i = 0; i <= s2; i++)
dp[i] = 0;
dp[0] = 1;
int nokori = x[n - 1].first - (s1 - x[n - 1].first);
vector<int> f;
f.push_back(0);
for (int i = 0; i < n - 1; i++) {
vector<int> g;
for (int a1 : f) {
if (dp[a1 + x[i].second] == 0) {
g.push_back(a1 + x[i].second);
dp[a1 + x[i].second] = 1;
}
}
for (int i = 0; i < g.size(); i++)
f.push_back(g[i]);
}
int g = 0;
for (int i = nokori; i >= 0; i--) {
if (dp[i] == 1) {
g = i;
break;
}
}
cout << (nokori - g) + s1 + s2 << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
while (true) {
cin >> n;
s1 = 0;
s2 = 0;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a >> b;
x[i] = make_pair(a, b);
s1 += a;
s2 += b;
}
sort(x, x + n);
if (x[n - 1].first * 2 <= s1) {
cout << s1 + s2 << endl;
} else {
for (int i = 0; i < 1100000; i++)
dp[i] = 0;
dp[0] = 1;
int nokori = x[n - 1].first - (s1 - x[n - 1].first);
vector<int> f;
f.push_back(0);
for (int i = 0; i < n - 1; i++) {
vector<int> g;
for (int a1 : f) {
if (dp[a1 + x[i].second] == 0) {
g.push_back(a1 + x[i].second);
dp[a1 + x[i].second] = 1;
}
}
for (int i = 0; i < g.size(); i++)
f.push_back(g[i]);
}
int g = 0;
for (int i = nokori; i >= 0; i--) {
if (dp[i] == 1) {
g = i;
break;
}
}
cout << (nokori - g) + s1 + s2 << endl;
}
}
return 0;
}
|
replace
| 25 | 26 | 25 | 26 |
TLE
| |
p01624
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
class ParseError {};
struct Result {
int p, val;
Result(int a, int b) : p(a), val(b) {}
};
struct Parser {
int order_dict[256];
Parser() {
memset(order_dict, 0, sizeof(order_dict));
order_dict['*'] = 5;
order_dict['+'] = 4;
order_dict['-'] = 4;
order_dict['&'] = 3;
order_dict['^'] = 2;
order_dict['|'] = 1;
}
int eval(string s) {
Result r = expr(s, 0, 0);
if (r.p != s.size())
throw ParseError();
return r.val;
}
int calc(int a, int b, char c) {
// printf("%d%c%d\n", a, c, b);
if (c == '*')
return a * b;
if (c == '+')
return a + b;
if (c == '-')
return a - b;
if (c == '&')
return a & b;
if (c == '^')
return a ^ b;
if (c == '|')
return a | b;
assert(false);
}
Result expr(string s, int k, int order) {
if (order == 5)
return term(s, k);
Result r = expr(s, k, order + 1);
while (order_dict[s[r.p]] == order + 1) {
Result t = expr(s, r.p + 1, order + 1);
r.val = calc(r.val, t.val, s[r.p]);
r.p = t.p;
}
return r;
}
Result term(string s, int k) {
if (s[k] == '(') {
Result r = expr(s, k + 1, 0);
if (s[r.p++] != ')')
throw ParseError();
return r;
} else if (isdigit(s[k])) {
if (s[k] == '0')
throw ParseError();
int val = 0;
while (isdigit(s[k])) {
val = val * 10 + s[k++] - '0';
}
return Result(k, val);
} else {
throw ParseError();
}
}
bool valid(string s) {
try {
eval(s);
return true;
} catch (...) {
return false;
}
}
};
Parser P;
int dfs(const string &s, int n, int type) {
if (n == 0) {
try {
return P.eval(s);
} catch (...) {
return (type == 0 ? INT_MAX : INT_MIN);
}
}
string cs = "()*+-&^|0123456789";
vector<string> next;
int res = (type == 0 ? INT_MIN : INT_MAX);
// add
for (int i = 0; i <= s.size(); i++) {
for (char c : cs) {
string ns = s.substr(0, i) + string(1, c) + s.substr(i);
next.push_back(ns);
}
}
// delete
for (int i = 0; i < s.size(); i++) {
string ns = s.substr(0, i) + s.substr(i + 1);
next.push_back(ns);
}
for (const string &ns : next) {
// cout << s << "->" << ns << endl;
if (type == 0) {
res = max(res, dfs(ns, n - 1, type ^ 1));
} else {
res = min(res, dfs(ns, n - 1, type ^ 1));
}
}
return res;
}
int main() {
int N;
while (cin >> N && N > 0) {
if (N % 2 == 0)
N = 2;
else
N = 1;
string s;
cin >> s;
cout << dfs(s, N, 0) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
class ParseError {};
struct Result {
int p, val;
Result(int a, int b) : p(a), val(b) {}
};
struct Parser {
int order_dict[256];
Parser() {
memset(order_dict, 0, sizeof(order_dict));
order_dict['*'] = 5;
order_dict['+'] = 4;
order_dict['-'] = 4;
order_dict['&'] = 3;
order_dict['^'] = 2;
order_dict['|'] = 1;
}
int eval(string s) {
Result r = expr(s, 0, 0);
if (r.p != s.size())
throw ParseError();
return r.val;
}
int calc(int a, int b, char c) {
// printf("%d%c%d\n", a, c, b);
if (c == '*')
return a * b;
if (c == '+')
return a + b;
if (c == '-')
return a - b;
if (c == '&')
return a & b;
if (c == '^')
return a ^ b;
if (c == '|')
return a | b;
assert(false);
}
Result expr(string s, int k, int order) {
if (order == 5)
return term(s, k);
Result r = expr(s, k, order + 1);
while (order_dict[s[r.p]] == order + 1) {
Result t = expr(s, r.p + 1, order + 1);
r.val = calc(r.val, t.val, s[r.p]);
r.p = t.p;
}
return r;
}
Result term(string s, int k) {
if (s[k] == '(') {
Result r = expr(s, k + 1, 0);
if (s[r.p++] != ')')
throw ParseError();
return r;
} else if (isdigit(s[k])) {
if (s[k] == '0')
throw ParseError();
int val = 0;
while (isdigit(s[k])) {
val = val * 10 + s[k++] - '0';
}
return Result(k, val);
} else {
throw ParseError();
}
}
bool valid(string s) {
try {
eval(s);
return true;
} catch (...) {
return false;
}
}
};
Parser P;
int dfs(const string &s, int n, int type) {
if (n == 0) {
try {
return P.eval(s);
} catch (...) {
return (type == 0 ? INT_MAX : INT_MIN);
}
}
string cs = "()*+-&^|0123456789";
vector<string> next;
int res = (type == 0 ? INT_MIN : INT_MAX);
// add
for (int i = 0; i <= s.size(); i++) {
for (char c : cs) {
string ns = s.substr(0, i) + string(1, c) + s.substr(i);
next.push_back(ns);
}
}
// delete
for (int i = 0; i < s.size(); i++) {
string ns = s.substr(0, i) + s.substr(i + 1);
next.push_back(ns);
}
for (const string &ns : next) {
// cout << s << "->" << ns << endl;
try {
P.eval(ns);
if (type == 0) {
res = max(res, dfs(ns, n - 1, type ^ 1));
} else {
res = min(res, dfs(ns, n - 1, type ^ 1));
}
} catch (...) {
}
}
return res;
}
int main() {
int N;
while (cin >> N && N > 0) {
if (N % 2 == 0)
N = 2;
else
N = 1;
string s;
cin >> s;
cout << dfs(s, N, 0) << endl;
}
return 0;
}
|
replace
| 108 | 112 | 108 | 116 |
TLE
| |
p01628
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int x[9][9];
int N, M, A[9], P[9], cx, pows[9][9];
int power2(int a, int b) {
int res = 1;
for (int i = 0; i < b; i++) {
res *= a;
}
return res;
}
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
pows[i][j] = power2(i, j);
}
}
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> A[i];
x[i + 1][A[i]] = 1;
}
for (int i = 1; i <= N; i++) {
cx = i;
for (int j = 1; j <= M; j++) {
if (x[j][cx] == 1) {
cx++;
} else if (x[j][cx - 1] == 1) {
cx--;
}
}
P[i] = cx;
}
int Q = M, maxv, S, cy;
int D[9], F[9], y[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
y[i][j] = 0;
for (int i = 0; i < pows[M][M] * 9 / 10; i++) {
if (i >= 1) {
for (int j = 0; j < M; j++)
y[D[j] + 1][A[j]] = 0;
}
for (int j = 0; j < M; j++) {
D[j] = (i / pows[M][j]) % M;
y[D[j] + 1][A[j]]++;
}
for (int j = 0; j < M; j++) {
if (y[D[j] + 1][A[j]] >= 2 || y[D[j] + 1][A[j] + 1] >= 1)
goto E;
}
for (int j = 1; j <= N; j++) {
cy = j;
for (int k = 1; k <= M; k++) {
if (y[k][cy] == 1)
cy++;
else if (y[k][cy - 1] == 1)
cy--;
}
F[j] = cy;
}
for (int j = 1; j <= N; j++) {
if (P[j] != F[j])
goto E;
}
maxv = 0;
for (int j = 1; j <= M; j++) {
S = (i / pows[M][j - 1]) % M;
maxv = max(maxv, S + 1);
}
if (Q > maxv)
Q = maxv;
E:;
}
cout << Q << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int x[9][9];
int N, M, A[9], P[9], cx, pows[9][9];
int power2(int a, int b) {
int res = 1;
for (int i = 0; i < b; i++) {
res *= a;
}
return res;
}
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
pows[i][j] = power2(i, j);
}
}
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> A[i];
x[i + 1][A[i]] = 1;
}
for (int i = 1; i <= N; i++) {
cx = i;
for (int j = 1; j <= M; j++) {
if (x[j][cx] == 1) {
cx++;
} else if (x[j][cx - 1] == 1) {
cx--;
}
}
P[i] = cx;
}
int Q = M, maxv, S, cy;
int D[9], F[9], y[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
y[i][j] = 0;
for (int i = 0; i < pows[M][M] * 3 / 4; i++) {
if (i >= 1) {
for (int j = 0; j < M; j++)
y[D[j] + 1][A[j]] = 0;
}
for (int j = 0; j < M; j++) {
D[j] = (i / pows[M][j]) % M;
y[D[j] + 1][A[j]]++;
}
for (int j = 0; j < M; j++) {
if (y[D[j] + 1][A[j]] >= 2 || y[D[j] + 1][A[j] + 1] >= 1)
goto E;
}
for (int j = 1; j <= N; j++) {
cy = j;
for (int k = 1; k <= M; k++) {
if (y[k][cy] == 1)
cy++;
else if (y[k][cy - 1] == 1)
cy--;
}
F[j] = cy;
}
for (int j = 1; j <= N; j++) {
if (P[j] != F[j])
goto E;
}
maxv = 0;
for (int j = 1; j <= M; j++) {
S = (i / pows[M][j - 1]) % M;
maxv = max(maxv, S + 1);
}
if (Q > maxv)
Q = maxv;
E:;
}
cout << Q << endl;
return 0;
}
|
replace
| 40 | 41 | 40 | 41 |
TLE
| |
p01632
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 300
enum Type {
IN,
OUT,
};
// 辺を表す構造体(行先、容量、逆辺のインデックス)
struct Edge {
Edge(int arg_to, int arg_capacity, int arg_rev_index) {
to = arg_to;
capacity = arg_capacity;
rev_index = arg_rev_index;
}
int to, capacity, rev_index;
};
struct Info {
int from, to;
};
int V;
vector<Edge> G[NUM]; // グラフの隣接リスト表現
int dist[NUM]; // sourceからの距離
int cheked_index[NUM]; // どこまで調べ終わったか
Info info[100];
// fromからtoへ向かう容量capacityの辺をグラフに追加する
void add_edge(int from, int to, int capacity) {
G[from].push_back(Edge(to, capacity, G[to].size()));
G[to].push_back(Edge(from, 0, G[from].size() - 1)); // 逆辺の、初期容量は0
}
// sourceからの最短距離をBFSで計算する
void bfs(int source) {
for (int i = 0; i < V; i++)
dist[i] = -1;
queue<int> Q;
dist[source] = 0;
Q.push(source);
while (!Q.empty()) {
int node_id = Q.front();
Q.pop();
for (int i = 0; i < G[node_id].size(); i++) {
Edge &e = G[node_id][i];
if (e.capacity > 0 &&
dist[e.to] < 0) { // 辺の容量が正で、かつエッジの行先に未訪問の場合
dist[e.to] = dist[node_id] + 1;
Q.push(e.to);
}
}
}
}
// 増加パスをDFSで探す
int dfs(int node_id, int sink, int flow) {
if (node_id == sink)
return flow; // 終点についたらflowをreturn
for (int &i = cheked_index[node_id]; i < G[node_id].size();
i++) { // node_idから出ているエッジを調査
Edge &e = G[node_id][i];
if (e.capacity > 0 &&
dist[node_id] <
dist[e.to]) { // 流せる余裕があり、かつsourceからの距離が増加する方法である場合
int tmp_flow = dfs(e.to, sink, min(flow, e.capacity)); // 流せるだけ流す
if (tmp_flow > 0) { // 流せた場合
e.capacity -= tmp_flow; // 流した分、エッジの容量を削減する
G[e.to][e.rev_index].capacity +=
tmp_flow; // 逆辺の容量を、流した分だけ増加させる
return tmp_flow;
}
}
}
return 0;
}
// sourceからsinkへの最大流を求める
int max_flow(int source, int sink) { // source:始点 sink:終点
int flow = 0, add;
while (true) { // 増加パスが存在する限り、流量を追加し続ける
bfs(source);
if (dist[sink] < 0)
break; // sourceからsinkへと辿り着く残余グラフがない、つまり増加パスが無くなった場合、break
for (int i = 0; i < V; i++)
cheked_index[i] = 0;
while ((add = dfs(source, sink, BIG_NUM)) >
0) { // 増加パスが見つかる間、加算
flow += add;
}
}
return flow;
}
int main() {
int N, M;
scanf("%d %d", &N, &M);
int source = 0, sink = 2 * N;
int index_table[100][2];
for (int i = 0; i < N; i++) {
index_table[i][IN] = 2 * i;
index_table[i][OUT] = 2 * i + 1;
}
V = 2 * N + 1;
for (int i = 0; i < M; i++) {
scanf("%d %d", &info[i].from, &info[i].to);
info[i].from--;
info[i].to--;
if (i == 0) {
printf("No\n");
continue;
}
for (int k = 0; k < NUM; k++)
G[k].clear();
add_edge(index_table[0][IN], index_table[0][OUT], 2); // 行きと帰りで流量2
for (int k = 1; k < N; k++) {
add_edge(index_table[k][IN], index_table[k][OUT],
1); // 各町の入次ノードと出次ノードを結ぶ
}
// 辺を張りなおす
for (int k = 0; k < i; k++) {
add_edge(index_table[info[k].from][OUT], index_table[info[k].to][IN], 1);
add_edge(index_table[info[k].to][OUT], index_table[info[k].from][IN], 1);
}
// 最新の辺は、その両端のノードをsinkへつなぐ
add_edge(index_table[info[i].from][OUT], sink, 1);
add_edge(index_table[info[i].to][OUT], sink, 1);
if (max_flow(source, sink) == 2) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
|
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 300
enum Type {
IN,
OUT,
};
// 辺を表す構造体(行先、容量、逆辺のインデックス)
struct Edge {
Edge(int arg_to, int arg_capacity, int arg_rev_index) {
to = arg_to;
capacity = arg_capacity;
rev_index = arg_rev_index;
}
int to, capacity, rev_index;
};
struct Info {
int from, to;
};
int V;
vector<Edge> G[NUM]; // グラフの隣接リスト表現
int dist[NUM]; // sourceからの距離
int cheked_index[NUM]; // どこまで調べ終わったか
Info info[6000];
// fromからtoへ向かう容量capacityの辺をグラフに追加する
void add_edge(int from, int to, int capacity) {
G[from].push_back(Edge(to, capacity, G[to].size()));
G[to].push_back(Edge(from, 0, G[from].size() - 1)); // 逆辺の、初期容量は0
}
// sourceからの最短距離をBFSで計算する
void bfs(int source) {
for (int i = 0; i < V; i++)
dist[i] = -1;
queue<int> Q;
dist[source] = 0;
Q.push(source);
while (!Q.empty()) {
int node_id = Q.front();
Q.pop();
for (int i = 0; i < G[node_id].size(); i++) {
Edge &e = G[node_id][i];
if (e.capacity > 0 &&
dist[e.to] < 0) { // 辺の容量が正で、かつエッジの行先に未訪問の場合
dist[e.to] = dist[node_id] + 1;
Q.push(e.to);
}
}
}
}
// 増加パスをDFSで探す
int dfs(int node_id, int sink, int flow) {
if (node_id == sink)
return flow; // 終点についたらflowをreturn
for (int &i = cheked_index[node_id]; i < G[node_id].size();
i++) { // node_idから出ているエッジを調査
Edge &e = G[node_id][i];
if (e.capacity > 0 &&
dist[node_id] <
dist[e.to]) { // 流せる余裕があり、かつsourceからの距離が増加する方法である場合
int tmp_flow = dfs(e.to, sink, min(flow, e.capacity)); // 流せるだけ流す
if (tmp_flow > 0) { // 流せた場合
e.capacity -= tmp_flow; // 流した分、エッジの容量を削減する
G[e.to][e.rev_index].capacity +=
tmp_flow; // 逆辺の容量を、流した分だけ増加させる
return tmp_flow;
}
}
}
return 0;
}
// sourceからsinkへの最大流を求める
int max_flow(int source, int sink) { // source:始点 sink:終点
int flow = 0, add;
while (true) { // 増加パスが存在する限り、流量を追加し続ける
bfs(source);
if (dist[sink] < 0)
break; // sourceからsinkへと辿り着く残余グラフがない、つまり増加パスが無くなった場合、break
for (int i = 0; i < V; i++)
cheked_index[i] = 0;
while ((add = dfs(source, sink, BIG_NUM)) >
0) { // 増加パスが見つかる間、加算
flow += add;
}
}
return flow;
}
int main() {
int N, M;
scanf("%d %d", &N, &M);
int source = 0, sink = 2 * N;
int index_table[100][2];
for (int i = 0; i < N; i++) {
index_table[i][IN] = 2 * i;
index_table[i][OUT] = 2 * i + 1;
}
V = 2 * N + 1;
for (int i = 0; i < M; i++) {
scanf("%d %d", &info[i].from, &info[i].to);
info[i].from--;
info[i].to--;
if (i == 0) {
printf("No\n");
continue;
}
for (int k = 0; k < NUM; k++)
G[k].clear();
add_edge(index_table[0][IN], index_table[0][OUT], 2); // 行きと帰りで流量2
for (int k = 1; k < N; k++) {
add_edge(index_table[k][IN], index_table[k][OUT],
1); // 各町の入次ノードと出次ノードを結ぶ
}
// 辺を張りなおす
for (int k = 0; k < i; k++) {
add_edge(index_table[info[k].from][OUT], index_table[info[k].to][IN], 1);
add_edge(index_table[info[k].to][OUT], index_table[info[k].from][IN], 1);
}
// 最新の辺は、その両端のノードをsinkへつなぐ
add_edge(index_table[info[i].from][OUT], sink, 1);
add_edge(index_table[info[i].to][OUT], sink, 1);
if (max_flow(source, sink) == 2) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
|
replace
| 45 | 46 | 45 | 46 |
0
| |
p01637
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll pay[2][1000];
ll lost[2][1000];
int main() {
ll m, r[2], c[2];
cin >> m >> r[0] >> r[1] >> c[0] >> c[1];
rep(j, 2) rep(i, 1000) pay[j][i] = lost[j][i] = 1ll << 60;
rep(i, 2) {
ll Min = 100 * c[i] / r[i];
for (int x = Min; x < Min + 1000; x++) {
ll D = x * r[i] / 100;
D -= c[i];
if (D < 0)
continue;
ll rest = D * 100 / r[i];
pay[i][x - Min] = x;
lost[i][x - Min] = x - rest;
}
}
ll ans = 1ll << 60;
rep(i, 1000) rep(j, 1000) {
if (pay[0][i] + pay[1][j] > m)
continue;
ans = min(ans, lost[0][i] + lost[1][j]);
}
if (m < ans)
cout << -1 << endl;
else
cout << m - ans << endl;
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll pay[2][1000];
ll lost[2][1000];
int main() {
ll m, r[2], c[2];
cin >> m >> r[0] >> r[1] >> c[0] >> c[1];
rep(j, 2) rep(i, 1000) pay[j][i] = lost[j][i] = 1ll << 60;
rep(i, 2) {
ll Min = 100 * c[i] / r[i];
for (ll x = Min; x < Min + 1000; x++) {
ll D = x * r[i] / 100;
D -= c[i];
if (D < 0)
continue;
ll rest = D * 100 / r[i];
pay[i][x - Min] = x;
lost[i][x - Min] = x - rest;
}
}
ll ans = 1ll << 60;
rep(i, 1000) rep(j, 1000) {
if (pay[0][i] + pay[1][j] > m)
continue;
ans = min(ans, lost[0][i] + lost[1][j]);
}
if (m < ans)
cout << -1 << endl;
else
cout << m - ans << endl;
return 0;
}
|
replace
| 40 | 41 | 40 | 41 |
TLE
| |
p01639
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
b--;
unsigned long long x = c;
for (int i = 0; i < a; i++) {
cnt[((long long)x) / (1LL << 47) + 100000]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
int at = 0;
for (int i = 0; i < 200000; i++) {
if (b < cnt[i]) {
at = i;
break;
} else
b -= cnt[i];
}
// printf("%d\n",at);
x = c;
for (int i = 0; i < a; i++) {
if (((long long)x) / (1LL << 47) + 100000 == at) {
v.push_back((long long)x);
}
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
std::sort(v.begin(), v.end());
printf("%lld\n", v[b]);
}
|
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (c == 0) {
printf("0\n");
return 0;
}
b--;
unsigned long long x = c;
for (int i = 0; i < a; i++) {
cnt[((long long)x) / (1LL << 47) + 100000]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
int at = 0;
for (int i = 0; i < 200000; i++) {
if (b < cnt[i]) {
at = i;
break;
} else
b -= cnt[i];
}
// printf("%d\n",at);
x = c;
for (int i = 0; i < a; i++) {
if (((long long)x) / (1LL << 47) + 100000 == at) {
v.push_back((long long)x);
}
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
std::sort(v.begin(), v.end());
printf("%lld\n", v[b]);
}
|
insert
| 9 | 9 | 9 | 13 |
MLE
| |
p01639
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 9223372036854775807
#define DIVISOR 10000000000000000
int main() {
ull count[2000];
for (int i = 0; i < 2000; i++)
count[i] = 0;
ull N, K;
ull x_0;
scanf("%lld %lld %lld", &N, &K, &x_0);
ull x = x_0;
for (int i = 0; i < N; i++) {
count[(ull)(NUM + (ll)x) / DIVISOR]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
int sum = 0;
ull loc;
for (ull i = 0; i < 2000; i++) {
if (sum + count[i] >= K) {
loc = i;
break;
}
sum += count[i];
}
vector<ll> V;
x = x_0;
for (int i = 0; i < N; i++) {
if (loc == (ull)(NUM + (ll)x) / DIVISOR) {
V.push_back(x);
}
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
sort(V.begin(), V.end());
printf("%lld\n", V[(K - sum) - 1]);
return 0;
}
|
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 9223372036854775807
#define DIVISOR 10000000000000000
int main() {
ull count[2000];
for (int i = 0; i < 2000; i++)
count[i] = 0;
ull N, K;
ull x_0;
scanf("%lld %lld %lld", &N, &K, &x_0);
if (x_0 == 0) {
printf("0\n");
return 0;
}
ull x = x_0;
for (int i = 0; i < N; i++) {
count[(ull)(NUM + (ll)x) / DIVISOR]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
int sum = 0;
ull loc;
for (ull i = 0; i < 2000; i++) {
if (sum + count[i] >= K) {
loc = i;
break;
}
sum += count[i];
}
vector<ll> V;
x = x_0;
for (int i = 0; i < N; i++) {
if (loc == (ull)(NUM + (ll)x) / DIVISOR) {
V.push_back(x);
}
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
sort(V.begin(), V.end());
printf("%lld\n", V[(K - sum) - 1]);
return 0;
}
|
insert
| 32 | 32 | 32 | 36 |
MLE
| |
p01639
|
C++
|
Memory Limit Exceeded
|
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull mask = 1LL << 63;
int bm[1 << 16];
int main() {
ll x0, n, k;
cin >> n >> k >> x0;
k--;
ull x = x0;
for (int i = 0; i < n; i++) {
bm[(x + mask) >> 48]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
ll sm = 0;
ll u;
for (u = 0; u < 1 << 16; u++) {
if (k < sm + bm[u])
break;
sm += bm[u];
}
k -= sm;
vector<ll> v;
x = x0;
for (int i = 0; i < n; i++) {
if ((x + mask) >> 48 == u)
v.push_back((ll)x);
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
sort(v.begin(), v.end());
cout << v[k] << endl;
return 0;
}
|
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull mask = 1LL << 63;
int bm[1 << 16];
int main() {
ll x0, n, k;
cin >> n >> k >> x0;
k--;
if (!x0) {
cout << "0" << endl;
return 0;
}
ull x = x0;
for (int i = 0; i < n; i++) {
bm[(x + mask) >> 48]++;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
ll sm = 0;
ll u;
for (u = 0; u < 1 << 16; u++) {
if (k < sm + bm[u])
break;
sm += bm[u];
}
k -= sm;
vector<ll> v;
x = x0;
for (int i = 0; i < n; i++) {
if ((x + mask) >> 48 == u)
v.push_back((ll)x);
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
}
sort(v.begin(), v.end());
cout << v[k] << endl;
return 0;
}
|
insert
| 25 | 25 | 25 | 29 |
MLE
| |
p01639
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
int x, n, k, cnt[1050000];
int main() {
cin >> n >> k >> x;
k--;
ull z1 = x;
for (int i = 0; i < n; i++) {
long long w = z1;
cnt[(w >> 44) + 524288]++;
z1 ^= (z1 << 13);
z1 ^= (z1 >> 7);
z1 ^= (z1 << 17);
}
int sum = 0, r = 0;
for (int i = 0; i < 1048576; i++) {
sum += cnt[i];
if (sum > k) {
r = i;
break;
}
}
int c = k - (sum - cnt[r]);
ull z2 = x;
vector<long long> v;
for (int i = 0; i < n; i++) {
long long w = z2;
if ((w >> 44) + 524288 == r)
v.push_back(z2);
z2 ^= (z2 << 13);
z2 ^= (z2 >> 7);
z2 ^= (z2 << 17);
}
sort(v.begin(), v.end());
cout << v[c] << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
int x, n, k, cnt[1050000];
int main() {
cin >> n >> k >> x;
k--;
if (x == 0) {
cout << 0 << endl;
return 0;
}
ull z1 = x;
for (int i = 0; i < n; i++) {
long long w = z1;
cnt[(w >> 44) + 524288]++;
z1 ^= (z1 << 13);
z1 ^= (z1 >> 7);
z1 ^= (z1 << 17);
}
int sum = 0, r = 0;
for (int i = 0; i < 1048576; i++) {
sum += cnt[i];
if (sum > k) {
r = i;
break;
}
}
int c = k - (sum - cnt[r]);
ull z2 = x;
vector<long long> v;
for (int i = 0; i < n; i++) {
long long w = z2;
if ((w >> 44) + 524288 == r)
v.push_back(z2);
z2 ^= (z2 << 13);
z2 ^= (z2 >> 7);
z2 ^= (z2 << 17);
}
sort(v.begin(), v.end());
cout << v[c] << endl;
return 0;
}
|
insert
| 9 | 9 | 9 | 13 |
MLE
| |
p01646
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
bool dfs(int pos, int start, const vector<vector<int>> &graph,
vector<bool> &visited) {
if (visited[pos])
return false;
visited[pos] = true;
for (int v : graph[pos]) {
if (v == start)
return true;
if (dfs(v, start, graph, visited))
return true;
}
return false;
}
bool solve() {
int N;
if (!(cin >> N))
return false;
if (!N)
return false;
vector<string> v(N);
for (int i = 0; i < N; ++i) {
cin >> v[i];
}
vector<vector<int>> graph(26);
for (int i = 0; i < N - 1; ++i) {
const string &s1 = v[i];
const string &s2 = v[i + 1];
if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
cout << "no" << endl;
return true;
}
for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
if (s1[j] != s2[j]) {
graph[s1[j] - 'a'].push_back(s2[j] - 'a');
break;
}
}
}
for (int i = 0; i < N; ++i) {
vector<bool> visited(26, false);
if (dfs(i, i, graph, visited)) {
cout << "no" << endl;
return true;
}
}
cout << "yes" << endl;
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
while (solve())
;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
bool dfs(int pos, int start, const vector<vector<int>> &graph,
vector<bool> &visited) {
if (visited[pos])
return false;
visited[pos] = true;
for (int v : graph[pos]) {
if (v == start)
return true;
if (dfs(v, start, graph, visited))
return true;
}
return false;
}
bool solve() {
int N;
if (!(cin >> N))
return false;
if (!N)
return false;
vector<string> v(N);
for (int i = 0; i < N; ++i) {
cin >> v[i];
}
vector<vector<int>> graph(26);
for (int i = 0; i < N - 1; ++i) {
const string &s1 = v[i];
const string &s2 = v[i + 1];
if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
cout << "no" << endl;
return true;
}
for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
if (s1[j] != s2[j]) {
graph[s1[j] - 'a'].push_back(s2[j] - 'a');
break;
}
}
}
for (int i = 0; i < 26; ++i) {
vector<bool> visited(26, false);
if (dfs(i, i, graph, visited)) {
cout << "no" << endl;
return true;
}
}
cout << "yes" << endl;
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
while (solve())
;
return 0;
}
|
replace
| 49 | 50 | 49 | 50 |
0
| |
p01646
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<char, char> P;
typedef pair<int, int> Pii;
const int MAX = 30;
vector<Pii> E;
P comp(string a, string b) {
for (int i = 0; i < (int)min(a.length(), b.length()); i++)
if (a[i] != b[i])
return P(a[i], b[i]);
if (a.length() > b.length())
return P(a[b.length()], '?');
if (a.length() < b.length())
return P('?', b[a.length()]);
return P('?', '?');
}
bool add_edge(const vector<string> &v) {
E.clear();
for (int i = 0; i + 1 < (int)v.size(); i++) {
P p = comp(v[i], v[i + 1]);
if (p == P('?', '?'))
continue;
if (p.second == '?')
return false;
if (p.first != '?')
E.push_back(P(p.first - 'a', p.second - 'a'));
}
return true;
}
bool hasCircle(int N) {
vector<int> d(N, 0);
for (int i = 0; i <= N; i++) {
bool update = false;
for (int j = 0; j < (int)E.size(); j++)
if (d[E[j].second] < d[E[j].first] + 1) {
d[E[j].second] = d[E[j].first] + 1;
update = true;
}
if (i == N && update)
return true;
if (!update)
break;
}
return false;
}
bool solve(const vector<string> &v) {
if (!add_edge(v))
return false;
return !hasCircle(v.size());
}
int main() {
int N;
while (cin >> N && N) {
vector<string> v(N);
for (int i = 0; i < N; i++)
cin >> v[i];
cout << (solve(v) ? "yes" : "no") << endl;
}
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<char, char> P;
typedef pair<int, int> Pii;
const int MAX = 30;
vector<Pii> E;
P comp(string a, string b) {
for (int i = 0; i < (int)min(a.length(), b.length()); i++)
if (a[i] != b[i])
return P(a[i], b[i]);
if (a.length() > b.length())
return P(a[b.length()], '?');
if (a.length() < b.length())
return P('?', b[a.length()]);
return P('?', '?');
}
bool add_edge(const vector<string> &v) {
E.clear();
for (int i = 0; i + 1 < (int)v.size(); i++) {
P p = comp(v[i], v[i + 1]);
if (p == P('?', '?'))
continue;
if (p.second == '?')
return false;
if (p.first != '?')
E.push_back(P(p.first - 'a', p.second - 'a'));
}
return true;
}
bool hasCircle(int N) {
vector<int> d(MAX, 0);
for (int i = 0; i <= N; i++) {
bool update = false;
for (int j = 0; j < (int)E.size(); j++)
if (d[E[j].second] < d[E[j].first] + 1) {
d[E[j].second] = d[E[j].first] + 1;
update = true;
}
if (i == N && update)
return true;
if (!update)
break;
}
return false;
}
bool solve(const vector<string> &v) {
if (!add_edge(v))
return false;
return !hasCircle(v.size());
}
int main() {
int N;
while (cin >> N && N) {
vector<string> v(N);
for (int i = 0; i < N; i++)
cin >> v[i];
cout << (solve(v) ? "yes" : "no") << endl;
}
return 0;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p01646
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
typedef long long ll;
int main() {
int n;
char s[256][256];
while (~scanf("%d", &n) && n) {
rep(i, n) scanf("%s", s[i]);
set<char> st[256];
bool ans = true;
rep(i, n) {
REP(j, i + 1, n) {
bool f = true;
int li = strlen(s[i]), lj = strlen(s[j]);
int l = min(li, lj);
rep(k, l) {
if (s[i][k] != s[j][k]) {
f = false;
if (st[s[j][k]].count(s[i][k])) {
ans = false;
break;
}
st[s[i][k]].insert(s[j][k]);
break;
}
}
if (f && li > lj)
ans = false;
}
}
printf("%s\n", (ans ? "yes" : "no"));
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
typedef long long ll;
int main() {
int n;
char s[1024][256];
while (~scanf("%d", &n) && n) {
rep(i, n) scanf("%s", s[i]);
set<char> st[256];
bool ans = true;
rep(i, n) {
REP(j, i + 1, n) {
bool f = true;
int li = strlen(s[i]), lj = strlen(s[j]);
int l = min(li, lj);
rep(k, l) {
if (s[i][k] != s[j][k]) {
f = false;
if (st[s[j][k]].count(s[i][k])) {
ans = false;
break;
}
st[s[i][k]].insert(s[j][k]);
break;
}
}
if (f && li > lj)
ans = false;
}
}
printf("%s\n", (ans ? "yes" : "no"));
}
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p01648
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
vector<int> rank, p;
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
}
public:
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
for (int i = 0; i < size; i++)
p[i] = i, rank[i] = 0;
}
void Union(int x, int y) {
if (Find(x) != Find(y))
link(Find(x), Find(y));
}
int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};
struct edge {
int u, v, cost;
bool operator<(const edge &e) const { return cost < e.cost; }
};
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<edge> edges;
for (int i = 0; i < m; i++) {
int s, t, c;
cin >> s >> t >> c;
edges.push_back((edge){s, t, c});
}
sort(edges.begin(), edges.end());
DisjointSet UnionFind(n);
vector<int> cost;
for (int i = 0; i < edges.size(); i++) {
edge &e = edges[i];
if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
UnionFind.Union(e.u, e.v);
cost.push_back(e.cost);
}
}
if (cost.size() % 2 == 0) {
cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
} else {
cout << cost[cost.size() / 2] << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
vector<int> rank, p;
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
}
public:
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
for (int i = 0; i < size; i++)
p[i] = i, rank[i] = 0;
}
void Union(int x, int y) {
if (Find(x) != Find(y))
link(Find(x), Find(y));
}
int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};
struct edge {
int u, v, cost;
bool operator<(const edge &e) const { return cost < e.cost; }
};
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<edge> edges;
for (int i = 0; i < m; i++) {
int s, t, c;
cin >> s >> t >> c;
edges.push_back((edge){s - 1, t - 1, c});
}
sort(edges.begin(), edges.end());
DisjointSet UnionFind(n);
vector<int> cost;
for (int i = 0; i < edges.size(); i++) {
edge &e = edges[i];
if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
UnionFind.Union(e.u, e.v);
cost.push_back(e.cost);
}
}
if (cost.size() % 2 == 0) {
cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
} else {
cout << cost[cost.size() / 2] << endl;
}
}
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p01648
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
class union_find {
private:
vector<int> parent, rank;
int size;
public:
int cnt;
union_find() = default;
union_find(int n) { init(n); }
void init(int n) {
size = n;
cnt = n;
parent.resize(size);
rank.resize(size);
for (int i = 0; i < size; ++i) {
parent[i] = i;
rank[i] = 0;
}
}
int find(int x) {
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else {
parent[y] = x;
if (rank[x] == rank[y])
++rank[x];
}
--cnt;
}
bool is_same(int x, int y) { return find(x) == find(y); }
};
struct edge {
int s, t, c;
edge(int s, int t, int c) : s(s), t(t), c(c) {}
bool operator<(const edge &other) const { return c < other.c; }
};
void solve() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 and m == 0)
break;
vector<edge> edges;
rep(i, 0, m) {
int s, t, c;
cin >> s >> t >> c;
--s;
--t;
edges.emplace_back(edge(s, t, c));
}
sort(edges.begin(), edges.end());
union_find uf(n);
vector<int> costs;
for (const auto &e : edges)
if (!uf.is_same(e.s, e.t)) {
uf.unite(e.s, e.t);
costs.emplace_back(e.c);
}
cerr << costs << endl;
cout << costs[(n - 2) / 2] << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cout << fixed << setprecision(8);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
class union_find {
private:
vector<int> parent, rank;
int size;
public:
int cnt;
union_find() = default;
union_find(int n) { init(n); }
void init(int n) {
size = n;
cnt = n;
parent.resize(size);
rank.resize(size);
for (int i = 0; i < size; ++i) {
parent[i] = i;
rank[i] = 0;
}
}
int find(int x) {
if (parent[x] == x)
return x;
else
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else {
parent[y] = x;
if (rank[x] == rank[y])
++rank[x];
}
--cnt;
}
bool is_same(int x, int y) { return find(x) == find(y); }
};
struct edge {
int s, t, c;
edge(int s, int t, int c) : s(s), t(t), c(c) {}
bool operator<(const edge &other) const { return c < other.c; }
};
void solve() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 and m == 0)
break;
vector<edge> edges;
rep(i, 0, m) {
int s, t, c;
cin >> s >> t >> c;
--s;
--t;
edges.emplace_back(edge(s, t, c));
}
sort(edges.begin(), edges.end());
union_find uf(n);
vector<int> costs;
for (const auto &e : edges)
if (!uf.is_same(e.s, e.t)) {
uf.unite(e.s, e.t);
costs.emplace_back(e.c);
}
cout << costs[max(0, (n - 2) / 2)] << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cout << fixed << setprecision(8);
solve();
return 0;
}
|
replace
| 106 | 108 | 106 | 107 |
0
|
[5,]
[1,2,3,]
[43,51,345,421,426,582,608,]
|
p01649
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
L(const P &a, const P &b) {
at(0) = a;
at(1) = b;
}
L() {}
};
struct C {
P p;
double r;
C(const P &p, const double &r) : p(p), r(r) {}
C() {}
};
double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return +1; // ccw
if (cross(b, c) < -EPS)
return -1; // cw
if (dot(b, c) < -EPS)
return +2; // c-a-b
if (abs(c) - abs(b) > EPS)
return -2; // a-b-c
return 0; // a-c-b
}
P unit(const P &p) { return p / abs(p); }
double distanceLP(const L &l, const P &p) {
return abs(cross(l[1] - l[0], p - l[0])) / abs(l[1] - l[0]);
}
double distanceSP(const L &s, const P &p) {
if (dot(s[1] - s[0], p - s[0]) < EPS)
return abs(p - s[0]);
if (dot(s[0] - s[1], p - s[1]) < EPS)
return abs(p - s[1]);
return distanceLP(s, p);
}
bool intersectSP(const L &s, const P &p) {
return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < EPS;
}
bool intersectSS(const L &a, const L &b) {
return (ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0) &&
(ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0);
}
P projection(const L &l, const P &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
P reflection(const L &l, const P &p) {
return p + 2.0 * (projection(l, p) - p);
}
bool isParallel(const P &a, const P &b) { return abs(cross(a, b)) < EPS; }
bool isParallel(const L &a, const L &b) {
return isParallel(a[1] - a[0], b[1] - b[0]);
}
P crosspointLL(const L &l, const L &m) {
double A = cross(l[1] - l[0], m[1] - m[0]);
double B = cross(l[1] - l[0], l[1] - m[0]);
return m[0] + B / A * (m[1] - m[0]);
}
VP crosspointCL(const C &c, const L &l) {
VP ret;
P mid = projection(l, c.p);
double d = distanceLP(l, c.p);
if (EQ(d, c.r)) {
ret.push_back(mid);
} else if (d < c.r) {
double len = sqrt(c.r * c.r - d * d);
ret.push_back(mid + len * unit(l[1] - l[0]));
ret.push_back(mid - len * unit(l[1] - l[0]));
}
return ret;
}
VP crosspointCS(const C &c, const L &s) {
VP ret;
VP cp = crosspointCL(c, s);
for (int i = 0; i < (int)cp.size(); i++) {
if (intersectSP(s, cp[i])) {
ret.push_back(cp[i]);
}
}
return ret;
}
int main() {
while (1) {
int n;
cin >> n;
if (n == 0)
break;
double w, h, r, vx, vy;
cin >> w >> h >> r >> vx >> vy;
VP rec{P(r, r), P(w - r, r), P(w - r, h - r), P(r, h - r)};
P dir = unit(P(vx, vy));
VP p(n);
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
p[i] = P(x, y);
}
P pos = p[0];
double limit = 1e4;
int ans = -1;
while (limit > 0) {
P ep = pos + dir * 1e4;
L ray(pos, ep);
bool touch = false;
P cp(INF, INF);
for (int i = 1; i < n; i++) {
if (distanceSP(ray, p[i]) < 2 * r + EPS) {
VP cpcs = crosspointCS(C(p[i], 2 * r), ray);
for (P p : cpcs) {
if (abs(pos - p) < abs(pos - cp)) {
cp = p;
ans = i + 1;
}
}
touch = true;
}
}
if (touch) {
limit -= abs(pos - cp);
break;
}
bool corner = false;
for (int i = 0; i < 4; i++) {
if (intersectSP(ray, rec[i])) {
limit -= 2 * abs(pos - rec[i]);
dir = -dir;
corner = true;
break;
}
}
for (int i = 0; i < 4 && !corner; i++) {
L edge(rec[i], rec[(i + 1) % 4]);
if (!isParallel(ray, edge) && intersectSS(ray, edge)) {
P cpll = crosspointLL(ray, edge);
if (cpll == pos)
continue;
P to = reflection(edge, ep);
limit -= abs(cpll - pos);
pos = cpll;
dir = unit(to - cpll);
break;
}
}
}
if (limit < -EPS || ans == -1) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
L(const P &a, const P &b) {
at(0) = a;
at(1) = b;
}
L() {}
};
struct C {
P p;
double r;
C(const P &p, const double &r) : p(p), r(r) {}
C() {}
};
namespace std {
bool operator<(const P &a, const P &b) {
return (a.X != b.X) ? a.X < b.X : a.Y < b.Y;
}
bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }
} // namespace std
double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return +1; // ccw
if (cross(b, c) < -EPS)
return -1; // cw
if (dot(b, c) < -EPS)
return +2; // c-a-b
if (abs(c) - abs(b) > EPS)
return -2; // a-b-c
return 0; // a-c-b
}
P unit(const P &p) { return p / abs(p); }
double distanceLP(const L &l, const P &p) {
return abs(cross(l[1] - l[0], p - l[0])) / abs(l[1] - l[0]);
}
double distanceSP(const L &s, const P &p) {
if (dot(s[1] - s[0], p - s[0]) < EPS)
return abs(p - s[0]);
if (dot(s[0] - s[1], p - s[1]) < EPS)
return abs(p - s[1]);
return distanceLP(s, p);
}
bool intersectSP(const L &s, const P &p) {
return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < EPS;
}
bool intersectSS(const L &a, const L &b) {
return (ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0) &&
(ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0);
}
P projection(const L &l, const P &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
P reflection(const L &l, const P &p) {
return p + 2.0 * (projection(l, p) - p);
}
bool isParallel(const P &a, const P &b) { return abs(cross(a, b)) < EPS; }
bool isParallel(const L &a, const L &b) {
return isParallel(a[1] - a[0], b[1] - b[0]);
}
P crosspointLL(const L &l, const L &m) {
double A = cross(l[1] - l[0], m[1] - m[0]);
double B = cross(l[1] - l[0], l[1] - m[0]);
return m[0] + B / A * (m[1] - m[0]);
}
VP crosspointCL(const C &c, const L &l) {
VP ret;
P mid = projection(l, c.p);
double d = distanceLP(l, c.p);
if (EQ(d, c.r)) {
ret.push_back(mid);
} else if (d < c.r) {
double len = sqrt(c.r * c.r - d * d);
ret.push_back(mid + len * unit(l[1] - l[0]));
ret.push_back(mid - len * unit(l[1] - l[0]));
}
return ret;
}
VP crosspointCS(const C &c, const L &s) {
VP ret;
VP cp = crosspointCL(c, s);
for (int i = 0; i < (int)cp.size(); i++) {
if (intersectSP(s, cp[i])) {
ret.push_back(cp[i]);
}
}
return ret;
}
int main() {
while (1) {
int n;
cin >> n;
if (n == 0)
break;
double w, h, r, vx, vy;
cin >> w >> h >> r >> vx >> vy;
VP rec{P(r, r), P(w - r, r), P(w - r, h - r), P(r, h - r)};
P dir = unit(P(vx, vy));
VP p(n);
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
p[i] = P(x, y);
}
P pos = p[0];
double limit = 1e4;
int ans = -1;
while (limit > 0) {
P ep = pos + dir * 1e4;
L ray(pos, ep);
bool touch = false;
P cp(INF, INF);
for (int i = 1; i < n; i++) {
if (distanceSP(ray, p[i]) < 2 * r + EPS) {
VP cpcs = crosspointCS(C(p[i], 2 * r), ray);
for (P p : cpcs) {
if (abs(pos - p) < abs(pos - cp)) {
cp = p;
ans = i + 1;
}
}
touch = true;
}
}
if (touch) {
limit -= abs(pos - cp);
break;
}
bool corner = false;
for (int i = 0; i < 4; i++) {
if (intersectSP(ray, rec[i])) {
limit -= 2 * abs(pos - rec[i]);
dir = -dir;
corner = true;
break;
}
}
for (int i = 0; i < 4 && !corner; i++) {
L edge(rec[i], rec[(i + 1) % 4]);
if (!isParallel(ray, edge) && intersectSS(ray, edge)) {
P cpll = crosspointLL(ray, edge);
if (cpll == pos)
continue;
P to = reflection(edge, ep);
limit -= abs(cpll - pos);
pos = cpll;
dir = unit(to - cpll);
break;
}
}
}
if (limit < -EPS || ans == -1) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
return 0;
}
|
insert
| 29 | 29 | 29 | 35 |
TLE
| |
p01650
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
/*
<url:>
問題文============================================================
=================================================================
解説=============================================================
================================================================
*/
int dx[2] = {1, 0};
int dy[2] = {0, 1};
#define MAX_WH 55
vector<pii> alpha[30];
int dp[MAX_WH][MAX_WH][MAX_WH][MAX_WH];
bool can[MAX_WH][MAX_WH][MAX_WH][MAX_WH];
ll H, W;
int dfs(ll y1, ll x1, ll y2, ll x2, vector<vector<char>> &maze) {
int &ret = dp[y1][x1][y2][x2];
if (ret != -1)
return ret;
if (y1 == y2 && x1 == x2)
return ret = 0;
if (maze[y1][x1] == '#')
return ret;
if (y1 + 1 <= y2 && maze[y1 + 1][x1] != '#') {
ret = max(ret, dfs(y1 + 1, x1, y2, x2, maze));
}
if (x1 + 1 <= x2 && maze[y1][x1 + 1] != '#') {
ret = max(ret, dfs(y1, x1 + 1, y2, x2, maze));
}
char c = maze[y1][x1];
if (c >= 'a' && c <= 'z') {
int idx = c - 'a';
for (auto p : alpha[idx]) {
ll ny = p.first, nx = p.second;
if (ny > y2 || nx > x2)
continue;
if (abs(ny - y1) + abs(nx - x1) == 1)
ret = max(ret, dfs(ny, nx, y2, x2, maze) + 1);
else {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
int ny1 = y1 + dy[i], nx1 = x1 + dx[i];
if (ny1 > y2 || nx1 > x2)
continue;
int bny = ny - dy[j], bnx = nx - dx[j];
if (bny < y1 || bnx < x1)
continue;
if (!can[ny1][nx1][bny][bnx])
continue;
ret = max(ret, 1 + dfs(ny1, nx1, bny, bnx, maze) +
dfs(ny, nx, y2, x2, maze));
}
}
}
}
return ret;
}
int solve() {
int res = -1;
vector<vector<char>> maze(H + 2, vector<char>(W + 2, '#'));
for (int i = 0; i < 30; i++)
alpha[i].clear();
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char c;
cin >> c;
maze[i][j] = c;
if (c >= 'A' && c <= 'Z')
alpha[c - 'A'].push_back({i, j});
}
}
memset(can, false, sizeof(can));
// fill(***can,***can+MAX_WH*MAX_WH*MAX_WH*MAX_WH,false);
for (ll i = H; i >= 1; i--) {
for (ll j = W; j >= 1; j--) {
if (maze[i][j] == '#')
continue;
can[i][j][i][j] = 1;
if (maze[i + 1][j] != '#') {
for (ll ii = i + 1; ii <= H; ii++) {
for (ll jj = j; jj <= W; jj++) {
can[i][j][ii][jj] |= can[i + 1][j][ii][jj];
}
}
}
if (maze[i][j + 1] != '#') {
for (ll ii = i; ii <= H; ii++) {
for (ll jj = j + 1; jj <= W; jj++) {
can[i][j][ii][jj] |= can[i][j + 1][ii][jj];
}
}
}
}
}
// cout << maze << endl;
// fill(***dp,***dp+MAX_WH*MAX_WH*MAX_WH*MAX_WH,-1);
memset(dp, -1, sizeof(dp));
res = max(res, dfs(1, 1, H, W, maze));
return res;
}
int main(void) {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (cin >> H >> W, H | W) {
cout << solve() << endl;
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
/*
<url:>
問題文============================================================
=================================================================
解説=============================================================
================================================================
*/
int dx[2] = {1, 0};
int dy[2] = {0, 1};
#define MAX_WH 55
vector<pii> alpha[30];
int dp[MAX_WH][MAX_WH][MAX_WH][MAX_WH];
bool can[MAX_WH][MAX_WH][MAX_WH][MAX_WH];
ll H, W;
int dfs(ll y1, ll x1, ll y2, ll x2, vector<vector<char>> &maze) {
int &ret = dp[y1][x1][y2][x2];
if (ret != -1)
return ret;
if (y1 == y2 && x1 == x2)
return ret = 0;
ret = -INF;
if (maze[y1][x1] == '#')
return ret;
if (y1 + 1 <= y2 && maze[y1 + 1][x1] != '#') {
ret = max(ret, dfs(y1 + 1, x1, y2, x2, maze));
}
if (x1 + 1 <= x2 && maze[y1][x1 + 1] != '#') {
ret = max(ret, dfs(y1, x1 + 1, y2, x2, maze));
}
char c = maze[y1][x1];
if (c >= 'a' && c <= 'z') {
int idx = c - 'a';
for (auto p : alpha[idx]) {
ll ny = p.first, nx = p.second;
if (ny > y2 || nx > x2)
continue;
if (abs(ny - y1) + abs(nx - x1) == 1)
ret = max(ret, dfs(ny, nx, y2, x2, maze) + 1);
else {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
int ny1 = y1 + dy[i], nx1 = x1 + dx[i];
if (ny1 > y2 || nx1 > x2)
continue;
int bny = ny - dy[j], bnx = nx - dx[j];
if (bny < y1 || bnx < x1)
continue;
if (!can[ny1][nx1][bny][bnx])
continue;
ret = max(ret, 1 + dfs(ny1, nx1, bny, bnx, maze) +
dfs(ny, nx, y2, x2, maze));
}
}
}
}
return ret;
}
int solve() {
int res = -1;
vector<vector<char>> maze(H + 2, vector<char>(W + 2, '#'));
for (int i = 0; i < 30; i++)
alpha[i].clear();
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char c;
cin >> c;
maze[i][j] = c;
if (c >= 'A' && c <= 'Z')
alpha[c - 'A'].push_back({i, j});
}
}
memset(can, false, sizeof(can));
// fill(***can,***can+MAX_WH*MAX_WH*MAX_WH*MAX_WH,false);
for (ll i = H; i >= 1; i--) {
for (ll j = W; j >= 1; j--) {
if (maze[i][j] == '#')
continue;
can[i][j][i][j] = 1;
if (maze[i + 1][j] != '#') {
for (ll ii = i + 1; ii <= H; ii++) {
for (ll jj = j; jj <= W; jj++) {
can[i][j][ii][jj] |= can[i + 1][j][ii][jj];
}
}
}
if (maze[i][j + 1] != '#') {
for (ll ii = i; ii <= H; ii++) {
for (ll jj = j + 1; jj <= W; jj++) {
can[i][j][ii][jj] |= can[i][j + 1][ii][jj];
}
}
}
}
}
// cout << maze << endl;
// fill(***dp,***dp+MAX_WH*MAX_WH*MAX_WH*MAX_WH,-1);
memset(dp, -1, sizeof(dp));
res = max(res, dfs(1, 1, H, W, maze));
return res;
}
int main(void) {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (cin >> H >> W, H | W) {
cout << solve() << endl;
}
return 0;
}
|
insert
| 64 | 64 | 64 | 65 |
0
| |
p01653
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
struct aa {
int now;
long long int time;
int x;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
template <class T> class cht {
public:
vector<pair<T, T>> ls;
bool check(const pair<T, T> &l1, const pair<T, T> &l2,
const pair<T, T> &l3) const {
return 1.l * (l2.first - l1.first) * (l3.second - l2.second) >=
1.l * (l2.second - l1.second) * (l3.first - l2.first);
}
cht() : ls() {}
cht(vector<pair<T, T>> &vs) : ls() {
sort(vs.begin(), vs.end());
for (auto v : vs) {
add(v);
}
}
void add(const pair<T, T> &p) {
while (ls.size() >= 2) {
if (!check(ls[ls.size() - 2], ls[ls.size() - 1], p)) {
break;
} else {
ls.pop_back();
}
}
if (ls.size() == 1 && ls.back().second == p.second) {
ls.back().first = max(ls.back().first, p.first);
} else {
ls.emplace_back(p);
}
}
T f(int k, const T &x) const { return ls[k].first * x + ls[k].second; }
T query(const T &x) const {
int amin = 0;
int amax = ls.size();
while (amin + 1 != amax) {
int amid = (amin + amax) / 2;
if (f(amid - 1, x) <= f(amid, x)) {
amin = amid;
} else {
amax = amid;
}
}
return f(amin, x);
}
};
int main() {
while (1) {
int N, M, S1, S2, T;
cin >> N >> M >> S1 >> S2 >> T;
if (!N)
break;
S1--;
S2--;
T--;
vector<vector<pair<int, long long int>>> edges(N);
for (int i = 0; i < M; ++i) {
int a, b;
string w;
cin >> a >> b >> w;
a--;
b--;
long long int value;
if (w == "x") {
value = -1;
} else {
value = stoll(w);
}
edges[a].push_back(make_pair(b, value));
edges[b].push_back(make_pair(a, value));
}
// if (N != 1000)continue;
vector<vector<long long int>> memo(N, vector<long long int>(102, 1e18));
memo[T][0] = 0;
priority_queue<aa, vector<aa>, Compare> que;
que.push(aa{T, 0, 0});
while (!que.empty()) {
aa atop(que.top());
const int now = atop.now;
const long long int now_time = atop.time;
const int now_x = atop.x;
que.pop();
for (auto e : edges[atop.now]) {
if (e.second == -1) {
const long long int next_time = now_time;
if (now_x >= 100)
continue;
if (memo[e.first][now_x + 1] > next_time) {
bool ok = true;
for (int ax = 0; ax < now_x + 1; ++ax) {
if (memo[e.first][ax] <= next_time) {
ok = false;
break;
}
}
if (!ok)
continue;
memo[e.first][now_x + 1] = next_time;
que.push(aa{e.first, next_time, now_x + 1});
}
} else {
const long long int next_time = now_time + e.second;
if (memo[e.first][now_x] > next_time) {
bool ok = true;
for (int ax = 0; ax < now_x; ++ax) {
if (memo[e.first][ax] <= next_time) {
ok = false;
break;
}
}
if (!ok)
continue;
memo[e.first][now_x] = next_time;
que.push(aa{e.first, next_time, now_x});
}
}
}
}
cht<long long int> cht1, cht2;
for (int i = 100; i >= 0; --i) {
if (memo[S1][i] < 1e17) {
cht1.add(make_pair(-i, -memo[S1][i]));
}
if (memo[S2][i] < 1e17) {
cht2.add(make_pair(-i, -memo[S2][i]));
}
}
vector<long long int> nums;
nums.emplace_back(0);
nums.emplace_back(static_cast<long long int>(1e15));
assert(!cht1.ls.empty() && !cht2.ls.empty());
for (int i = 0; i < int(cht1.ls.size()) - 1; ++i) {
auto pa = cht1.ls[i];
auto pb = cht1.ls[i + 1];
assert(pb.first != pa.first);
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first));
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first) + 1);
}
for (int i = 0; i < int(cht2.ls.size()) - 1; ++i) {
auto pa = cht2.ls[i];
auto pb = cht2.ls[i + 1];
assert(pb.first != pa.first);
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first));
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first) + 1);
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
long long int ans = 1e18;
for (int i = 0; i < nums.size() - 1; ++i) {
long long int al = nums[i];
long long int ar = nums[i + 1];
if (ar < 0)
continue;
else if (al < 0)
al = 0;
long long int lvalue1 = cht1.query(al);
long long int lvalue2 = cht2.query(al);
long long int rvalue1 = cht1.query(ar);
long long int rvalue2 = cht2.query(ar);
if ((lvalue1 > lvalue2 && rvalue1 > rvalue2) ||
(lvalue1 < lvalue2 && rvalue1 < rvalue2)) {
ans = min(ans, min(abs(lvalue1 - lvalue2), abs(rvalue1 - rvalue2)));
} else {
long long int dis1 = (rvalue1 - lvalue1) / (ar - al);
long long int dis2 = (rvalue2 - lvalue2) / (ar - al);
if (dis1 == dis2) {
ans = min(ans, abs(lvalue1 - lvalue2));
} else {
long long int cx = abs((lvalue2 - lvalue1) / (dis1 - dis2));
if (dis1 > dis2) {
ans = min(ans, abs((lvalue2 - lvalue1) - (cx - 1) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx + 1) * (dis1 - dis2)));
} else {
ans = min(ans, abs((lvalue2 - lvalue1) - (cx - 1) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx + 1) * (dis1 - dis2)));
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
struct aa {
int now;
long long int time;
int x;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
template <class T> class cht {
public:
vector<pair<T, T>> ls;
bool check(const pair<T, T> &l1, const pair<T, T> &l2,
const pair<T, T> &l3) const {
return 1.l * (l2.first - l1.first) * (l3.second - l2.second) >=
1.l * (l2.second - l1.second) * (l3.first - l2.first);
}
cht() : ls() {}
cht(vector<pair<T, T>> &vs) : ls() {
sort(vs.begin(), vs.end());
for (auto v : vs) {
add(v);
}
}
void add(const pair<T, T> &p) {
while (ls.size() >= 2) {
if (!check(ls[ls.size() - 2], ls[ls.size() - 1], p)) {
break;
} else {
ls.pop_back();
}
}
if (ls.size() == 1 && ls.back().second == p.second) {
ls.back().first = max(ls.back().first, p.first);
} else {
ls.emplace_back(p);
}
}
T f(int k, const T &x) const { return ls[k].first * x + ls[k].second; }
T query(const T &x) const {
int amin = 0;
int amax = ls.size();
while (amin + 1 != amax) {
int amid = (amin + amax) / 2;
if (f(amid - 1, x) <= f(amid, x)) {
amin = amid;
} else {
amax = amid;
}
}
return f(amin, x);
}
};
int main() {
while (1) {
int N, M, S1, S2, T;
cin >> N >> M >> S1 >> S2 >> T;
if (!N)
break;
S1--;
S2--;
T--;
vector<vector<pair<int, long long int>>> edges(N);
for (int i = 0; i < M; ++i) {
int a, b;
string w;
cin >> a >> b >> w;
a--;
b--;
long long int value;
if (w == "x") {
value = -1;
} else {
value = stoll(w);
}
edges[a].push_back(make_pair(b, value));
edges[b].push_back(make_pair(a, value));
}
// if (N != 1000)continue;
vector<vector<long long int>> memo(N, vector<long long int>(102, 1e18));
memo[T][0] = 0;
priority_queue<aa, vector<aa>, Compare> que;
que.push(aa{T, 0, 0});
while (!que.empty()) {
aa atop(que.top());
const int now = atop.now;
const long long int now_time = atop.time;
const int now_x = atop.x;
que.pop();
for (auto e : edges[atop.now]) {
if (e.second == -1) {
const long long int next_time = now_time;
if (now_x >= 100)
continue;
if (memo[e.first][now_x + 1] > next_time) {
bool ok = true;
for (int ax = 0; ax < now_x + 1; ++ax) {
if (memo[e.first][ax] <= next_time) {
ok = false;
break;
}
}
if (!ok)
continue;
memo[e.first][now_x + 1] = next_time;
que.push(aa{e.first, next_time, now_x + 1});
}
} else {
const long long int next_time = now_time + e.second;
if (memo[e.first][now_x] > next_time) {
bool ok = true;
for (int ax = 0; ax < now_x; ++ax) {
if (memo[e.first][ax] <= next_time) {
ok = false;
break;
}
}
if (!ok)
continue;
memo[e.first][now_x] = next_time;
que.push(aa{e.first, next_time, now_x});
}
}
}
}
cht<long long int> cht1, cht2;
for (int i = 100; i >= 0; --i) {
if (memo[S1][i] < 1e17) {
cht1.add(make_pair(-i, -memo[S1][i]));
}
if (memo[S2][i] < 1e17) {
cht2.add(make_pair(-i, -memo[S2][i]));
}
}
vector<long long int> nums;
nums.emplace_back(0);
nums.emplace_back(static_cast<long long int>(1e15));
assert(!cht1.ls.empty() && !cht2.ls.empty());
for (int i = 0; i < int(cht1.ls.size()) - 1; ++i) {
auto pa = cht1.ls[i];
auto pb = cht1.ls[i + 1];
assert(pb.first != pa.first);
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first));
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first) + 1);
}
for (int i = 0; i < int(cht2.ls.size()) - 1; ++i) {
auto pa = cht2.ls[i];
auto pb = cht2.ls[i + 1];
assert(pb.first != pa.first);
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first));
nums.emplace_back((pa.second - pb.second) / (pb.first - pa.first) + 1);
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
long long int ans = 1e18;
for (int i = 0; i < nums.size() - 1; ++i) {
long long int al = nums[i];
long long int ar = nums[i + 1];
if (ar < 0)
continue;
else if (al < 0)
al = 0;
if (al == ar)
continue;
long long int lvalue1 = cht1.query(al);
long long int lvalue2 = cht2.query(al);
long long int rvalue1 = cht1.query(ar);
long long int rvalue2 = cht2.query(ar);
if ((lvalue1 > lvalue2 && rvalue1 > rvalue2) ||
(lvalue1 < lvalue2 && rvalue1 < rvalue2)) {
ans = min(ans, min(abs(lvalue1 - lvalue2), abs(rvalue1 - rvalue2)));
} else {
long long int dis1 = (rvalue1 - lvalue1) / (ar - al);
long long int dis2 = (rvalue2 - lvalue2) / (ar - al);
if (dis1 == dis2) {
ans = min(ans, abs(lvalue1 - lvalue2));
} else {
long long int cx = abs((lvalue2 - lvalue1) / (dis1 - dis2));
if (dis1 > dis2) {
ans = min(ans, abs((lvalue2 - lvalue1) - (cx - 1) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx + 1) * (dis1 - dis2)));
} else {
ans = min(ans, abs((lvalue2 - lvalue1) - (cx - 1) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx) * (dis1 - dis2)));
ans = min(ans, abs((lvalue2 - lvalue1) - (cx + 1) * (dis1 - dis2)));
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
insert
| 179 | 179 | 179 | 181 |
0
| |
p01653
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < (b) ? (a = b, 1) : 0)
#define chmin(a, b) (a > (b) ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int, int> pii;
typedef long long ll;
vector<pii> g[1000];
ll dist[1000][101];
struct P {
int v, c;
ll d;
P(int v, int c, ll d) : v(v), c(c), d(d) {}
bool operator<(const P &rhs) const { return d > rhs.d; }
};
int n, m, s1, s2, t;
vector<ll> func(int s) {
priority_queue<P> Q;
Q.push(P(s, 0, 0));
REP(i, n) REP(j, 101) dist[i][j] = 1LL << 60;
dist[s][0] = 0;
while (!Q.empty()) {
P p = Q.top();
Q.pop();
if (dist[p.v][p.c] < p.d)
continue;
FOR(it, g[p.v]) {
ll nd = p.d;
int nc = p.c;
if (it->second == -1) {
nc++;
} else {
nd += it->second;
}
if (dist[it->first][nc] > nd) {
dist[it->first][nc] = nd;
Q.push(P(it->first, nc, nd));
}
}
}
vector<ll> res;
REP(i, 101) { res.push_back(dist[t][i]); }
return res;
}
vector<ll> func2(vector<ll> v) {
vector<ll> res;
REP(i, v.size()) {
if (v[i] == 1LL << 60)
continue;
REP(j, i) {
if (v[j] == 1LL << 60)
continue;
// v[i]+i*x = v[j]+j*x
double x = -(v[i] - v[j]) / (i - j);
if (x < 0)
continue;
res.push_back((ll)x);
res.push_back((ll)(x + 1));
}
}
return res;
}
int main() {
while (cin >> n >> m >> s1 >> s2 >> t, n) {
s1--;
s2--;
t--;
REP(i, n) g[i].clear();
REP(i, m) {
int a, b;
string s;
cin >> a >> b >> s;
a--;
b--;
int d;
if (s == "x") {
d = -1;
} else {
d = atoi(s.c_str());
}
g[a].push_back(pii(b, d));
g[b].push_back(pii(a, d));
}
vector<ll> v1 = func(s1);
vector<ll> v2 = func(s2);
vector<ll> xs = func2(v1);
vector<ll> xs2 = func2(v2);
FOR(it, xs2) xs.push_back(*it);
REP(i, v1.size()) {
if (v1[i] == INF)
continue;
REP(j, v2.size()) {
if (v2[j] == INF)
continue;
if (i == j)
continue;
double x = -(v1[i] - v2[j]) / (i - j);
if (x < 0)
continue;
xs.push_back((ll)x);
xs.push_back((ll)(x + 1));
}
}
// REP(i,3) {
// cout << i << " " << v1[i] << " " << v2[i]<< endl;
// }
xs.push_back(0);
sort(ALL(xs));
xs.erase(unique(ALL(xs)), xs.end());
ll res = 1LL << 61;
FOR(it, xs) {
ll x = *it;
ll y1 = 1LL << 61;
REP(i, v1.size()) {
if (v1[i] == 1LL << 60)
continue;
chmin(y1, v1[i] + i * x);
}
ll y2 = 1LL << 61;
REP(i, v2.size()) {
if (v2[i] == 1LL << 60)
continue;
chmin(y2, v2[i] + i * x);
}
chmin(res, abs(y1 - y2));
}
cout << res << endl;
}
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < (b) ? (a = b, 1) : 0)
#define chmin(a, b) (a > (b) ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int, int> pii;
typedef long long ll;
vector<pii> g[1000];
ll dist[1000][101];
struct P {
int v, c;
ll d;
P(int v, int c, ll d) : v(v), c(c), d(d) {}
bool operator<(const P &rhs) const { return d > rhs.d; }
};
int n, m, s1, s2, t;
vector<ll> func(int s) {
priority_queue<P> Q;
Q.push(P(s, 0, 0));
REP(i, n) REP(j, 101) dist[i][j] = 1LL << 60;
dist[s][0] = 0;
while (!Q.empty()) {
P p = Q.top();
Q.pop();
if (dist[p.v][p.c] < p.d)
continue;
FOR(it, g[p.v]) {
ll nd = p.d;
int nc = p.c;
if (it->second == -1) {
nc++;
} else {
nd += it->second;
}
if (nc > 100)
continue;
if (dist[it->first][nc] > nd) {
dist[it->first][nc] = nd;
Q.push(P(it->first, nc, nd));
}
}
}
vector<ll> res;
REP(i, 101) { res.push_back(dist[t][i]); }
return res;
}
vector<ll> func2(vector<ll> v) {
vector<ll> res;
REP(i, v.size()) {
if (v[i] == 1LL << 60)
continue;
REP(j, i) {
if (v[j] == 1LL << 60)
continue;
// v[i]+i*x = v[j]+j*x
double x = -(v[i] - v[j]) / (i - j);
if (x < 0)
continue;
res.push_back((ll)x);
res.push_back((ll)(x + 1));
}
}
return res;
}
int main() {
while (cin >> n >> m >> s1 >> s2 >> t, n) {
s1--;
s2--;
t--;
REP(i, n) g[i].clear();
REP(i, m) {
int a, b;
string s;
cin >> a >> b >> s;
a--;
b--;
int d;
if (s == "x") {
d = -1;
} else {
d = atoi(s.c_str());
}
g[a].push_back(pii(b, d));
g[b].push_back(pii(a, d));
}
vector<ll> v1 = func(s1);
vector<ll> v2 = func(s2);
vector<ll> xs = func2(v1);
vector<ll> xs2 = func2(v2);
FOR(it, xs2) xs.push_back(*it);
REP(i, v1.size()) {
if (v1[i] == INF)
continue;
REP(j, v2.size()) {
if (v2[j] == INF)
continue;
if (i == j)
continue;
double x = -(v1[i] - v2[j]) / (i - j);
if (x < 0)
continue;
xs.push_back((ll)x);
xs.push_back((ll)(x + 1));
}
}
// REP(i,3) {
// cout << i << " " << v1[i] << " " << v2[i]<< endl;
// }
xs.push_back(0);
sort(ALL(xs));
xs.erase(unique(ALL(xs)), xs.end());
ll res = 1LL << 61;
FOR(it, xs) {
ll x = *it;
ll y1 = 1LL << 61;
REP(i, v1.size()) {
if (v1[i] == 1LL << 60)
continue;
chmin(y1, v1[i] + i * x);
}
ll y2 = 1LL << 61;
REP(i, v2.size()) {
if (v2[i] == 1LL << 60)
continue;
chmin(y2, v2[i] + i * x);
}
chmin(res, abs(y1 - y2));
}
cout << res << endl;
}
}
|
insert
| 61 | 61 | 61 | 63 |
0
| |
p01656
|
C++
|
Runtime Error
|
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
map<int, string> nm;
nm[0] = "kogakubu10gokan";
for (int i = 0; i < n; i++) {
int num;
string s;
cin >> num >> s;
nm[num] = s;
}
bool ju = true;
map<int, string>::iterator itr = ++nm.begin();
for (; itr != nm.end(); itr++) {
if (itr->first > q) {
cout << (--itr)->second << endl;
ju = false;
break;
}
}
if (ju)
cout << nm.end()->second << endl;
}
|
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
map<int, string> nm;
nm[0] = "kogakubu10gokan";
for (int i = 0; i < n; i++) {
int num;
string s;
cin >> num >> s;
nm[num] = s;
}
bool ju = true;
map<int, string>::iterator itr = ++nm.begin();
for (; itr != nm.end(); itr++) {
if (itr->first > q) {
cout << (--itr)->second << endl;
ju = false;
break;
}
}
if (ju)
cout << (--nm.end())->second << endl;
}
|
replace
| 26 | 27 | 26 | 27 |
0
| |
p01656
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()
#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> vvi;
const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007
int main() {
int n, m;
cin >> n >> m;
map<int, string> mp;
int t;
string s;
for (int i = 0; i < n; i++) {
cin >> t >> s;
mp[t] = s;
}
auto it = mp.upper_bound(m);
it--;
cout << it->se << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()
#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> vvi;
const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007
int main() {
int n, m;
cin >> n >> m;
map<int, string> mp;
int t;
string s;
for (int i = 0; i < n; i++) {
cin >> t >> s;
mp[t] = s;
}
mp[0] = "kogakubu10gokan";
auto it = mp.upper_bound(m);
it--;
cout << it->se << endl;
}
|
insert
| 52 | 52 | 52 | 53 |
0
| |
p01657
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)
typedef vector<int> vi;
pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
vi const &s) {
vi ans;
int ans_sum = -1;
int end = x + 1;
rep(i, n - 1) end *= x + 1;
rep(q, end) {
int mask = q;
vi v(n);
rep(i, n) {
v[i] = mask % (x + 1);
mask /= (x + 1);
}
vi sum(n);
sum[0] = v[0];
loop(i, 1, n) sum[i] = sum[i - 1] + v[i];
bool ok = true;
rep(i, m) {
int a = sum[r[i]];
if (l[i] != 0)
a -= sum[l[i] - 1];
if (a == s[i])
continue;
ok = false;
break;
}
if (ok && sum.back() > ans_sum) {
ans_sum = sum.back();
swap(ans, v);
}
}
return make_pair(ans, ans_sum);
}
int main() {
int n, x, m;
while (cin >> n >> x >> m) {
vi l(n), r(n), s(n);
rep(i, m) {
cin >> l[i] >> r[i] >> s[i];
l[i]--;
r[i]--;
}
auto t = solve(n, x, m, l, r, s);
if (t.second != -1) {
rep(i, t.first.size() - 1) cout << t.first[i] << " ";
cout << t.first.back() << endl;
} else {
cout << -1 << endl;
}
}
}
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)
typedef vector<int> vi;
pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
vi const &s) {
vi ans;
int ans_sum = -1;
int end = x + 1;
rep(i, n - 1) end *= x + 1;
rep(q, end) {
int mask = q;
vi v(n);
rep(i, n) {
v[i] = mask % (x + 1);
mask /= (x + 1);
}
vi sum(n);
sum[0] = v[0];
loop(i, 1, n) sum[i] = sum[i - 1] + v[i];
bool ok = true;
rep(i, m) {
int a = sum[r[i]];
if (l[i] != 0)
a -= sum[l[i] - 1];
if (a == s[i])
continue;
ok = false;
break;
}
if (ok && sum.back() > ans_sum) {
ans_sum = sum.back();
swap(ans, v);
}
}
return make_pair(ans, ans_sum);
}
int main() {
int n, x, m;
while (cin >> n >> x >> m) {
vi l(m), r(m), s(m);
rep(i, m) {
cin >> l[i] >> r[i] >> s[i];
l[i]--;
r[i]--;
}
auto t = solve(n, x, m, l, r, s);
if (t.second != -1) {
rep(i, t.first.size() - 1) cout << t.first[i] << " ";
cout << t.first.back() << endl;
} else {
cout << -1 << endl;
}
}
}
|
replace
| 51 | 52 | 51 | 52 |
0
| |
p01657
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
int main() {
int n, m, x;
int l[10], r[10], s[10];
while (cin >> n >> x >> m) {
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i] >> s[i];
}
x++;
int end = 1;
for (int i = 0; i < n; i++)
end *= x;
int maxSum = -1;
int ans[10];
int res[10];
for (int i = 0; i < end; i++) {
int t = i;
for (int j = 0; j < n; j++) {
ans[j] = t % x;
t /= x;
}
bool check = true;
for (int j = 0; j < m; j++) {
int sum = 0;
for (int k = l[j]; k <= r[j]; k++) {
sum += ans[k - 1];
}
if (sum != s[j])
check = false;
}
if (check) {
int allSum = 0;
for (int j = 0; j < n; j++)
allSum += ans[j];
if (allSum > maxSum) {
maxSum = allSum;
for (int j = 0; j < n; j++)
res[j] = ans[j];
}
}
}
if (maxSum >= 0) {
for (int i = 0; i < n - 1; i++)
cout << res[i] << " ";
cout << res[n - 1] << endl;
} else {
cout << -1 << endl;
}
}
}
|
#include <iostream>
using namespace std;
int main() {
int n, m, x;
int l[10], r[10], s[10];
while (cin >> n >> x >> m) {
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> s[i];
}
x++;
int end = 1;
for (int i = 0; i < n; i++)
end *= x;
int maxSum = -1;
int ans[10];
int res[10];
for (int i = 0; i < end; i++) {
int t = i;
for (int j = 0; j < n; j++) {
ans[j] = t % x;
t /= x;
}
bool check = true;
for (int j = 0; j < m; j++) {
int sum = 0;
for (int k = l[j]; k <= r[j]; k++) {
sum += ans[k - 1];
}
if (sum != s[j])
check = false;
}
if (check) {
int allSum = 0;
for (int j = 0; j < n; j++)
allSum += ans[j];
if (allSum > maxSum) {
maxSum = allSum;
for (int j = 0; j < n; j++)
res[j] = ans[j];
}
}
}
if (maxSum >= 0) {
for (int i = 0; i < n - 1; i++)
cout << res[i] << " ";
cout << res[n - 1] << endl;
} else {
cout << -1 << endl;
}
}
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p01659
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;
int N;
int a[200010];
int main() {
cin >> N;
for (int j = 0; j < N; ++j) {
cin >> a[j];
}
stack<int> s;
int cnt = 1;
s.push(a[0]);
for (int j = 1; j < N; ++j) {
int t = s.top();
while (t > a[j]) {
s.pop();
t = s.top();
}
if (t < a[j]) {
++cnt;
s.push(a[j]);
}
}
cout << cnt << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;
int N;
int a[200010];
int main() {
cin >> N;
for (int j = 0; j < N; ++j) {
cin >> a[j];
}
stack<int> s;
int cnt = 1;
s.push(0);
s.push(a[0]);
for (int j = 1; j < N; ++j) {
int t = s.top();
while (t > a[j]) {
s.pop();
t = s.top();
}
if (t < a[j]) {
++cnt;
s.push(a[j]);
}
}
cout << cnt << endl;
return 0;
}
|
insert
| 33 | 33 | 33 | 34 |
0
| |
p01659
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int n, res, a[21600];
set<int> nums;
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
nums.insert(a[i]);
while (*nums.rbegin() > a[i]) {
res++;
nums.erase(*nums.rbegin());
}
}
cout << res + nums.size() << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int n, res, a[216000];
set<int> nums;
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
nums.insert(a[i]);
while (*nums.rbegin() > a[i]) {
res++;
nums.erase(*nums.rbegin());
}
}
cout << res + nums.size() << endl;
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p01667
|
C++
|
Runtime Error
|
#include <algorithm>
#include <stdio.h>
using namespace std;
char str[3];
pair<int, int> v[100][100];
int g[100][100];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++) {
int c;
scanf("%d", &c);
for (int j = 0; j < b; j++)
v[i][j] = make_pair(0, 100);
for (int j = 0; j < c; j++) {
int d, e;
scanf("%d%s%d", &d, str, &e);
d--;
if (str[0] == '>') {
v[i][d].first = max(v[i][d].first, e);
} else
v[i][d].second = min(v[i][d].second, e);
}
}
bool ok = true;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
if (v[i][j].first > v[i][j].second)
ok = false;
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++) {
for (int k = 0; k < b; k++)
if (v[i][k].second < v[j][k].first)
g[i][j] = 1;
}
for (int k = 0; k < a; k++)
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
g[i][j] |= (g[i][k] & g[k][j]);
for (int i = 0; i < a; i++)
if (g[i][i])
ok = false;
for (int i = 0; ok && i < a; i++) {
for (int j = 0; ok && j < a; j++) {
for (int k = 0; ok && k < b; k++) {
for (int l = 0; ok && l < b; l++) {
if (v[i][k].second < v[j][k].first && v[j][l].second < v[i][l].first)
ok = false;
if (v[j][k].second < v[i][k].first && v[i][l].second < v[j][l].first)
ok = false;
}
}
}
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
|
#include <algorithm>
#include <stdio.h>
using namespace std;
char str[10];
pair<int, int> v[110][110];
int g[110][110];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++) {
int c;
scanf("%d", &c);
for (int j = 0; j < b; j++)
v[i][j] = make_pair(0, 100);
for (int j = 0; j < c; j++) {
int d, e;
scanf("%d%s%d", &d, str, &e);
d--;
if (str[0] == '>') {
v[i][d].first = max(v[i][d].first, e);
} else
v[i][d].second = min(v[i][d].second, e);
}
}
bool ok = true;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
if (v[i][j].first > v[i][j].second)
ok = false;
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++) {
for (int k = 0; k < b; k++)
if (v[i][k].second < v[j][k].first)
g[i][j] = 1;
}
for (int k = 0; k < a; k++)
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
g[i][j] |= (g[i][k] & g[k][j]);
for (int i = 0; i < a; i++)
if (g[i][i])
ok = false;
for (int i = 0; ok && i < a; i++) {
for (int j = 0; ok && j < a; j++) {
for (int k = 0; ok && k < b; k++) {
for (int l = 0; ok && l < b; l++) {
if (v[i][k].second < v[j][k].first && v[j][l].second < v[i][l].first)
ok = false;
if (v[j][k].second < v[i][k].first && v[i][l].second < v[j][l].first)
ok = false;
}
}
}
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
|
replace
| 4 | 7 | 4 | 7 |
0
| |
p01667
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
bool f = 1, e[100][100];
int n;
void dfs(int now, int par, VI visit) {
if (visit[now]) {
if (now == par)
f = 0;
return;
}
visit[now] = 1;
REP(i, n) {
if (e[now][i])
dfs(i, par, visit);
}
}
int main() {
int m;
cin >> n >> m;
VVI h(n, VI(m, 1000)), l(n, VI(m));
REP(i, n) {
int k;
cin >> k;
while (k--) {
int j, x;
string s;
cin >> j >> s >> x;
if (s == ">=")
l[i][j - 1] = max(l[i][j - 1], x);
else
h[i][j - 1] = min(h[i][j - 1], x);
}
}
REP(i, n) REP(j, m) if (l[i][j] > h[i][j]) f = 0;
REP(i, n) REP(j, n) {
bool g = 0;
REP(k, m) if (l[i][k] > h[j][k]) g = 1;
e[i][j] = g;
}
REP(i, n) {
VI visit(n);
dfs(i, i, visit);
}
cout << (f ? "Yes" : "No") << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
bool f = 1, e[100][100];
int n;
void dfs(int now, int par, VI &visit) {
if (visit[now]) {
if (now == par)
f = 0;
return;
}
visit[now] = 1;
REP(i, n) {
if (e[now][i])
dfs(i, par, visit);
}
}
int main() {
int m;
cin >> n >> m;
VVI h(n, VI(m, 1000)), l(n, VI(m));
REP(i, n) {
int k;
cin >> k;
while (k--) {
int j, x;
string s;
cin >> j >> s >> x;
if (s == ">=")
l[i][j - 1] = max(l[i][j - 1], x);
else
h[i][j - 1] = min(h[i][j - 1], x);
}
}
REP(i, n) REP(j, m) if (l[i][j] > h[i][j]) f = 0;
REP(i, n) REP(j, n) {
bool g = 0;
REP(k, m) if (l[i][k] > h[j][k]) g = 1;
e[i][j] = g;
}
REP(i, n) {
VI visit(n);
dfs(i, i, visit);
}
cout << (f ? "Yes" : "No") << endl;
return 0;
}
|
replace
| 28 | 29 | 28 | 29 |
TLE
| |
p01667
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];
int check(int a, int b) {
if (memo[a][b] != -1)
return memo[a][b];
for (int i = 0; i < n; i++)
if (cmd[a][i].l > cmd[b][i].r)
return memo[a][b] = 0;
return memo[a][b] = 1;
}
int solve() {
memset(memo, -1, sizeof(memo));
int cnt = 0;
for (int i = 0; i < m; i++) {
int j = 0;
while (j < m && (used[j] || check(i, j)))
j++;
if (j == m)
used[i] = 1, cnt++, i = -1;
}
return cnt / m;
}
int main() {
for (int i = 0; i < 101; i++)
for (int j = 0; j < 101; j++)
cmd[i][j] = P(-INF, INF);
cin >> m >> n;
for (int i = 0, k; i < m; i++) {
cin >> k;
while (k--) {
string cond;
int s, t;
cin >> s >> cond >> t;
s--;
if (cond == "<=")
cmd[i][s].r = min(cmd[i][s].r, t);
if (cond == ">=")
cmd[i][s].l = max(cmd[i][s].l, t);
if (cmd[i][s].l > cmd[i][s].r)
ans = 0;
}
}
cout << (ans * solve() ? "Yes" : "No") << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];
int check(int a, int b) {
if (memo[a][b] != -1)
return memo[a][b];
for (int i = 0; i < n; i++)
if (cmd[a][i].l > cmd[b][i].r)
return memo[a][b] = 0;
return memo[a][b] = 1;
}
int solve() {
memset(memo, -1, sizeof(memo));
int cnt = 0;
for (int i = 0; i < m; i++) {
int j = 0;
while (!used[i] && j < m && (used[j] || check(i, j)))
j++;
if (j == m)
used[i] = 1, cnt++, i = -1;
}
return cnt / m;
}
int main() {
for (int i = 0; i < 101; i++)
for (int j = 0; j < 101; j++)
cmd[i][j] = P(-INF, INF);
cin >> m >> n;
for (int i = 0, k; i < m; i++) {
cin >> k;
while (k--) {
string cond;
int s, t;
cin >> s >> cond >> t;
s--;
if (cond == "<=")
cmd[i][s].r = min(cmd[i][s].r, t);
if (cond == ">=")
cmd[i][s].l = max(cmd[i][s].l, t);
if (cmd[i][s].l > cmd[i][s].r)
ans = 0;
}
}
cout << (ans * solve() ? "Yes" : "No") << endl;
return 0;
}
|
replace
| 23 | 24 | 23 | 24 |
TLE
| |
p01670
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef bitset<300000> BS;
BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];
int rec(const BS &bs, int nth, int r) {
if (r < 0)
return r;
if (bs == absbs)
return r;
while ((is[idx[nth]] & ~bs).none()) {
nth++;
}
BS n = is[idx[nth]] & ~bs;
if (n.count() == 1) {
for (int i = 0;; i++) {
if (n[i]) {
return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
}
}
} else {
int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
BS o = bs;
int c = 0;
for (int i = 0; n.any(); i++) {
if (n[i]) {
o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
c++;
n[i] = false;
}
}
return max(rv, rec(o, nth + 1, r - c));
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
int pop[32345] = {};
for (int i = 0; i < M; i++) {
absbs[i] = true;
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
pop[a[i]]++;
pop[b[i]]++;
is[a[i]][i] = true;
is[b[i]][i] = true;
}
idx.resize(N);
iota(begin(idx), end(idx), 0);
sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
int r = rec(BS(), 0, K);
if (r < 0) {
cout << "Impossible" << endl;
} else {
cout << K - r << endl;
}
}
|
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef bitset<300000 / 10> BS;
BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];
int rec(const BS &bs, int nth, int r) {
if (r < 0)
return r;
if (bs == absbs)
return r;
while ((is[idx[nth]] & ~bs).none()) {
nth++;
}
BS n = is[idx[nth]] & ~bs;
if (n.count() == 1) {
for (int i = 0;; i++) {
if (n[i]) {
return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
}
}
} else {
int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
BS o = bs;
int c = 0;
for (int i = 0; n.any(); i++) {
if (n[i]) {
o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
c++;
n[i] = false;
}
}
return max(rv, rec(o, nth + 1, r - c));
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
int pop[32345] = {};
for (int i = 0; i < M; i++) {
absbs[i] = true;
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
pop[a[i]]++;
pop[b[i]]++;
is[a[i]][i] = true;
is[b[i]][i] = true;
}
idx.resize(N);
iota(begin(idx), end(idx), 0);
sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
int r = rec(BS(), 0, K);
if (r < 0) {
cout << "Impossible" << endl;
} else {
cout << K - r << endl;
}
}
|
replace
| 8 | 9 | 8 | 9 |
-11
| |
p01670
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n) \
cout << #a << ":"; \
rep(pachico, n) cout << " " << a[pachico]; \
cout << endl
#define svec(v) \
cout << #v << ":"; \
rep(pachico, v.size()) cout << " " << v[pachico]; \
cout << endl
#define svecp(v) \
cout << #v << ":"; \
each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second \
<< "}"; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(pachico, s) cout << " " << pachico; \
cout << endl
#define smap(m) \
cout << #m << ":"; \
each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second \
<< "}"; \
cout << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template <typename T> class ListIterator;
// 常に不変なイテレーターが欲しい場合に別のコンテナにこれをかぶせる形で使うとよい
template <typename T> class List {
public:
using iterator = ListIterator<T>;
private:
int N, sz;
vector<int> prev, next;
vector<T> container;
friend ListIterator<T>;
public:
// 添字 next_index の要素の前に添字 now_index の要素の iterator を挿入
iterator insert(int next_index, int now_index) {
sz++;
prev[now_index] = prev[next_index], next[now_index] = next_index;
next[prev[next_index]] = now_index, prev[next_index] = now_index;
return iterator(this, now_index);
}
// 添字 index の値を削除 → 次の要素の iterator を返す
iterator erase(int index) {
sz--;
next[prev[index]] = next[index], prev[next[index]] = prev[index];
return iterator(this, next[index]);
}
public:
friend ostream &operator<<(ostream &os, List &ls) {
for (auto &val : ls) {
os << val << ' ';
}
return os;
}
const T &operator[](size_t index) const { return container[index]; }
T &operator[](size_t index) { return container[index]; }
size_t size() { return sz; }
bool empty() { return size() == 0; }
T &front() { return container[next[N]]; }
T &back() { return container[prev[N]]; }
iterator begin() { return iterator(this, next[N]); }
iterator end() { return iterator(this, N); }
iterator insert(const iterator &itr, int index) {
return insert(itr.index, index);
}
iterator erase(const iterator &itr) { return erase(itr.index); }
iterator push_back() { return insert(N, prev[N] + 1); }
void pop_back() { erase(prev[N]); }
void clear() {
N = sz = 0;
prev = next = {0};
container.clear();
}
public:
List() : N(0), sz(0), prev({0}), next({0}) {}
List(int _N) : container(_N) { build(); }
List(int _N, T val) : container(_N, val) { build(); }
List(const List &ls)
: N(ls.N), sz(ls.sz), prev(ls.prev), next(ls.next),
container(ls.container) {}
List(const vector<T> &vec) : container(vec) { build(); }
List &operator=(const List &ls) {
container = ls.container;
build();
return *this;
}
List &operator=(const vector<T> &vec) {
container = vec;
build();
return *this;
}
List &operator=(const List &&ls) noexcept {
container = ls.container;
build();
return *this;
}
List(initializer_list<T> vals) : container(vals.begin(), vals.end()) {
build();
}
iterator push_back(T val) {
N++, sz++;
prev.push_back(N - 1), prev[0] = N;
next.back() = N, next.push_back(0);
container.push_back(val);
return iterator(this, N - 1);
}
void build() {
N = sz = (int)container.size();
prev.resize(N + 1), next.resize(N + 1);
iota(prev.begin(), prev.end(), -1), iota(next.begin(), next.end(), 1);
prev[0] = N, next[N] = 0;
}
};
template <typename T>
class ListIterator
: public std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T *, T &> {
private:
friend List<T>;
List<T> *ls_ptr;
int index;
private:
ListIterator(List<T> *ls, int _index) : ls_ptr(ls), index(_index) {}
public:
ListIterator() {}
ListIterator(const ListIterator &itr) {
ls_ptr = itr.ls_ptr, index = itr.index;
}
operator int() const noexcept { return index; }
ListIterator &operator=(const ListIterator &itr) & {
ls_ptr = itr.ls_ptr, index = itr.index;
return *this;
}
ListIterator &operator=(ListIterator &&itr) &noexcept {
ls_ptr = itr.ls_ptr, index = itr.index;
return *this;
}
T &operator*() const { return ls_ptr->container[index]; }
T *operator->() const { return &ls_ptr->container[index]; }
ListIterator &operator++() {
index = ls_ptr->next[index];
return *this;
}
ListIterator operator++(int) {
ListIterator res = *this;
index = ls_ptr->next[index];
return res;
}
ListIterator &operator--() {
index = ls_ptr->prev[index];
return *this;
};
ListIterator operator--(int) {
ListIterator res = *this;
index = ls_ptr->prev[index];
return res;
};
bool operator==(const ListIterator &itr) const { return !(*this != itr); };
bool operator!=(const ListIterator &itr) const {
return this->ls_ptr != itr.ls_ptr || this->index != itr.index;
}
friend void swap(const ListIterator<T> &itr1, const ListIterator<T> &itr2) {
ListIterator<T> tmp = itr1;
itr1 = itr2, itr2 = itr1;
};
};
// 多重辺はなし
class MVC {
private:
struct edge {
int to, rev;
};
struct info {
// from から to 番目に出ている辺が(を) next の前から消えた(に入れる)
int from, to, next;
info(int ag1, int ag2, int ag3) : from(ag1), to(ag2), next(ag3) {}
};
int V, rem_edge_size, fp_size;
vector<List<edge>> G;
List<int> rem_ver;
vector<int> cand, deg;
vector<vector<info>> edge_info;
set<int> small_deg_ver, use_ver;
bool push_cand(int u, int add_size) {
add_size++, cand.push_back(u);
return (int)cand.size() < ans_size;
}
bool erase(int u, bool use, bool alr_use, int add_size,
vector<pair<int, int>> &erase_ver) {
if (use && !alr_use) {
if (!push_cand(u, add_size))
return false;
}
erase_ver.emplace_back(u, rem_ver.erase(u));
for (auto &e : G[u]) {
edge_info[u].emplace_back(e.to, e.rev, G[e.to].erase(e.rev));
deg[e.to]--, rem_edge_size--;
if (deg[e.to] == 0)
erase_ver.emplace_back(e.to, rem_ver.erase(e.to));
if (!use) {
if (use_ver.count(e.to) == 1)
continue;
if (!push_cand(e.to, add_size))
return false;
if (deg[e.to] > 0)
use_ver.insert(e.to);
} else {
if (deg[e.to] == 1 && use_ver.count(e.to) == 0)
small_deg_ver.insert(e.to);
}
}
return true;
}
bool erase_rec_set(set<int> &st, bool flag, int add_size,
vector<pair<int, int>> &erase_ver) {
while (!st.empty()) {
int v = *st.begin();
st.erase(v);
if (deg[v] == 0)
continue;
if (!erase(v, flag, flag, add_size, erase_ver))
return false;
}
return true;
}
bool erase_rec(int u, bool use, int add_size,
vector<pair<int, int>> &erase_ver) {
if (!erase(u, use, false, add_size, erase_ver))
return false;
while (!small_deg_ver.empty() || !use_ver.empty()) {
if (!erase_rec_set(small_deg_ver, false, add_size, erase_ver))
return false;
if (!erase_rec_set(use_ver, true, add_size, erase_ver))
return false;
}
return true;
}
bool preprocessing(int K, int add_size, vector<pair<int, int>> &erase_ver) {
for (int u : rem_ver) {
if (deg[u] > K) {
if (!erase_rec(u, true, add_size, erase_ver))
return false;
}
}
return true;
}
void postprocessing(int K, int add_size, vector<pair<int, int>> &erase_ver) {
// 最初の層で行われた前処理による変更は特に後処理する必要がない
if (K == fp_size)
return;
for (int i = 0; i < add_size; i++) {
cand.pop_back();
}
for (int i = (int)erase_ver.size() - 1; i >= 0; i--) {
auto &p = erase_ver[i];
while (!edge_info[p.first].empty()) {
auto &dat = edge_info[p.first].back();
G[dat.from].insert(dat.next, dat.to), deg[dat.from]++, rem_edge_size++;
}
rem_ver.insert(p.second, p.first);
}
}
bool check() {
if (rem_edge_size == 0) {
if ((int)cand.size() < ans_size) {
ans_size = (int)cand.size(), ans_set = cand;
}
return false;
}
return (int)rem_ver.size() > 0;
}
void transition(int K, int next_ver, bool use) {
int pl_add_size = 0;
vector<pair<int, int>> pl_erase_ver;
if (!erase_rec(next_ver, use, pl_add_size, pl_erase_ver)) {
return postprocessing(K - 1, pl_add_size, pl_erase_ver);
}
if (check())
judge(K - 1);
return postprocessing(K - 1, pl_add_size, pl_erase_ver);
}
int find_max_degree_ver() {
// 次数の高い頂点から探索する
int max_deg = -1, next_ver = -1;
for (int v : rem_ver) {
if (deg[v] > max_deg)
max_deg = deg[v], next_ver = v;
}
if (2 * rem_edge_size > (long long)max_deg * (int)rem_ver.size())
return -1;
return next_ver;
}
void judge(int K) {
int add_size = 0;
vector<pair<int, int>> erase_ver;
if (!preprocessing(K, add_size, erase_ver))
return postprocessing(K, add_size, erase_ver);
if (!check())
return postprocessing(K, add_size, erase_ver);
int next_ver = find_max_degree_ver();
if (next_ver < 0)
return postprocessing(K, add_size, erase_ver);
// 使う場合
transition(K, next_ver, true);
// 使わない場合
if ((int)cand.size() + (int)G[next_ver].size() >= ans_size) {
transition(K, next_ver, false);
}
return postprocessing(K, add_size, erase_ver);
}
public:
vector<int> ans_set;
int ans_size;
MVC(int node_size)
: V(node_size), rem_edge_size(0), G(V), rem_ver(V), deg(V, 0),
edge_info(V) {
for (int i = 0; i < V; i++)
rem_ver[i] = i;
}
void add_edge(int u, int v) {
G[u].push_back((edge){v, (int)G[v].size()});
G[v].push_back((edge){u, (int)G[u].size() - 1});
deg[u]++, deg[v]++, rem_edge_size++;
}
bool solve(int K) {
fp_size = K, ans_size = fp_size + 1;
judge(fp_size);
return ans_size <= fp_size;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, K;
cin >> n >> m >> K;
MVC mvc(n);
rep(i, m) {
int a, b;
cin >> a >> b;
mvc.add_edge(a - 1, b - 1);
}
if (mvc.solve(K)) {
cout << mvc.ans_size << "\n";
} else {
cout << "Impossible\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n) \
cout << #a << ":"; \
rep(pachico, n) cout << " " << a[pachico]; \
cout << endl
#define svec(v) \
cout << #v << ":"; \
rep(pachico, v.size()) cout << " " << v[pachico]; \
cout << endl
#define svecp(v) \
cout << #v << ":"; \
each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second \
<< "}"; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(pachico, s) cout << " " << pachico; \
cout << endl
#define smap(m) \
cout << #m << ":"; \
each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second \
<< "}"; \
cout << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template <typename T> class ListIterator;
// 常に不変なイテレーターが欲しい場合に別のコンテナにこれをかぶせる形で使うとよい
template <typename T> class List {
public:
using iterator = ListIterator<T>;
private:
int N, sz;
vector<int> prev, next;
vector<T> container;
friend ListIterator<T>;
public:
// 添字 next_index の要素の前に添字 now_index の要素の iterator を挿入
iterator insert(int next_index, int now_index) {
sz++;
prev[now_index] = prev[next_index], next[now_index] = next_index;
next[prev[next_index]] = now_index, prev[next_index] = now_index;
return iterator(this, now_index);
}
// 添字 index の値を削除 → 次の要素の iterator を返す
iterator erase(int index) {
sz--;
next[prev[index]] = next[index], prev[next[index]] = prev[index];
return iterator(this, next[index]);
}
public:
friend ostream &operator<<(ostream &os, List &ls) {
for (auto &val : ls) {
os << val << ' ';
}
return os;
}
const T &operator[](size_t index) const { return container[index]; }
T &operator[](size_t index) { return container[index]; }
size_t size() { return sz; }
bool empty() { return size() == 0; }
T &front() { return container[next[N]]; }
T &back() { return container[prev[N]]; }
iterator begin() { return iterator(this, next[N]); }
iterator end() { return iterator(this, N); }
iterator insert(const iterator &itr, int index) {
return insert(itr.index, index);
}
iterator erase(const iterator &itr) { return erase(itr.index); }
iterator push_back() { return insert(N, prev[N] + 1); }
void pop_back() { erase(prev[N]); }
void clear() {
N = sz = 0;
prev = next = {0};
container.clear();
}
public:
List() : N(0), sz(0), prev({0}), next({0}) {}
List(int _N) : container(_N) { build(); }
List(int _N, T val) : container(_N, val) { build(); }
List(const List &ls)
: N(ls.N), sz(ls.sz), prev(ls.prev), next(ls.next),
container(ls.container) {}
List(const vector<T> &vec) : container(vec) { build(); }
List &operator=(const List &ls) {
container = ls.container;
build();
return *this;
}
List &operator=(const vector<T> &vec) {
container = vec;
build();
return *this;
}
List &operator=(const List &&ls) noexcept {
container = ls.container;
build();
return *this;
}
List(initializer_list<T> vals) : container(vals.begin(), vals.end()) {
build();
}
iterator push_back(T val) {
N++, sz++;
prev.push_back(N - 1), prev[0] = N;
next.back() = N, next.push_back(0);
container.push_back(val);
return iterator(this, N - 1);
}
void build() {
N = sz = (int)container.size();
prev.resize(N + 1), next.resize(N + 1);
iota(prev.begin(), prev.end(), -1), iota(next.begin(), next.end(), 1);
prev[0] = N, next[N] = 0;
}
};
template <typename T>
class ListIterator
: public std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T *, T &> {
private:
friend List<T>;
List<T> *ls_ptr;
int index;
private:
ListIterator(List<T> *ls, int _index) : ls_ptr(ls), index(_index) {}
public:
ListIterator() {}
ListIterator(const ListIterator &itr) {
ls_ptr = itr.ls_ptr, index = itr.index;
}
operator int() const noexcept { return index; }
ListIterator &operator=(const ListIterator &itr) & {
ls_ptr = itr.ls_ptr, index = itr.index;
return *this;
}
ListIterator &operator=(ListIterator &&itr) &noexcept {
ls_ptr = itr.ls_ptr, index = itr.index;
return *this;
}
T &operator*() const { return ls_ptr->container[index]; }
T *operator->() const { return &ls_ptr->container[index]; }
ListIterator &operator++() {
index = ls_ptr->next[index];
return *this;
}
ListIterator operator++(int) {
ListIterator res = *this;
index = ls_ptr->next[index];
return res;
}
ListIterator &operator--() {
index = ls_ptr->prev[index];
return *this;
};
ListIterator operator--(int) {
ListIterator res = *this;
index = ls_ptr->prev[index];
return res;
};
bool operator==(const ListIterator &itr) const { return !(*this != itr); };
bool operator!=(const ListIterator &itr) const {
return this->ls_ptr != itr.ls_ptr || this->index != itr.index;
}
friend void swap(const ListIterator<T> &itr1, const ListIterator<T> &itr2) {
ListIterator<T> tmp = itr1;
itr1 = itr2, itr2 = itr1;
};
};
// 多重辺はなし
class MVC {
private:
struct edge {
int to, rev;
};
struct info {
// from から to 番目に出ている辺が(を) next の前から消えた(に入れる)
int from, to, next;
info(int ag1, int ag2, int ag3) : from(ag1), to(ag2), next(ag3) {}
};
int V, rem_edge_size, fp_size;
vector<List<edge>> G;
List<int> rem_ver;
vector<int> cand, deg;
vector<vector<info>> edge_info;
set<int> small_deg_ver, use_ver;
bool push_cand(int u, int add_size) {
add_size++, cand.push_back(u);
return (int)cand.size() < ans_size;
}
bool erase(int u, bool use, bool alr_use, int add_size,
vector<pair<int, int>> &erase_ver) {
if (use && !alr_use) {
if (!push_cand(u, add_size))
return false;
}
erase_ver.emplace_back(u, rem_ver.erase(u));
for (auto &e : G[u]) {
edge_info[u].emplace_back(e.to, e.rev, G[e.to].erase(e.rev));
deg[e.to]--, rem_edge_size--;
if (deg[e.to] == 0)
erase_ver.emplace_back(e.to, rem_ver.erase(e.to));
if (!use) {
if (use_ver.count(e.to) == 1)
continue;
if (!push_cand(e.to, add_size))
return false;
if (deg[e.to] > 0)
use_ver.insert(e.to);
} else {
if (deg[e.to] == 1 && use_ver.count(e.to) == 0)
small_deg_ver.insert(e.to);
}
}
return true;
}
bool erase_rec_set(set<int> &st, bool flag, int add_size,
vector<pair<int, int>> &erase_ver) {
while (!st.empty()) {
int v = *st.begin();
st.erase(v);
if (deg[v] == 0)
continue;
if (!erase(v, flag, flag, add_size, erase_ver))
return false;
}
return true;
}
bool erase_rec(int u, bool use, int add_size,
vector<pair<int, int>> &erase_ver) {
if (!erase(u, use, false, add_size, erase_ver))
return false;
while (!small_deg_ver.empty() || !use_ver.empty()) {
if (!erase_rec_set(small_deg_ver, false, add_size, erase_ver))
return false;
if (!erase_rec_set(use_ver, true, add_size, erase_ver))
return false;
}
return true;
}
bool preprocessing(int K, int add_size, vector<pair<int, int>> &erase_ver) {
for (int u : rem_ver) {
if (deg[u] > K) {
if (!erase_rec(u, true, add_size, erase_ver))
return false;
}
}
return true;
}
void postprocessing(int K, int add_size, vector<pair<int, int>> &erase_ver) {
// 最初の層で行われた前処理による変更は特に後処理する必要がない
if (K == fp_size)
return;
for (int i = 0; i < add_size; i++) {
cand.pop_back();
}
for (int i = (int)erase_ver.size() - 1; i >= 0; i--) {
auto &p = erase_ver[i];
while (!edge_info[p.first].empty()) {
auto &dat = edge_info[p.first].back();
G[dat.from].insert(dat.next, dat.to), deg[dat.from]++, rem_edge_size++;
edge_info[p.first].pop_back();
}
rem_ver.insert(p.second, p.first);
}
}
bool check() {
if (rem_edge_size == 0) {
if ((int)cand.size() < ans_size) {
ans_size = (int)cand.size(), ans_set = cand;
}
return false;
}
return (int)rem_ver.size() > 0;
}
void transition(int K, int next_ver, bool use) {
int pl_add_size = 0;
vector<pair<int, int>> pl_erase_ver;
if (!erase_rec(next_ver, use, pl_add_size, pl_erase_ver)) {
return postprocessing(K - 1, pl_add_size, pl_erase_ver);
}
if (check())
judge(K - 1);
return postprocessing(K - 1, pl_add_size, pl_erase_ver);
}
int find_max_degree_ver() {
// 次数の高い頂点から探索する
int max_deg = -1, next_ver = -1;
for (int v : rem_ver) {
if (deg[v] > max_deg)
max_deg = deg[v], next_ver = v;
}
if (2 * rem_edge_size > (long long)max_deg * (int)rem_ver.size())
return -1;
return next_ver;
}
void judge(int K) {
int add_size = 0;
vector<pair<int, int>> erase_ver;
if (!preprocessing(K, add_size, erase_ver))
return postprocessing(K, add_size, erase_ver);
if (!check())
return postprocessing(K, add_size, erase_ver);
int next_ver = find_max_degree_ver();
if (next_ver < 0)
return postprocessing(K, add_size, erase_ver);
// 使う場合
transition(K, next_ver, true);
// 使わない場合
if ((int)cand.size() + (int)G[next_ver].size() >= ans_size) {
transition(K, next_ver, false);
}
return postprocessing(K, add_size, erase_ver);
}
public:
vector<int> ans_set;
int ans_size;
MVC(int node_size)
: V(node_size), rem_edge_size(0), G(V), rem_ver(V), deg(V, 0),
edge_info(V) {
for (int i = 0; i < V; i++)
rem_ver[i] = i;
}
void add_edge(int u, int v) {
G[u].push_back((edge){v, (int)G[v].size()});
G[v].push_back((edge){u, (int)G[u].size() - 1});
deg[u]++, deg[v]++, rem_edge_size++;
}
bool solve(int K) {
fp_size = K, ans_size = fp_size + 1;
judge(fp_size);
return ans_size <= fp_size;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, K;
cin >> n >> m >> K;
MVC mvc(n);
rep(i, m) {
int a, b;
cin >> a >> b;
mvc.add_edge(a - 1, b - 1);
}
if (mvc.solve(K)) {
cout << mvc.ans_size << "\n";
} else {
cout << "Impossible\n";
}
return 0;
}
|
insert
| 301 | 301 | 301 | 302 |
TLE
| |
p01671
|
C++
|
Runtime Error
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
struct unifo {
int n;
int uni[200005];
void init(int in) {
n = in;
rep(i, n) uni[i] = i;
}
int find(int p) {
if (uni[p] == p)
return p;
else
return (uni[p] = find(uni[p]));
}
void mer(int p, int q) {
if (!same(p, q))
uni[find(p)] = find(q);
}
bool same(int p, int q) { return find(p) == find(q); }
void out() {
vector<mp> ps;
// printf("%d %d\n",i,find(i));
rep(i, n) ps.push_back(mp(find(i), i));
sort(ps.begin(), ps.end());
printf("uni .. %d elem\n{ ", n);
rep(i, n) {
if (i != 0 && ps[i - 1].fir != ps[i].fir)
printf("}\n{ ");
printf("%d ", ps[i].sec);
}
printf("}\n\n");
}
} uni;
typedef pair<int, mp> mmp;
typedef pair<lli, int> lip;
int n, m;
vector<mp> bvs[100005];
vector<lli> ecs;
vector<mp> eds;
vector<int> ess;
mp vs[100005];
lli sc = 0;
lli ans[100005] = {};
int dep[100005];
void cdfs(int no, int p, int d) {
dep[no] = d;
rep(i, bvs[no].size()) {
mp pa = bvs[no][i];
int to = pa.fir, e = pa.sec;
if (to == p)
continue;
vs[to] = mp(no, e);
cdfs(to, no, d + 1);
}
}
int dub[100005][20];
void lcainit() {
rep(i, n) dub[i][0] = vs[i].fir;
rep(k, 19) {
rep(i, n) { dub[i][k + 1] = dub[dub[i][k]][k]; }
}
}
int getlca(int p, int q) {
if (dep[p] < dep[q])
swap(p, q);
irep(i, 20) {
if (dep[p] - dep[q] >= (1 << i)) {
p = dub[p][i];
}
}
irep(i, 20) {
if (dub[p][i] == dub[q][i])
continue;
p = dub[p][i];
q = dub[q][i];
}
if (p != q) {
p = dub[p][0];
q = dub[q][0];
}
return p;
}
void focus(int fr, int to, lli dco) {
for (;;) {
if (uni.same(fr, to))
break;
fr = uni.find(fr);
mp pa = vs[fr];
int tf = pa.fir, e = pa.sec;
ans[e] = sc + dco - ecs[e];
uni.mer(fr, tf);
fr = tf;
}
}
int main(void) {
scanf("%d%d", &n, &m);
rep(i, m) {
int a, b;
lli c;
scanf("%d%d%lld", &a, &b, &c);
a--;
b--;
ecs.push_back(c);
eds.push_back(mp(a, b));
}
{
vector<lip> v;
rep(i, m) v.push_back(lip(ecs[i], i));
sort(v.begin(), v.end());
rep(i, m) ess.push_back(v[i].sec);
}
{
uni.init(n + 5);
vector<int> v;
int spn = 0;
rep(i, m) {
int e = ess[i];
int a = eds[e].fir, b = eds[e].sec;
if (uni.same(a, b)) {
ans[e] = -2;
v.push_back(e);
continue;
}
ans[e] = -1;
spn++;
bvs[a].push_back(mp(b, e));
bvs[b].push_back(mp(a, e));
sc += ecs[e];
uni.mer(a, b);
}
if (spn != n - 1) {
rep(i, m) printf("-1\n");
return 0;
}
swap(v, ess);
rep(i, m) {
if (ans[i] == -2)
ans[i] = sc;
}
}
cdfs(0, -1, 0);
lcainit();
uni.init(n + 5);
rep(i, ess.size()) {
int e = ess[i];
int a = eds[e].fir, b = eds[e].sec;
if (uni.same(a, b))
continue;
int anc = getlca(a, b);
focus(a, anc, ecs[e]);
focus(b, anc, ecs[e]);
}
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
struct unifo {
int n;
int uni[200005];
void init(int in) {
n = in;
rep(i, n) uni[i] = i;
}
int find(int p) {
if (uni[p] == p)
return p;
else
return (uni[p] = find(uni[p]));
}
void mer(int p, int q) {
if (!same(p, q))
uni[find(p)] = find(q);
}
bool same(int p, int q) { return find(p) == find(q); }
void out() {
vector<mp> ps;
// printf("%d %d\n",i,find(i));
rep(i, n) ps.push_back(mp(find(i), i));
sort(ps.begin(), ps.end());
printf("uni .. %d elem\n{ ", n);
rep(i, n) {
if (i != 0 && ps[i - 1].fir != ps[i].fir)
printf("}\n{ ");
printf("%d ", ps[i].sec);
}
printf("}\n\n");
}
} uni;
typedef pair<int, mp> mmp;
typedef pair<lli, int> lip;
int n, m;
vector<mp> bvs[100005];
vector<lli> ecs;
vector<mp> eds;
vector<int> ess;
mp vs[100005];
lli sc = 0;
lli ans[200005] = {};
int dep[100005];
void cdfs(int no, int p, int d) {
dep[no] = d;
rep(i, bvs[no].size()) {
mp pa = bvs[no][i];
int to = pa.fir, e = pa.sec;
if (to == p)
continue;
vs[to] = mp(no, e);
cdfs(to, no, d + 1);
}
}
int dub[100005][20];
void lcainit() {
rep(i, n) dub[i][0] = vs[i].fir;
rep(k, 19) {
rep(i, n) { dub[i][k + 1] = dub[dub[i][k]][k]; }
}
}
int getlca(int p, int q) {
if (dep[p] < dep[q])
swap(p, q);
irep(i, 20) {
if (dep[p] - dep[q] >= (1 << i)) {
p = dub[p][i];
}
}
irep(i, 20) {
if (dub[p][i] == dub[q][i])
continue;
p = dub[p][i];
q = dub[q][i];
}
if (p != q) {
p = dub[p][0];
q = dub[q][0];
}
return p;
}
void focus(int fr, int to, lli dco) {
for (;;) {
if (uni.same(fr, to))
break;
fr = uni.find(fr);
mp pa = vs[fr];
int tf = pa.fir, e = pa.sec;
ans[e] = sc + dco - ecs[e];
uni.mer(fr, tf);
fr = tf;
}
}
int main(void) {
scanf("%d%d", &n, &m);
rep(i, m) {
int a, b;
lli c;
scanf("%d%d%lld", &a, &b, &c);
a--;
b--;
ecs.push_back(c);
eds.push_back(mp(a, b));
}
{
vector<lip> v;
rep(i, m) v.push_back(lip(ecs[i], i));
sort(v.begin(), v.end());
rep(i, m) ess.push_back(v[i].sec);
}
{
uni.init(n + 5);
vector<int> v;
int spn = 0;
rep(i, m) {
int e = ess[i];
int a = eds[e].fir, b = eds[e].sec;
if (uni.same(a, b)) {
ans[e] = -2;
v.push_back(e);
continue;
}
ans[e] = -1;
spn++;
bvs[a].push_back(mp(b, e));
bvs[b].push_back(mp(a, e));
sc += ecs[e];
uni.mer(a, b);
}
if (spn != n - 1) {
rep(i, m) printf("-1\n");
return 0;
}
swap(v, ess);
rep(i, m) {
if (ans[i] == -2)
ans[i] = sc;
}
}
cdfs(0, -1, 0);
lcainit();
uni.init(n + 5);
rep(i, ess.size()) {
int e = ess[i];
int a = eds[e].fir, b = eds[e].sec;
if (uni.same(a, b))
continue;
int anc = getlca(a, b);
focus(a, anc, ecs[e]);
focus(b, anc, ecs[e]);
}
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
|
replace
| 71 | 72 | 71 | 72 |
0
| |
p01671
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i, k, n) for (int i = (k); i <= (n); i++)
#define red(i, k, n) for (int i = (k); i >= (n); i--)
#define sqr(x) ((x) * (x))
#define clr(x, y) memset((x), (y), sizeof(x))
#define mod 1000000007
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
// const int maxn = ;
struct Edge {
int u, v, w, id;
} e[200010];
bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; }
struct tE {
int v, ord, next;
} te[200010];
int head[100010];
int tet;
LL mst;
LL ans[200010];
int f[100010];
int n, m, cnt;
bool me[200010];
int dep[100010];
int fa[100010];
bool vis[100010];
int fae[100010];
vector<int> ne;
void add(int u, int v, int ord) {
te[++tet].v = v;
te[tet].ord = ord;
te[tet].next = head[u];
head[u] = tet;
}
int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); }
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; ~i; i = te[i].next) {
int v = te[i].v;
if (vis[v])
continue;
fa[v] = u;
fae[v] = i;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int main() {
// #define LOCAL
#ifdef LOCAL
freopen("e:\\read.txt", "r", stdin);
// freopen("e:\\write.txt","w",stdout);
#endif
while (cin >> n >> m) {
ne.clear();
rep(i, 1, m) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
e[i].id = i;
}
sort(e + 1, e + 1 + m, cmp);
rep(i, 1, n) f[i] = i;
mst = cnt = 0;
int a, b;
clr(me, 0);
clr(head, -1);
tet = 0;
rep(i, 1, m) {
a = e[i].u;
b = e[i].v;
if (getf(a) != getf(b)) {
cnt++;
mst += e[i].w;
me[e[i].id] = 1;
f[getf(a)] = getf(b);
add(a, b, i);
add(b, a, i);
} else {
ne.push_back(i);
}
}
if (cnt < n - 1) {
rep(i, 1, m) puts("-1");
continue;
}
rep(i, 1, m) if (!me[i]) ans[i] = mst;
else ans[i] = -1;
rep(i, 1, n) f[i] = i;
clr(dep, 0);
clr(fa, 0);
clr(vis, 0);
dfs(1);
for (int i = 0; i < ne.size(); i++) {
a = e[ne[i]].u;
b = e[ne[i]].v;
while (getf(a) != getf(b)) {
if (dep[a] > dep[b])
swap(a, b);
int k = te[fae[b]].ord;
if (ans[e[k].id] == -1)
ans[e[k].id] = mst - e[k].w + e[ne[i]].w;
else
ans[e[k].id] = MIN(ans[e[k].id], mst - e[k].w + e[ne[i]].w);
f[getf(b)] = getf(fa[b]);
b = fa[b];
}
}
rep(i, 1, m) { printf("%lld\n", ans[i]); }
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i, k, n) for (int i = (k); i <= (n); i++)
#define red(i, k, n) for (int i = (k); i >= (n); i--)
#define sqr(x) ((x) * (x))
#define clr(x, y) memset((x), (y), sizeof(x))
#define mod 1000000007
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
// const int maxn = ;
struct Edge {
int u, v, w, id;
} e[200010];
bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; }
struct tE {
int v, ord, next;
} te[200010];
int head[100010];
int tet;
LL mst;
LL ans[200010];
int f[100010];
int n, m, cnt;
bool me[200010];
int dep[100010];
int fa[100010];
bool vis[100010];
int fae[100010];
vector<int> ne;
void add(int u, int v, int ord) {
te[++tet].v = v;
te[tet].ord = ord;
te[tet].next = head[u];
head[u] = tet;
}
int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); }
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; ~i; i = te[i].next) {
int v = te[i].v;
if (vis[v])
continue;
fa[v] = u;
fae[v] = i;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int main() {
// #define LOCAL
#ifdef LOCAL
freopen("e:\\read.txt", "r", stdin);
// freopen("e:\\write.txt","w",stdout);
#endif
while (cin >> n >> m) {
ne.clear();
rep(i, 1, m) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
e[i].id = i;
}
sort(e + 1, e + 1 + m, cmp);
rep(i, 1, n) f[i] = i;
mst = cnt = 0;
int a, b;
clr(me, 0);
clr(head, -1);
tet = 0;
rep(i, 1, m) {
a = e[i].u;
b = e[i].v;
if (getf(a) != getf(b)) {
cnt++;
mst += e[i].w;
me[e[i].id] = 1;
f[getf(a)] = getf(b);
add(a, b, i);
add(b, a, i);
} else {
ne.push_back(i);
}
}
if (cnt < n - 1) {
rep(i, 1, m) puts("-1");
continue;
}
rep(i, 1, m) if (!me[i]) ans[i] = mst;
else ans[i] = -1;
rep(i, 1, n) f[i] = i;
clr(dep, 0);
clr(fa, 0);
clr(vis, 0);
dfs(1);
for (int i = 0; i < ne.size(); i++) {
a = e[ne[i]].u;
b = e[ne[i]].v;
while (getf(a) != getf(b)) {
if (dep[a] > dep[b])
swap(a, b);
int k = te[fae[b]].ord;
if (ans[e[k].id] == -1)
ans[e[k].id] = mst - e[k].w + e[ne[i]].w;
else
ans[e[k].id] = MIN(ans[e[k].id], mst - e[k].w + e[ne[i]].w);
f[getf(b)] = getf(fa[b]);
b = getf(fa[b]);
}
}
rep(i, 1, m) { printf("%lld\n", ans[i]); }
}
return 0;
}
|
replace
| 130 | 131 | 130 | 131 |
TLE
| |
p01671
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct edge {
int from, to, id;
ll cost;
edge(int f, int t, int i, ll c) : from(f), to(t), id(i), cost(c) {}
bool operator<(const edge &that) const { return cost < that.cost; }
};
using edges = vector<edge>;
class union_find {
vector<int> par;
int find(int a) { return par[a] < 0 ? a : par[a] = find(par[a]); }
public:
union_find(int n) : par(n, -1) {}
bool same(int a, int b) { return find(a) == find(b); }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (par[a] < par[b])
swap(a, b);
else if (par[a] == par[b])
par[b]--;
par[a] = b;
}
};
class HLD {
vector<vector<int>> G;
vector<int> vid, head, heavy, par, dep;
int dfs(int v, int prev) {
par[v] = prev;
int cnt = 1, ma = 0;
for (int to : G[v])
if (to != prev) {
dep[to] = dep[v] + 1;
int c = dfs(to, v);
cnt += c;
if (ma < c) {
ma = c;
heavy[v] = to;
}
}
return cnt;
}
public:
HLD(int n) : G(n), vid(n, -1), head(n), heavy(n, -1), par(n), dep(n) {}
void add(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
void build() {
dfs(0, -1);
int k = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int h = q.front();
q.pop();
for (int i = h; i != -1; i = heavy[i]) {
vid[i] = k++;
head[i] = h;
for (int j : G[i])
if (j != par[i] && j != heavy[i])
q.push(j);
}
}
}
int operator[](int v) { return vid[v]; }
void for_each_edge(int u, int v, function<void(int, int)> f) {
while (true) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
} else {
if (u != v)
f(vid[u] + 1, vid[v]);
break;
}
}
}
};
struct ru {
using type = int;
static type id() { return INT_MAX; }
static type up(type m, type u) { return min(m, u); }
};
template <typename M> class segment_tree {
using T = typename M::type;
int size(int x) {
int res = 1;
while (res < x)
res <<= 1;
return res;
}
const int n;
vector<T> data;
void sub(int l, int r, T val, int node, int lb, int ub) {
if (r <= lb || ub <= l)
return;
if (l <= lb && ub <= r) {
data[node] = M::up(data[node], val);
return;
}
int m = (lb + ub) >> 1;
data[node << 1] = M::up(data[node << 1], data[node]);
data[(node << 1) | 1] = M::up(data[(node << 1) | 1], data[node]);
sub(l, r, val, node << 1, lb, m);
sub(l, r, val, (node << 1) | 1, m, ub);
}
T get(int p, int node, int lb, int ub) {
if (ub - lb == 1)
return data[node];
int m = (lb + ub) >> 1;
if (p < m) {
data[node << 1] = M::up(data[node << 1], data[node]);
return get(p, node << 1, lb, m);
}
data[(node << 1) | 1] = M::up(data[(node << 1) | 1], data[node]);
return get(p, (node << 1) | 1, m, ub);
}
public:
segment_tree(int n) : n(size(n)), data(n * 2, M::id()) {}
void update(int l, int r, T val) { sub(l, r, val, 1, 0, n); }
T find(int p) { return get(p, 1, 0, n); }
};
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
edges es;
for (int i = 0; i < m; i++) {
int a, b;
ll w;
cin >> a >> b >> w;
a--, b--;
es.emplace_back(a, b, i, w);
}
sort(es.begin(), es.end());
union_find uf(n);
vector<int> used(m);
ll sum = 0;
HLD hl(n);
for (int i = 0; i < m; i++) {
int u = es[i].from, v = es[i].to, c = es[i].cost;
if (!uf.same(u, v)) {
used[i] = 1;
uf.unite(u, v);
sum += c;
hl.add(u, v);
}
}
if (count(used.begin(), used.end(), 1) != n - 1) {
for (int i = 0; i < m; i++) {
puts("-1");
}
return 0;
}
hl.build();
segment_tree<ru> st(n + 1);
for (int i = 0; i < m; i++) {
if (!used[i]) {
int u = es[i].from, v = es[i].to, c = es[i].cost;
hl.for_each_edge(u, v, [&](int l, int r) { st.update(l, r + 1, c); });
}
}
vector<ll> res(m);
for (int i = 0; i < m; i++) {
if (!used[i]) {
res[es[i].id] = sum;
continue;
}
ll cost = INT_MAX;
hl.for_each_edge(es[i].from, es[i].to,
[&](int l, int r) { cost = min(cost, (ll)st.find(l)); });
if (cost == INT_MAX) {
res[es[i].id] = -1;
} else {
res[es[i].id] = sum + cost - es[i].cost;
}
}
for (int i = 0; i < m; i++) {
printf("%lld\n", res[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct edge {
int from, to, id;
ll cost;
edge(int f, int t, int i, ll c) : from(f), to(t), id(i), cost(c) {}
bool operator<(const edge &that) const { return cost < that.cost; }
};
using edges = vector<edge>;
class union_find {
vector<int> par;
int find(int a) { return par[a] < 0 ? a : par[a] = find(par[a]); }
public:
union_find(int n) : par(n, -1) {}
bool same(int a, int b) { return find(a) == find(b); }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (par[a] < par[b])
swap(a, b);
else if (par[a] == par[b])
par[b]--;
par[a] = b;
}
};
class HLD {
vector<vector<int>> G;
vector<int> vid, head, heavy, par, dep;
int dfs(int v, int prev) {
par[v] = prev;
int cnt = 1, ma = 0;
for (int to : G[v])
if (to != prev) {
dep[to] = dep[v] + 1;
int c = dfs(to, v);
cnt += c;
if (ma < c) {
ma = c;
heavy[v] = to;
}
}
return cnt;
}
public:
HLD(int n) : G(n), vid(n, -1), head(n), heavy(n, -1), par(n), dep(n) {}
void add(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
void build() {
dfs(0, -1);
int k = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int h = q.front();
q.pop();
for (int i = h; i != -1; i = heavy[i]) {
vid[i] = k++;
head[i] = h;
for (int j : G[i])
if (j != par[i] && j != heavy[i])
q.push(j);
}
}
}
int operator[](int v) { return vid[v]; }
void for_each_edge(int u, int v, function<void(int, int)> f) {
while (true) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
} else {
if (u != v)
f(vid[u] + 1, vid[v]);
break;
}
}
}
};
struct ru {
using type = int;
static type id() { return INT_MAX; }
static type up(type m, type u) { return min(m, u); }
};
template <typename M> class segment_tree {
using T = typename M::type;
int size(int x) {
int res = 1;
while (res < x)
res <<= 1;
return res;
}
const int n;
vector<T> data;
void sub(int l, int r, T val, int node, int lb, int ub) {
if (r <= lb || ub <= l)
return;
if (l <= lb && ub <= r) {
data[node] = M::up(data[node], val);
return;
}
int m = (lb + ub) >> 1;
data[node << 1] = M::up(data[node << 1], data[node]);
data[(node << 1) | 1] = M::up(data[(node << 1) | 1], data[node]);
sub(l, r, val, node << 1, lb, m);
sub(l, r, val, (node << 1) | 1, m, ub);
}
T get(int p, int node, int lb, int ub) {
if (ub - lb == 1)
return data[node];
int m = (lb + ub) >> 1;
if (p < m) {
data[node << 1] = M::up(data[node << 1], data[node]);
return get(p, node << 1, lb, m);
}
data[(node << 1) | 1] = M::up(data[(node << 1) | 1], data[node]);
return get(p, (node << 1) | 1, m, ub);
}
public:
segment_tree(int n_) : n(size(n_)), data(n * 2, M::id()) {}
void update(int l, int r, T val) { sub(l, r, val, 1, 0, n); }
T find(int p) { return get(p, 1, 0, n); }
};
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
edges es;
for (int i = 0; i < m; i++) {
int a, b;
ll w;
cin >> a >> b >> w;
a--, b--;
es.emplace_back(a, b, i, w);
}
sort(es.begin(), es.end());
union_find uf(n);
vector<int> used(m);
ll sum = 0;
HLD hl(n);
for (int i = 0; i < m; i++) {
int u = es[i].from, v = es[i].to, c = es[i].cost;
if (!uf.same(u, v)) {
used[i] = 1;
uf.unite(u, v);
sum += c;
hl.add(u, v);
}
}
if (count(used.begin(), used.end(), 1) != n - 1) {
for (int i = 0; i < m; i++) {
puts("-1");
}
return 0;
}
hl.build();
segment_tree<ru> st(n + 1);
for (int i = 0; i < m; i++) {
if (!used[i]) {
int u = es[i].from, v = es[i].to, c = es[i].cost;
hl.for_each_edge(u, v, [&](int l, int r) { st.update(l, r + 1, c); });
}
}
vector<ll> res(m);
for (int i = 0; i < m; i++) {
if (!used[i]) {
res[es[i].id] = sum;
continue;
}
ll cost = INT_MAX;
hl.for_each_edge(es[i].from, es[i].to,
[&](int l, int r) { cost = min(cost, (ll)st.find(l)); });
if (cost == INT_MAX) {
res[es[i].id] = -1;
} else {
res[es[i].id] = sum + cost - es[i].cost;
}
}
for (int i = 0; i < m; i++) {
printf("%lld\n", res[i]);
}
return 0;
}
|
replace
| 134 | 135 | 134 | 135 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p01671
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
#define rank Asdfiasofsad
class UF {
public:
int V;
vector<int> par, rank;
UF() {}
UF(int V) : V(V), par(V), rank(V, 0) {
for (int i = 0; i < V; i++)
par[i] = i;
}
int find(int x) {
assert(x < V);
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
class RMQ2 {
public:
typedef long long ll;
struct data {
bool type; // 0 - empty , 1 - update
ll value;
};
ll n;
vector<ll> dat;
vector<data> td;
RMQ2() { n = -1; }
RMQ2(int n_) {
n = 1;
while (n < n_)
n *= 2;
td.resize(2 * n - 1, (data){0, 0});
dat.resize(2 * n - 1, INF);
}
//[a,b)?????????x????????´???update(a,b,x)
ll update(int a, int b, int x, bool flg = true, int k = 0, int l = 0,
int r = -1) {
if (r == -1)
r = n, assert(a < n && b <= n);
if (r <= a || b <= l)
return flg ? dat[k] : INF;
if (a <= l && r <= b) {
if (flg == true) {
td[k] = (data){1, x};
dat[k] = x;
}
return dat[k];
}
if (td[k].type) {
dat[k * 2 + 1] = dat[k * 2 + 2] = td[k].value;
td[k * 2 + 1] = td[k * 2 + 2] = (data){1, td[k].value};
td[k].type = 0;
}
ll vl = update(a, b, x, flg, k * 2 + 1, l, (l + r) / 2);
ll vr = update(a, b, x, flg, k * 2 + 2, (l + r) / 2, r);
if (flg == true)
dat[k] = min(vl, vr);
return min(vl, vr);
}
//[a,b)???????°????????????????find(a,b);
ll find(int a, int b) { return update(a, b, 0, false); };
};
/*???HL????§£???O(V+E)*/
class HLD {
public:
int V; //???????????°
int root; //??????????????????
vector<vector<int>> G; // Grapth
vector<int> par; // parent: par[??????????????????]->??????????????????
vector<int> Hchild; //???????????????????????????Heavy Child???????????????:
//Hedge[??????????????????]->??????????????????
vector<int> number; //???????????????????????????????????????????????????:
//number[??????????????????]->????????´??????????????????
vector<int> id; //??????????????´????????°???????????????????????????:
//id[????????´??????????????????]->??????????????????
vector<int> group; //???????????????????±?????????°???????????????:
//group[??????????????????]->??????????????????
vector<int>
depth; //?????????????????????????????±???: depth[??????????????????]
bool ok; // build()???????????§?????????
HLD() { root = V = -1; }
HLD(int V)
: V(V), root(0), G(V), par(V, -1), Hchild(V, -1), number(V, -1),
id(V, -1), group(V, -1), depth(V, 0), ok(0) {}
void add_edge(int a, int b) {
assert(a < V && b < V);
G[a].push_back(b);
G[b].push_back(a);
}
void build(int root = 0) {
this->root = root;
ok = 1;
weightParent(); // weight and parent
int num = 0;
numbering(num);
depthGroup();
}
int weightParent(int pos = -1, int pre = -1) {
if (pos == -1)
pos = root;
par[pos] = pre;
int mx = -1, weight = 1;
for (int to : G[pos]) {
if (to == pre)
continue;
int cweight = weightParent(to, pos);
weight += cweight;
if (mx < cweight)
Hchild[pos] = to, mx = cweight;
}
return weight;
}
void depthGroup(int pos = -1, int pre = -1, int dep = 0, int g = -1) {
if (pos == -1)
pos = g = root;
depth[pos] = dep;
group[pos] = g;
for (int to : G[pos])
if (to != pre)
depthGroup(to, pos, dep + 1, Hchild[pos] == to ? g : to);
}
void numbering(int &cnt, int pos = -1, int pre = -1) {
if (pos == -1)
pos = root;
number[pos] = cnt++;
id[number[pos]] = pos;
if (Hchild[pos] >= 0)
numbering(cnt, Hchild[pos], pos);
for (int to : G[pos])
if (to != pre && to != Hchild[pos])
numbering(cnt, to, pos);
}
typedef pair<int, int> P;
vector<P>
paths; //[u,v]????????????
//????????????????????£???????????????????????????????§£?????????(?????§logN??????????????????????????????)
vector<P> lightEdges; // paths????????¶???????????¨??????
void buildPaths(int u, int v, int init = true) {
if (init)
paths.clear(), lightEdges.clear(), assert(ok);
if (depth[group[u]] < depth[group[v]])
swap(u, v); // ??±???????????????????????????????????????
int nu = number[u], nv = number[v];
if (same(u, v)) {
paths.push_back(P(min(nu, nv), max(nu, nv)));
return;
}
int gu = group[u];
buildPaths(par[gu], v, false);
paths.push_back(P(number[gu], number[u]));
lightEdges.push_back(P(number[par[gu]], number[gu]));
}
pair<vector<P>, vector<P>> getPath(int u, int v) {
buildPaths(u, v);
return make_pair(paths, lightEdges);
}
/*(??????????????????,??????????????????)*/
bool same(int a, int b) { return group[a] == group[b]; }
};
typedef pair<int, int> P;
typedef pair<P, P> PP;
int n, m;
vector<vector<int>> G;
vector<PP> edge;
vector<int> used;
HLD hld;
RMQ2 rmq;
int kuraskal() {
sort(edge.begin(), edge.end());
hld = HLD(n);
used.resize(m, 0);
UF U(n);
int sum = 0, cnt = 0;
for (int i = 0; i < m; i++) {
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
if (U.same(a, b))
continue;
cnt++;
U.unite(a, b);
hld.add_edge(a, b);
used[i] = 1;
sum += c;
}
if (cnt != n - 1)
return INF;
return sum;
}
void build() {
rmq = RMQ2(n);
hld.build();
for (int i = m - 1; i >= 0; i--) {
if (used[i])
continue;
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
pair<vector<P>, vector<P>> path = hld.getPath(a, b);
vector<P> &hpath = path.first, &ledge = path.second;
if (hld.depth[a] > hld.depth[b])
swap(a, b);
hpath[0].first++;
for (P p : hpath)
rmq.update(p.first, p.second + 1, c);
for (P e : ledge)
rmq.update(e.second, e.second + 1, c);
}
}
signed main() {
cin >> n >> m;
G.resize(n);
edge.resize(m);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
edge[i] = PP(P(c, i), P(a, b));
}
int mincost = kuraskal();
build();
vector<int> ans(m, mincost);
for (int i = 0; i < m; i++) {
if (!used[i])
continue;
int idx = edge[i].first.second;
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
if (hld.depth[a] > hld.depth[b])
swap(a, b);
int c2 = rmq.find(hld.number[b], hld.number[b] + 1);
ans[idx] = mincost - c + c2;
}
for (int a : ans)
cout << (a >= INF ? -1 : a) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
#define rank Asdfiasofsad
class UF {
public:
int V;
vector<int> par, rank;
UF() {}
UF(int V) : V(V), par(V), rank(V, 0) {
for (int i = 0; i < V; i++)
par[i] = i;
}
int find(int x) {
assert(x < V);
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
class RMQ2 {
public:
typedef long long ll;
struct data {
bool type; // 0 - empty , 1 - update
ll value;
};
ll n;
vector<ll> dat;
vector<data> td;
RMQ2() { n = -1; }
RMQ2(int n_) {
n = 1;
while (n < n_)
n *= 2;
td.resize(2 * n - 1, (data){0, 0});
dat.resize(2 * n - 1, INF);
}
//[a,b)?????????x????????´???update(a,b,x)
ll update(int a, int b, int x, bool flg = true, int k = 0, int l = 0,
int r = -1) {
if (r == -1)
r = n, assert(a < n && b <= n);
if (r <= a || b <= l)
return flg ? dat[k] : INF;
if (a <= l && r <= b) {
if (flg == true) {
td[k] = (data){1, x};
dat[k] = x;
}
return dat[k];
}
if (td[k].type) {
dat[k * 2 + 1] = dat[k * 2 + 2] = td[k].value;
td[k * 2 + 1] = td[k * 2 + 2] = (data){1, td[k].value};
td[k].type = 0;
}
ll vl = update(a, b, x, flg, k * 2 + 1, l, (l + r) / 2);
ll vr = update(a, b, x, flg, k * 2 + 2, (l + r) / 2, r);
if (flg == true)
dat[k] = min(vl, vr);
return min(vl, vr);
}
//[a,b)???????°????????????????find(a,b);
ll find(int a, int b) { return update(a, b, 0, false); };
};
/*???HL????§£???O(V+E)*/
class HLD {
public:
int V; //???????????°
int root; //??????????????????
vector<vector<int>> G; // Grapth
vector<int> par; // parent: par[??????????????????]->??????????????????
vector<int> Hchild; //???????????????????????????Heavy Child???????????????:
//Hedge[??????????????????]->??????????????????
vector<int> number; //???????????????????????????????????????????????????:
//number[??????????????????]->????????´??????????????????
vector<int> id; //??????????????´????????°???????????????????????????:
//id[????????´??????????????????]->??????????????????
vector<int> group; //???????????????????±?????????°???????????????:
//group[??????????????????]->??????????????????
vector<int>
depth; //?????????????????????????????±???: depth[??????????????????]
bool ok; // build()???????????§?????????
HLD() { root = V = -1; }
HLD(int V)
: V(V), root(0), G(V), par(V, -1), Hchild(V, -1), number(V, -1),
id(V, -1), group(V, -1), depth(V, 0), ok(0) {}
void add_edge(int a, int b) {
assert(a < V && b < V);
G[a].push_back(b);
G[b].push_back(a);
}
void build(int root = 0) {
this->root = root;
ok = 1;
weightParent(); // weight and parent
int num = 0;
numbering(num);
depthGroup();
}
int weightParent(int pos = -1, int pre = -1) {
if (pos == -1)
pos = root;
par[pos] = pre;
int mx = -1, weight = 1;
for (int to : G[pos]) {
if (to == pre)
continue;
int cweight = weightParent(to, pos);
weight += cweight;
if (mx < cweight)
Hchild[pos] = to, mx = cweight;
}
return weight;
}
void depthGroup(int pos = -1, int pre = -1, int dep = 0, int g = -1) {
if (pos == -1)
pos = g = root;
depth[pos] = dep;
group[pos] = g;
for (int to : G[pos])
if (to != pre)
depthGroup(to, pos, dep + 1, Hchild[pos] == to ? g : to);
}
void numbering(int &cnt, int pos = -1, int pre = -1) {
if (pos == -1)
pos = root;
number[pos] = cnt++;
id[number[pos]] = pos;
if (Hchild[pos] >= 0)
numbering(cnt, Hchild[pos], pos);
for (int to : G[pos])
if (to != pre && to != Hchild[pos])
numbering(cnt, to, pos);
}
typedef pair<int, int> P;
vector<P>
paths; //[u,v]????????????
//????????????????????£???????????????????????????????§£?????????(?????§logN??????????????????????????????)
vector<P> lightEdges; // paths????????¶???????????¨??????
void buildPaths(int u, int v, int init = true) {
if (init)
paths.clear(), lightEdges.clear(), assert(ok);
if (depth[group[u]] < depth[group[v]])
swap(u, v); // ??±???????????????????????????????????????
int nu = number[u], nv = number[v];
if (same(u, v)) {
paths.push_back(P(min(nu, nv), max(nu, nv)));
return;
}
int gu = group[u];
buildPaths(par[gu], v, false);
paths.push_back(P(number[gu], number[u]));
lightEdges.push_back(P(number[par[gu]], number[gu]));
}
pair<vector<P>, vector<P>> getPath(int u, int v) {
buildPaths(u, v);
return make_pair(paths, lightEdges);
}
/*(??????????????????,??????????????????)*/
bool same(int a, int b) { return group[a] == group[b]; }
};
typedef pair<int, int> P;
typedef pair<P, P> PP;
int n, m;
vector<vector<int>> G;
vector<PP> edge;
vector<int> used;
HLD hld;
RMQ2 rmq;
int kuraskal() {
sort(edge.begin(), edge.end());
hld = HLD(n);
used.resize(m, 0);
UF U(n);
int sum = 0, cnt = 0;
for (int i = 0; i < m; i++) {
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
if (U.same(a, b))
continue;
cnt++;
U.unite(a, b);
hld.add_edge(a, b);
used[i] = 1;
sum += c;
}
if (cnt != n - 1)
return INF;
return sum;
}
void build() {
rmq = RMQ2(n);
hld.build(rand() % n);
for (int i = m - 1; i >= 0; i--) {
if (used[i])
continue;
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
pair<vector<P>, vector<P>> path = hld.getPath(a, b);
vector<P> &hpath = path.first, &ledge = path.second;
if (hld.depth[a] > hld.depth[b])
swap(a, b);
hpath[0].first++;
for (P p : hpath)
rmq.update(p.first, p.second + 1, c);
for (P e : ledge)
rmq.update(e.second, e.second + 1, c);
}
}
signed main() {
cin >> n >> m;
G.resize(n);
edge.resize(m);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
edge[i] = PP(P(c, i), P(a, b));
}
int mincost = kuraskal();
build();
vector<int> ans(m, mincost);
for (int i = 0; i < m; i++) {
if (!used[i])
continue;
int idx = edge[i].first.second;
int a = edge[i].second.first;
int b = edge[i].second.second;
int c = edge[i].first.first;
if (hld.depth[a] > hld.depth[b])
swap(a, b);
int c2 = rmq.find(hld.number[b], hld.number[b] + 1);
ans[idx] = mincost - c + c2;
}
for (int a : ans)
cout << (a >= INF ? -1 : a) << endl;
return 0;
}
|
replace
| 240 | 242 | 240 | 241 |
0
| |
p01672
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c) \
for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
os << "[";
FOR(it, t) {
if (it != t.begin())
os << ",";
os << *it;
}
os << "]";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
os << "{";
FOR(it, t) {
if (it != t.begin())
os << ",";
os << *it;
}
os << "}";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
template <int B> // n <= 1<<B
struct FFT {
typedef complex<double> cd;
vi m2[B];
vector<cd> power[2 * B];
FFT() {
REP(e, B) {
m2[e].resize(1 << e);
REP(m, 1 << e) REP(i, e) if (m & (1 << i)) m2[e][m] |= (1 << (e - 1 - i));
power[e].resize(1 << e);
power[B + e].resize(1 << e);
power[e][0] = power[B + e][0] = cd(1, 0);
cd w(cos(acos(-1.0) / (1 << e)), sin(acos(-1.0) / (1 << e)));
REPS(j, (1 << e) - 1) {
power[e][j] = power[e][j - 1] * w;
power[B + e][j] = power[B + e][j - 1] * conj(w);
}
}
}
void transform(cd *f, int N, bool inv) {
int e;
for (e = 0;; e++)
if (N == (1 << e))
break;
REP(m, N) if (m < m2[e][m]) swap(f[m], f[m2[e][m]]);
for (int t = 0, t2 = 1; t2 < N; t++, t2 += t2) {
cd *po = power[inv * B + t].data();
for (int i = 0; i < N; i += t2 + t2) {
for (int j = 0; j < (1 << t); j++) {
cd x = f[i + j];
cd y = f[i + t2 + j] * po[j];
f[i + j] = x + y;
f[i + t2 + j] = x - y;
}
}
}
if (inv)
for (int i = 0; i < N; i++)
f[i] /= N;
}
template <typename T>
vector<cd> convolution(const vector<T> &f, const vector<T> &g) {
const int n = f.size(), m = g.size();
int n2 = 1;
while (n2 < 2 * max(f.size(), g.size()))
n2 *= 2;
vector<cd> cf(n2), cg(n2);
REP(i, n) cf[i] = f[i];
REP(i, m) cg[i] = g[i];
transform(cf.data(), n2, false);
transform(cg.data(), n2, false);
REP(i, n2) cf[i] *= cg[i];
transform(cf.data(), n2, true);
return cf;
}
};
FFT<22> fft;
int T, n, m;
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
scanf("%d", &n);
vi d(2 * n * n);
int sum = 0;
REP(i, n) REP(j, n) {
int x;
scanf("%d", &x);
d[i * (n + n) + j] = x;
sum += x;
}
vi e(RALL(d));
auto ret = fft.convolution(d, e);
map<int, ll> cnt;
double dsum = 0;
ll all = 0;
REP(i, 2 * n * n) {
int x = ret[i].real() + 0.5;
int w = 2 * n * n - 1 - i;
if (w == 0) {
x -= sum;
x /= 2;
}
if (x) {
int dy = w / (2 * n);
int dx = w % (2 * n);
if (dx >= n) {
dy++;
dx = (2 * n - dx);
}
cnt[dx * dx + dy * dy] += x;
all += x;
dsum += sqrt(dx * dx + dy * dy) * x;
}
}
printf("%.20f\n", dsum / all);
int putcnt = 0;
for (auto it : cnt) {
printf("%d %lld\n", it.first, it.second);
if (++putcnt == 10000)
break;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c) \
for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
os << "[";
FOR(it, t) {
if (it != t.begin())
os << ",";
os << *it;
}
os << "]";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
os << "{";
FOR(it, t) {
if (it != t.begin())
os << ",";
os << *it;
}
os << "}";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
template <int B> // n <= 1<<B
struct FFT {
typedef complex<double> cd;
vi m2[B];
vector<cd> power[2 * B];
FFT() {
REP(e, B) {
m2[e].resize(1 << e);
REP(m, 1 << e) REP(i, e) if (m & (1 << i)) m2[e][m] |= (1 << (e - 1 - i));
power[e].resize(1 << e);
power[B + e].resize(1 << e);
power[e][0] = power[B + e][0] = cd(1, 0);
cd w(cos(acos(-1.0) / (1 << e)), sin(acos(-1.0) / (1 << e)));
REPS(j, (1 << e) - 1) {
power[e][j] = power[e][j - 1] * w;
power[B + e][j] = power[B + e][j - 1] * conj(w);
}
}
}
void transform(cd *f, int N, bool inv) {
int e;
for (e = 0;; e++)
if (N == (1 << e))
break;
REP(m, N) if (m < m2[e][m]) swap(f[m], f[m2[e][m]]);
for (int t = 0, t2 = 1; t2 < N; t++, t2 += t2) {
cd *po = power[inv * B + t].data();
for (int i = 0; i < N; i += t2 + t2) {
for (int j = 0; j < (1 << t); j++) {
cd x = f[i + j];
cd y = f[i + t2 + j] * po[j];
f[i + j] = x + y;
f[i + t2 + j] = x - y;
}
}
}
if (inv)
for (int i = 0; i < N; i++)
f[i] /= N;
}
template <typename T>
vector<cd> convolution(const vector<T> &f, const vector<T> &g) {
const int n = f.size(), m = g.size();
int n2 = 1;
while (n2 < 2 * max(f.size(), g.size()))
n2 *= 2;
vector<cd> cf(n2), cg(n2);
REP(i, n) cf[i] = f[i];
REP(i, m) cg[i] = g[i];
transform(cf.data(), n2, false);
transform(cg.data(), n2, false);
REP(i, n2) cf[i] *= cg[i];
transform(cf.data(), n2, true);
return cf;
}
};
FFT<23> fft;
int T, n, m;
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
scanf("%d", &n);
vi d(2 * n * n);
int sum = 0;
REP(i, n) REP(j, n) {
int x;
scanf("%d", &x);
d[i * (n + n) + j] = x;
sum += x;
}
vi e(RALL(d));
auto ret = fft.convolution(d, e);
map<int, ll> cnt;
double dsum = 0;
ll all = 0;
REP(i, 2 * n * n) {
int x = ret[i].real() + 0.5;
int w = 2 * n * n - 1 - i;
if (w == 0) {
x -= sum;
x /= 2;
}
if (x) {
int dy = w / (2 * n);
int dx = w % (2 * n);
if (dx >= n) {
dy++;
dx = (2 * n - dx);
}
cnt[dx * dx + dy * dy] += x;
all += x;
dsum += sqrt(dx * dx + dy * dy) * x;
}
}
printf("%.20f\n", dsum / all);
int putcnt = 0;
for (auto it : cnt) {
printf("%d %lld\n", it.first, it.second);
if (++putcnt == 10000)
break;
}
return 0;
}
|
replace
| 160 | 161 | 160 | 161 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p01675
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int PMAX = 1000000;
template <typename T> struct node {
T val, add;
node *l, *r;
void init(T v, T a, node *left, node *right) {
val = v;
add = a;
l = left;
r = right;
}
};
node<ll> pool[PMAX];
template <typename T> class SaikyoSegmentTree {
const ll n;
const T id;
int it;
vector<node<T> *> root;
T getval(node<T> *x) { return x == nullptr ? id : x->val; }
T getadd(node<T> *x) { return x == nullptr ? id : x->add; }
node<T> *getl(node<T> *x) { return x == nullptr ? nullptr : x->l; }
node<T> *getr(node<T> *x) { return x == nullptr ? nullptr : x->r; }
ll size(ll n) {
assert(n < (1ll << 62ll));
ll res = 1;
while (res < n)
res <<= 1;
return res;
}
node<T> *new_node(T val, T add, node<T> *l = nullptr, node<T> *r = nullptr) {
pool[it].init(val, add, l, r);
return &pool[it++];
}
node<T> *fix(node<T> *x, ll l, ll r, T val, ll lb, ll ub) {
if (r <= lb || ub <= l)
return x;
if (l <= lb && ub <= r)
return new_node(getval(x), getadd(x) + val, getl(x), getr(x));
ll c = (lb + ub) / 2;
auto nl = fix(getl(x), l, r, val, lb, c);
auto nr = fix(getr(x), l, r, val, c, ub);
return new_node(getval(x) + val * (min(r, ub) - max(l, lb)), getadd(x), nl,
nr);
}
T sub(node<T> *x, ll l, ll r, ll lb, ll ub) {
if (x == nullptr)
return id;
if (ub <= l || r <= lb)
return id;
if (l <= lb && ub <= r)
return x->val + x->add * (ub - lb);
ll c = (lb + ub) / 2;
return sub(getl(x), l, r, lb, c) + sub(getr(x), l, r, c, ub) +
x->add * (min(r, ub) - max(l, lb));
}
public:
SaikyoSegmentTree(ll n_, T id_) : n(size(n_)), id(id_), it(0) {
root.push_back(nullptr);
}
void add(ll l, ll r, T val, int rt = -1) {
if (rt == -1)
rt = root.size() - 1;
assert(0 <= rt && rt < (int)root.size());
root.push_back(fix(root[rt], l, r + 1, val, 0, n));
}
T find(ll l, ll r, int rt = -1) {
if (rt == -1)
rt = root.size() - 1;
assert(0 <= rt && rt < (int)root.size());
return sub(root[rt], l, r + 1, 0, n);
}
};
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
ll N, M, Q;
cin >> N >> M >> Q;
vector<tuple<int, int, int>> vab(M);
for (int i = 0; i < M; i++) {
cin >> get<1>(vab[i]) >> get<2>(vab[i]) >> get<0>(vab[i]);
get<1>(vab[i])--;
get<2>(vab[i])--;
}
sort(vab.begin(), vab.end());
SaikyoSegmentTree<ll> sst(N, 0);
for (int i = 0; i < M; i++) {
sst.add(get<1>(vab[i]), get<2>(vab[i]), 1);
}
ll x, y, j;
while (Q--) {
cin >> x >> y >> j;
x--;
y--;
ll l = 0, r = M;
while (l + 1 < r) {
ll c = (l + r) / 2;
if (sst.find(x, y, c) >= j) {
r = c;
} else {
l = c;
}
}
printf("%d\n", get<0>(vab[r - 1]));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int PMAX = 10000000;
template <typename T> struct node {
T val, add;
node *l, *r;
void init(T v, T a, node *left, node *right) {
val = v;
add = a;
l = left;
r = right;
}
};
node<ll> pool[PMAX];
template <typename T> class SaikyoSegmentTree {
const ll n;
const T id;
int it;
vector<node<T> *> root;
T getval(node<T> *x) { return x == nullptr ? id : x->val; }
T getadd(node<T> *x) { return x == nullptr ? id : x->add; }
node<T> *getl(node<T> *x) { return x == nullptr ? nullptr : x->l; }
node<T> *getr(node<T> *x) { return x == nullptr ? nullptr : x->r; }
ll size(ll n) {
assert(n < (1ll << 62ll));
ll res = 1;
while (res < n)
res <<= 1;
return res;
}
node<T> *new_node(T val, T add, node<T> *l = nullptr, node<T> *r = nullptr) {
pool[it].init(val, add, l, r);
return &pool[it++];
}
node<T> *fix(node<T> *x, ll l, ll r, T val, ll lb, ll ub) {
if (r <= lb || ub <= l)
return x;
if (l <= lb && ub <= r)
return new_node(getval(x), getadd(x) + val, getl(x), getr(x));
ll c = (lb + ub) / 2;
auto nl = fix(getl(x), l, r, val, lb, c);
auto nr = fix(getr(x), l, r, val, c, ub);
return new_node(getval(x) + val * (min(r, ub) - max(l, lb)), getadd(x), nl,
nr);
}
T sub(node<T> *x, ll l, ll r, ll lb, ll ub) {
if (x == nullptr)
return id;
if (ub <= l || r <= lb)
return id;
if (l <= lb && ub <= r)
return x->val + x->add * (ub - lb);
ll c = (lb + ub) / 2;
return sub(getl(x), l, r, lb, c) + sub(getr(x), l, r, c, ub) +
x->add * (min(r, ub) - max(l, lb));
}
public:
SaikyoSegmentTree(ll n_, T id_) : n(size(n_)), id(id_), it(0) {
root.push_back(nullptr);
}
void add(ll l, ll r, T val, int rt = -1) {
if (rt == -1)
rt = root.size() - 1;
assert(0 <= rt && rt < (int)root.size());
root.push_back(fix(root[rt], l, r + 1, val, 0, n));
}
T find(ll l, ll r, int rt = -1) {
if (rt == -1)
rt = root.size() - 1;
assert(0 <= rt && rt < (int)root.size());
return sub(root[rt], l, r + 1, 0, n);
}
};
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
ll N, M, Q;
cin >> N >> M >> Q;
vector<tuple<int, int, int>> vab(M);
for (int i = 0; i < M; i++) {
cin >> get<1>(vab[i]) >> get<2>(vab[i]) >> get<0>(vab[i]);
get<1>(vab[i])--;
get<2>(vab[i])--;
}
sort(vab.begin(), vab.end());
SaikyoSegmentTree<ll> sst(N, 0);
for (int i = 0; i < M; i++) {
sst.add(get<1>(vab[i]), get<2>(vab[i]), 1);
}
ll x, y, j;
while (Q--) {
cin >> x >> y >> j;
x--;
y--;
ll l = 0, r = M;
while (l + 1 < r) {
ll c = (l + r) / 2;
if (sst.find(x, y, c) >= j) {
r = c;
} else {
l = c;
}
}
printf("%d\n", get<0>(vab[r - 1]));
}
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p01676
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, from;
edge(int to, int from) : to(to), from(from) {}
edge() {}
};
int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;
int main() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int s, t;
cin >> s >> t;
--s;
--t;
E.push_back(edge(t, s));
G[s].insert(i);
G[t].insert(i);
}
bool ed = true;
int res = 0;
while (ed) {
int minid = 0;
while (G[minid].empty())
minid++;
for (int i = 0; i < N; i++) {
if (G[i].empty())
continue;
if (G[minid].size() > G[i].size())
minid = i;
}
// cout << G[minid].size() << endl;
res += G[minid].size() - 1;
for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
used[*it] = true;
// cout << "new " << *it << endl;
for (int i = 0; i < N; i++) {
if (i == minid)
continue;
G[i].erase(*it);
}
}
G[minid].clear();
ed = false;
for (int i = 0; i < M; i++)
if (!used[i])
ed = true;
}
cout << res << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, from;
edge(int to, int from) : to(to), from(from) {}
edge() {}
};
int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;
int main() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int s, t;
cin >> s >> t;
--s;
--t;
E.push_back(edge(t, s));
G[s].insert(i);
G[t].insert(i);
}
if (M == 0) {
cout << 0 << endl;
return 0;
}
bool ed = true;
int res = 0;
while (ed) {
int minid = 0;
while (G[minid].empty())
minid++;
for (int i = 0; i < N; i++) {
if (G[i].empty())
continue;
if (G[minid].size() > G[i].size())
minid = i;
}
// cout << G[minid].size() << endl;
res += G[minid].size() - 1;
for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
used[*it] = true;
// cout << "new " << *it << endl;
for (int i = 0; i < N; i++) {
if (i == minid)
continue;
G[i].erase(*it);
}
}
G[minid].clear();
ed = false;
for (int i = 0; i < M; i++)
if (!used[i])
ed = true;
}
cout << res << endl;
}
|
insert
| 24 | 24 | 24 | 28 |
0
| |
p01679
|
C++
|
Time Limit Exceeded
|
#include <algorithm> // other
#include <bitset>
#include <cassert> // c
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream> // io
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <vector> // container
using namespace std;
typedef long long ll;
#define ALL(c) (begin(c)), (end(c))
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORr(i, 0, n)
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define FORr(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); --i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
// debug
#define DUMP(x) cerr << #x << " = " << (x)
#define LINE() cerr << " (L" << __LINE__ << ")"
class range {
private:
struct Iter {
int v;
int operator*() { return v; }
bool operator!=(Iter &itr) { return v < itr.v; }
void operator++() { ++v; }
};
Iter i, n;
public:
range(int n) : i({0}), n({n}) {}
range(int i, int n) : i({i}), n({n}) {}
Iter &begin() { return i; }
Iter &end() { return n; }
};
// output
template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) {
REP(i, as.size()) {
if (i != 0)
os << " ";
os << as[i];
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &as) {
REP(i, as.size()) {
if (i != 0)
os << endl;
os << as[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &ss) {
for (auto a : ss) {
if (a != ss.begin())
os << " ";
os << a;
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
bool isF = true;
for (auto &p : m) {
if (!isF)
os << endl;
os << p;
isF = false;
}
return os;
}
template <typename T1> ostream &operator<<(ostream &os, const tuple<T1> &t) {
os << get<0>(t);
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const tuple<T1, T2> &t) {
os << get<0>(t) << " " << get<1>(t);
return os;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t) << " " << get<5>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6, T7> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t) << " " << get<5>(t) << " " << get<6>(t);
return os;
}
// input
char tmp[1000];
#define nextInt(n) scanf("%d", &n)
#define nextLong(n) scanf("%lld", &n) // I64d
#define nextDouble(n) scanf("%lf", &n)
#define nextChar(n) scanf("%c", &n)
#define nextString(n) \
scanf("%s", tmp); \
n = tmp
// values
template <typename T> T INF() { assert(false); };
template <> int INF<int>() { return 1 << 28; };
template <> ll INF<ll>() { return 1LL << 58; };
template <> double INF<double>() { return 1e16; };
template <class T> T EPS() { assert(false); };
template <> int EPS<int>() { return 1; };
template <> ll EPS<ll>() { return 1LL; };
template <> double EPS<double>() { return 1e-8; };
template <> long double EPS<long double>() { return 1e-8; };
template <typename T, typename U> T pmod(T v, U M) { return (v % M + M) % M; }
typedef int Cost;
Cost CINF = 1 << 28;
struct Edge {
int f, t;
Cost c;
Edge(int f, int t, Cost c) : f(f), t(t), c(c){};
bool operator<(Edge r) const { return c < r.c; }
bool operator>(Edge r) const { return c > r.c; }
};
typedef vector<vector<Edge>> Graph;
struct Task {
int prev, pos;
Cost c;
Task(int prev, int pos, Cost c) : prev(prev), pos(pos), c(c){};
bool operator>(const Task &r) const { return c > r.c; }
};
vector<Cost> dijkstra(const Graph &g, const int s, vector<int> &prev) {
const int V = g.size();
vector<Cost> d(V, CINF);
d[s] = 0;
fill(ALL(prev), -2);
priority_queue<Task, vector<Task>, greater<Task>> que;
que.push(Task(-1, s, 0)); // [ ,e,,f, ] <=> e.cost < e.cost
vector<bool> visited(V);
while (!que.empty()) {
Task task = que.top();
que.pop();
if (visited[task.pos])
continue;
visited[task.pos] = true;
prev[task.pos] = task.prev;
EACH(e, g[task.pos]) if (d[e->t] > d[e->f] + e->c) {
d[e->t] = d[e->f] + e->c;
que.push(Task(e->f, e->t, d[e->t]));
}
}
return d;
}
vector<Cost> dijkstra(const Graph &g, const int s) {
vector<int> prev(g.size());
return dijkstra(g, s, prev);
}
class Main {
public:
void run() {
while (true) {
int n, m, l, s, t;
cin >> n >> m >> l >> s >> t;
if (n == 0)
break;
s--;
Graph g(n);
REP(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back(Edge(a, b, c));
g[b].push_back(Edge(b, a, c));
}
js.clear();
REP(i, l) {
int v, e;
cin >> v >> e;
v--;
js[v] = e;
}
enc.clear();
EACH(it, js) enc[it->first] = 0;
enc[s] = 0;
N = 0;
EACH(it, enc) enc[it->first] = N++;
dec.clear();
EACH(it, enc) dec[it->second] = it->first;
ds = vector<vector<int>>(N, vector<int>(N));
EACH(it, dec) {
vector<int> d = dijkstra(g, it->second);
EACH(itj, dec) ds[it->first][itj->first] = d[itj->second];
}
dp = vector<vector<int>>(N, vector<int>(1 << N, INF<int>()));
dp[enc[s]][0] = 0;
REP(i, N) REP(bit, 1 << N) rec(i, bit);
int Mv = 0;
REP(i, N) REP(bit, 1 << N) if (dp[i][bit] + ds[i][enc[s]] <= t) {
int bc = 0;
REP(k, N) if (bit & (1 << k)) bc++;
Mv = max(Mv, bc);
}
cout << Mv << endl;
}
}
map<int, int> js;
map<int, int> enc;
map<int, int> dec;
vector<vector<int>> dp;
vector<vector<int>> ds;
int N;
int rec(int i, int bit) {
if (dp[i][bit] != INF<int>())
return dp[i][bit];
if (((bit >> i) & 1) && js.count(dec[i])) {
REP(j, N)
dp[i][bit] =
min(dp[i][bit], rec(j, bit - (1 << i)) + ds[j][i] + js[dec[i]]);
}
return dp[i][bit];
}
};
int main() {
cout << fixed << setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
Main().run();
return 0;
}
|
#include <algorithm> // other
#include <bitset>
#include <cassert> // c
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream> // io
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <vector> // container
using namespace std;
typedef long long ll;
#define ALL(c) (begin(c)), (end(c))
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORr(i, 0, n)
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define FORr(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); --i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
// debug
#define DUMP(x) cerr << #x << " = " << (x)
#define LINE() cerr << " (L" << __LINE__ << ")"
class range {
private:
struct Iter {
int v;
int operator*() { return v; }
bool operator!=(Iter &itr) { return v < itr.v; }
void operator++() { ++v; }
};
Iter i, n;
public:
range(int n) : i({0}), n({n}) {}
range(int i, int n) : i({i}), n({n}) {}
Iter &begin() { return i; }
Iter &end() { return n; }
};
// output
template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) {
REP(i, as.size()) {
if (i != 0)
os << " ";
os << as[i];
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &as) {
REP(i, as.size()) {
if (i != 0)
os << endl;
os << as[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &ss) {
for (auto a : ss) {
if (a != ss.begin())
os << " ";
os << a;
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
bool isF = true;
for (auto &p : m) {
if (!isF)
os << endl;
os << p;
isF = false;
}
return os;
}
template <typename T1> ostream &operator<<(ostream &os, const tuple<T1> &t) {
os << get<0>(t);
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const tuple<T1, T2> &t) {
os << get<0>(t) << " " << get<1>(t);
return os;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t) << " " << get<5>(t);
return os;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6, T7> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t)
<< " " << get<4>(t) << " " << get<5>(t) << " " << get<6>(t);
return os;
}
// input
char tmp[1000];
#define nextInt(n) scanf("%d", &n)
#define nextLong(n) scanf("%lld", &n) // I64d
#define nextDouble(n) scanf("%lf", &n)
#define nextChar(n) scanf("%c", &n)
#define nextString(n) \
scanf("%s", tmp); \
n = tmp
// values
template <typename T> T INF() { assert(false); };
template <> int INF<int>() { return 1 << 28; };
template <> ll INF<ll>() { return 1LL << 58; };
template <> double INF<double>() { return 1e16; };
template <class T> T EPS() { assert(false); };
template <> int EPS<int>() { return 1; };
template <> ll EPS<ll>() { return 1LL; };
template <> double EPS<double>() { return 1e-8; };
template <> long double EPS<long double>() { return 1e-8; };
template <typename T, typename U> T pmod(T v, U M) { return (v % M + M) % M; }
typedef int Cost;
Cost CINF = 1 << 28;
struct Edge {
int f, t;
Cost c;
Edge(int f, int t, Cost c) : f(f), t(t), c(c){};
bool operator<(Edge r) const { return c < r.c; }
bool operator>(Edge r) const { return c > r.c; }
};
typedef vector<vector<Edge>> Graph;
struct Task {
int prev, pos;
Cost c;
Task(int prev, int pos, Cost c) : prev(prev), pos(pos), c(c){};
bool operator>(const Task &r) const { return c > r.c; }
};
vector<Cost> dijkstra(const Graph &g, const int s, vector<int> &prev) {
const int V = g.size();
vector<Cost> d(V, CINF);
d[s] = 0;
fill(ALL(prev), -2);
priority_queue<Task, vector<Task>, greater<Task>> que;
que.push(Task(-1, s, 0)); // [ ,e,,f, ] <=> e.cost < e.cost
vector<bool> visited(V);
while (!que.empty()) {
Task task = que.top();
que.pop();
if (visited[task.pos])
continue;
visited[task.pos] = true;
prev[task.pos] = task.prev;
EACH(e, g[task.pos]) if (d[e->t] > d[e->f] + e->c) {
d[e->t] = d[e->f] + e->c;
que.push(Task(e->f, e->t, d[e->t]));
}
}
return d;
}
vector<Cost> dijkstra(const Graph &g, const int s) {
vector<int> prev(g.size());
return dijkstra(g, s, prev);
}
class Main {
public:
void run() {
while (true) {
int n, m, l, s, t;
cin >> n >> m >> l >> s >> t;
if (n == 0)
break;
s--;
Graph g(n);
REP(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back(Edge(a, b, c));
g[b].push_back(Edge(b, a, c));
}
js.clear();
REP(i, l) {
int v, e;
cin >> v >> e;
v--;
js[v] = e;
}
enc.clear();
EACH(it, js) enc[it->first] = 0;
enc[s] = 0;
N = 0;
EACH(it, enc) enc[it->first] = N++;
dec.clear();
EACH(it, enc) dec[it->second] = it->first;
ds = vector<vector<int>>(N, vector<int>(N));
EACH(it, dec) {
vector<int> d = dijkstra(g, it->second);
EACH(itj, dec) ds[it->first][itj->first] = d[itj->second];
}
dp = vector<vector<int>>(N, vector<int>(1 << N, INF<int>()));
dp[enc[s]][0] = 0;
REP(i, N) REP(bit, 1 << N) rec(i, bit);
int Mv = 0;
REP(i, N) REP(bit, 1 << N) if (dp[i][bit] + ds[i][enc[s]] <= t) {
int bc = 0;
REP(k, N) if (bit & (1 << k)) bc++;
Mv = max(Mv, bc);
}
cout << Mv << endl;
}
}
map<int, int> js;
map<int, int> enc;
map<int, int> dec;
vector<vector<int>> dp;
vector<vector<int>> ds;
int N;
int rec(int i, int bit) {
if (dp[i][bit] != INF<int>())
return dp[i][bit];
dp[i][bit] = INF<int>() - 1;
if (((bit >> i) & 1) && js.count(dec[i])) {
REP(j, N)
dp[i][bit] =
min(dp[i][bit], rec(j, bit - (1 << i)) + ds[j][i] + js[dec[i]]);
}
return dp[i][bit];
}
};
int main() {
cout << fixed << setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
Main().run();
return 0;
}
|
insert
| 266 | 266 | 266 | 267 |
TLE
| |
p01680
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define GET_BIT(n, i) (((n) & (1 << ((i)-1))) >> ((i)-1)) // i start from 1
#define SET_BIT(n, i) ((n) | (1 << ((i)-1)))
#define CLR_BIT(n, i) ((n) & ~(1 << ((i)-1)))
#define SHOW_A(x) \
{ cout << #x << " = " << x << endl; }
#define SHOW_B(x, y) \
{ cout << #x << " = " << x << ", " << #y << " = " << y << endl; }
#define SHOW_C(x, y, z) \
{ \
cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z \
<< " = " << z << endl; \
}
#define REACH_HERE \
{ cout << "REACH_HERE! line: " << __LINE__ << endl; }
const double E = 1e-8;
const double PI = acos(-1);
using namespace std;
const int MAX_N = 100005, MAX_M = 10005;
const int MOD_BASE = 1000000007;
int baba[MAX_N];
int power_modulo(int n, int p, int M) {
int result = 1;
while (p > 0) {
if (p % 2 == 1)
result = ((long long)result * n) % M;
p /= 2;
n = ((long long)n * n) % M;
}
return result;
}
int find_baba(int i) {
if (baba[i] != i) {
int new_baba = find_baba(baba[i]);
baba[i] = new_baba;
return new_baba;
}
return i;
}
void solve(int n, int m) {
for (int i = 1; i <= n; i++) {
baba[i] = i;
}
bool unstable = false;
int total = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
baba[find_baba(b)] = baba[a];
unstable = true;
}
int group = 0;
for (int i = 1; i <= n; i++) {
if (find_baba(i) == i) {
group++;
}
}
int ans = unstable ? 1 : 0;
ans += power_modulo(2, group, MOD_BASE);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
solve(n, m);
}
return 0;
}
|
#include <bits/stdc++.h>
#define GET_BIT(n, i) (((n) & (1 << ((i)-1))) >> ((i)-1)) // i start from 1
#define SET_BIT(n, i) ((n) | (1 << ((i)-1)))
#define CLR_BIT(n, i) ((n) & ~(1 << ((i)-1)))
#define SHOW_A(x) \
{ cout << #x << " = " << x << endl; }
#define SHOW_B(x, y) \
{ cout << #x << " = " << x << ", " << #y << " = " << y << endl; }
#define SHOW_C(x, y, z) \
{ \
cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z \
<< " = " << z << endl; \
}
#define REACH_HERE \
{ cout << "REACH_HERE! line: " << __LINE__ << endl; }
const double E = 1e-8;
const double PI = acos(-1);
using namespace std;
const int MAX_N = 100005, MAX_M = 10005;
const int MOD_BASE = 1000000007;
int baba[MAX_N];
int power_modulo(int n, int p, int M) {
int result = 1;
while (p > 0) {
if (p % 2 == 1)
result = ((long long)result * n) % M;
p /= 2;
n = ((long long)n * n) % M;
}
return result;
}
int find_baba(int i) {
if (baba[i] != i) {
int new_baba = find_baba(baba[i]);
baba[i] = new_baba;
return new_baba;
}
return i;
}
void solve(int n, int m) {
for (int i = 1; i <= n; i++) {
baba[i] = i;
}
bool unstable = false;
int total = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
baba[find_baba(b)] = find_baba(a);
unstable = true;
}
int group = 0;
for (int i = 1; i <= n; i++) {
if (find_baba(i) == i) {
group++;
}
}
int ans = unstable ? 1 : 0;
ans += power_modulo(2, group, MOD_BASE);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
solve(n, m);
}
return 0;
}
|
replace
| 56 | 57 | 56 | 57 |
0
| |
p01680
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define GET_BIT(n, i) (((n) & (1 << ((i)-1))) >> ((i)-1)) // i start from 1
#define SET_BIT(n, i) ((n) | (1 << ((i)-1)))
#define CLR_BIT(n, i) ((n) & ~(1 << ((i)-1)))
#define SHOW_A(x) \
{ cout << #x << " = " << x << endl; }
#define SHOW_B(x, y) \
{ cout << #x << " = " << x << ", " << #y << " = " << y << endl; }
#define SHOW_C(x, y, z) \
{ \
cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z \
<< " = " << z << endl; \
}
#define REACH_HERE \
{ cout << "REACH_HERE! line: " << __LINE__ << endl; }
const double E = 1e-8;
const double PI = acos(-1);
using namespace std;
const int MAX_N = 100005, MAX_M = 10005;
const int MOD_BASE = 1000000007;
int baba[MAX_N];
int power_modulo(int n, int p, int M) {
int result = 1;
while (p > 0) {
if (p % 2 == 1)
result = ((long long)result * n) % M;
p /= 2;
n = ((long long)n * n) % M;
}
return result;
}
int find_baba(int i) {
if (baba[i] != i) {
int new_baba = find_baba(baba[i]);
baba[i] = new_baba;
return new_baba;
}
return i;
}
void solve(int n, int m) {
for (int i = 1; i <= n; i++) {
baba[i] = i;
}
bool unstable = false;
int total = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
if (baba[b] != b) {
baba[baba[b]] = a;
} else {
baba[b] = a;
}
unstable = true;
}
int group = 0;
for (int i = 1; i <= n; i++) {
if (find_baba(i) == i) {
group++;
}
}
int ans = unstable ? 1 : 0;
ans += power_modulo(2, group, MOD_BASE);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
solve(n, m);
}
return 0;
}
|
#include <bits/stdc++.h>
#define GET_BIT(n, i) (((n) & (1 << ((i)-1))) >> ((i)-1)) // i start from 1
#define SET_BIT(n, i) ((n) | (1 << ((i)-1)))
#define CLR_BIT(n, i) ((n) & ~(1 << ((i)-1)))
#define SHOW_A(x) \
{ cout << #x << " = " << x << endl; }
#define SHOW_B(x, y) \
{ cout << #x << " = " << x << ", " << #y << " = " << y << endl; }
#define SHOW_C(x, y, z) \
{ \
cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z \
<< " = " << z << endl; \
}
#define REACH_HERE \
{ cout << "REACH_HERE! line: " << __LINE__ << endl; }
const double E = 1e-8;
const double PI = acos(-1);
using namespace std;
const int MAX_N = 100005, MAX_M = 10005;
const int MOD_BASE = 1000000007;
int baba[MAX_N];
int power_modulo(int n, int p, int M) {
int result = 1;
while (p > 0) {
if (p % 2 == 1)
result = ((long long)result * n) % M;
p /= 2;
n = ((long long)n * n) % M;
}
return result;
}
int find_baba(int i) {
if (baba[i] != i) {
int new_baba = find_baba(baba[i]);
baba[i] = new_baba;
return new_baba;
}
return i;
}
void solve(int n, int m) {
for (int i = 1; i <= n; i++) {
baba[i] = i;
}
bool unstable = false;
int total = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
baba[find_baba(b)] = find_baba(a);
unstable = true;
}
int group = 0;
for (int i = 1; i <= n; i++) {
if (find_baba(i) == i) {
group++;
}
}
int ans = unstable ? 1 : 0;
ans += power_modulo(2, group, MOD_BASE);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
solve(n, m);
}
return 0;
}
|
replace
| 56 | 61 | 56 | 57 |
0
| |
p01680
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;
struct UnionFindTree {
vector<int> v;
vector<int> rank;
int numgroup;
UnionFindTree(int n) : v(n, -1), rank(n, 0), numgroup(n) {}
int Find(int x) {
if (v[x] < 0)
return x;
return v[x] = Find(v[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a == b)
return;
if (rank[a] < rank[b])
swap(a, b);
if (rank[a] == rank[b])
rank[a]++;
v[b] = a;
numgroup--;
}
};
lli modpow(lli n, lli p, lli mod) {
lli res = 1;
for (int i = 63; i >= 0; i--) {
res = res * res % mod;
if ((p & 1LL << i) != 0)
res = res * n % mod;
}
return res;
}
int main() {
while (1) {
int n, m;
cin >> n >> m;
if (n == 0)
break;
UnionFindTree uft(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
uft.Union(a, b);
}
if (m == 0) {
cout << modpow(2, n, mod) << endl;
} else {
cout << (modpow(2, uft.numgroup, mod) + 1) % mod << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;
struct UnionFindTree {
vector<int> v;
vector<int> rank;
int numgroup;
UnionFindTree(int n) : v(n, -1), rank(n, 0), numgroup(n) {}
int Find(int x) {
if (v[x] < 0)
return x;
return v[x] = Find(v[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a == b)
return;
if (rank[a] < rank[b])
swap(a, b);
if (rank[a] == rank[b])
rank[a]++;
v[b] = a;
numgroup--;
}
};
lli modpow(lli n, lli p, lli mod) {
lli res = 1;
for (int i = 63; i >= 0; i--) {
res = res * res % mod;
if ((p & 1LL << i) != 0)
res = res * n % mod;
}
return res;
}
int main() {
while (1) {
int n, m;
cin >> n >> m;
if (n == 0)
break;
UnionFindTree uft(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
uft.Union(a, b);
}
if (m == 0) {
cout << modpow(2, n, mod) << endl;
} else {
cout << (modpow(2, uft.numgroup, mod) + 1) % mod << endl;
}
}
return 0;
}
|
insert
| 53 | 53 | 53 | 55 |
0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.