solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k;
cin >> n >> m >> k;
long long r, l, h;
r = k;
l = k;
h = 1;
m -= n;
while (m >= (r - l + 1) && !(l == 1 && r == n)) {
m -= (r - l + 1);
if (r < n) r++;
if (l > 1) l--;
h++;
}
if (l == 1 && r == n) {
h += m / n;
}
cout << h << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int i, j, k, n, m, x, y, z;
char a[1005][1005];
int H[2005][2005], l;
int A[1005][1005][3][9];
int yon[8][2] = {0, -1, -1, -1, -1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1};
bool cmp(pair<int, int> x, pair<int, int> y) {
int t1 = min(x.first, y.first);
x.first -= t1;
y.first -= t1;
int t2 = min(x.second, y.second);
x.second -= t2;
y.second -= t2;
if (!y.first and !y.second) return 0;
if (!x.first and !x.second) return 1;
if (x.first and y.second) return H[y.second][x.first];
return !H[x.second][y.first];
}
int main() {
cin >> n;
int p = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
scanf(" %c", &a[i][j]);
p |= a[i][j] - '0';
if (a[i][j] != '0') {
for (k = 0; k < 3; k++) {
A[i][j][0][k] +=
A[i + yon[k][0]][j + yon[k][1]][0][k] + (a[i][j] == '2');
A[i][j][1][k] +=
A[i + yon[k][0]][j + yon[k][1]][1][k] + (a[i][j] == '3');
A[i][j][2][k] +=
A[i + yon[k][0]][j + yon[k][1]][2][k] + (a[i][j] == '1');
}
}
}
if (!p) {
puts("0");
return 0;
}
for (i = 1; i <= n; i++)
for (j = n; j >= 1; j--) {
if (a[i][j] != '0') {
for (k = 3; k < 5; k++) {
A[i][j][0][k] +=
A[i + yon[k][0]][j + yon[k][1]][0][k] + (a[i][j] == '2');
A[i][j][1][k] +=
A[i + yon[k][0]][j + yon[k][1]][1][k] + (a[i][j] == '3');
A[i][j][2][k] +=
A[i + yon[k][0]][j + yon[k][1]][2][k] + (a[i][j] == '1');
}
}
}
for (i = n; i >= 1; i--)
for (j = n; j >= 1; j--) {
if (a[i][j] != '0') {
for (k = 5; k < 6; k++) {
A[i][j][0][k] +=
A[i + yon[k][0]][j + yon[k][1]][0][k] + (a[i][j] == '2');
A[i][j][1][k] +=
A[i + yon[k][0]][j + yon[k][1]][1][k] + (a[i][j] == '3');
A[i][j][2][k] +=
A[i + yon[k][0]][j + yon[k][1]][2][k] + (a[i][j] == '1');
}
}
}
for (i = n; i >= 1; i--)
for (j = 1; j <= n; j++) {
if (a[i][j] != '0') {
for (k = 6; k < 8; k++) {
A[i][j][2][k] +=
A[i + yon[k][0]][j + yon[k][1]][2][k] + (a[i][j] == '1');
A[i][j][0][k] +=
A[i + yon[k][0]][j + yon[k][1]][0][k] + (a[i][j] == '2');
A[i][j][1][k] +=
A[i + yon[k][0]][j + yon[k][1]][1][k] + (a[i][j] == '3');
}
}
}
pair<int, int> t;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
if (a[i][j] == '0') continue;
int mn = 1000000007;
t = make_pair(0, 0);
t.first -= (a[i][j] == '2') * 3;
t.second -= (a[i][j] == '3') * 3;
for (k = 0; k < 8; k += 2)
mn = min(mn, A[i][j][0][k] + A[i][j][1][k] + A[i][j][2][k]);
for (k = 0; k < 8; k += 2) {
t.first +=
A[i][j][0][k] - A[i + mn * yon[k][0]][j + mn * yon[k][1]][0][k];
t.second +=
A[i][j][1][k] - A[i + mn * yon[k][0]][j + mn * yon[k][1]][1][k];
}
v.push_back(t);
t = make_pair(0, 0);
t.first -= (a[i][j] == '2') * 3;
t.second -= (a[i][j] == '3') * 3;
mn = 1000000007;
for (k = 1; k < 8; k += 2)
mn = min(mn, A[i][j][0][k] + A[i][j][1][k] + A[i][j][2][k]);
for (k = 1; k < 8; k += 2) {
t.first +=
A[i][j][0][k] - A[i + mn * yon[k][0]][j + mn * yon[k][1]][0][k];
t.second +=
A[i][j][1][k] - A[i + mn * yon[k][0]][j + mn * yon[k][1]][1][k];
}
v.push_back(t);
}
double l = 0;
long long ans = 1;
for (i = 1; i <= 2000; i++) {
l += log2(3);
for (j = 0; j <= min((int)l, 2000); j++) H[i][j] = 1;
}
sort(v.begin(), v.end(), cmp);
t = v.back();
for (i = 0; i < t.first; i++) ans = ans * 2 % 1000000007;
for (i = 0; i < t.second; i++) ans = ans * 3 % 1000000007;
cout << ans << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define pb emplace_back
#define ll long long
#define pii pair<int, int>
#define ld long double
const int INF = 2e9 + 1;
const int mod = 1e9 + 7;
vector<int> z_function(string &s) {
int n = (int)s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s, t;
cin >> s >> t;
while ((int)s.size() < 2 * (int)t.size()) s += s;
s += s;
string t1 = t;
while ((int)t1.size() < (int)s.size()) t1 += t;
string lol = t1 + "#" + s;
int st1 = t1.size() + 1, end1 = t1.size() + 1 + (int)s.size() / 2;
vector<int> z = z_function(lol);
s += s;
while ((int)t1.size() < (int)s.size()) t1 += t;
string kek = t1 + "#" + s;
int st2 = t1.size() + 1;
vector<int> newz = z_function(kek);
int ans = 0;
int j = st2;
for (int i = st1; i < end1; i++) {
int cnt1 = z[i] / (int)t.size();
int cnt2 = newz[j] / (int)t.size();
if (cnt2 > cnt1) {
cout << -1;
return 0;
}
ans = max(ans, cnt1);
j++;
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long mn = 9999999999;
long long mx2 = 0;
while (n--) {
long long x, y;
cin >> x >> y;
mn = min(y, mn);
mx2 = max(mx2, x);
}
long long m;
cin >> m;
long long mx = 0;
long long mn2 = 9999999999;
while (m--) {
long long x, y;
cin >> x >> y;
mx = max(mx, x);
mn2 = min(mn2, y);
}
long long ans1 = 0, ans2 = 0;
if (mx - mn > 0) ans1 = mx - mn;
if (mx2 - mn2 > 0) ans2 = mx2 - mn2;
cout << max(ans1, ans2) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
set<int> S;
map<int, int> M;
struct node {
int x, y;
} A[222222];
bool operator<(const node x, const node y) {
if (x.y != y.y) {
return x.y > y.y;
} else {
return x.x < y.x;
}
}
int T[222222];
void add(int x) {
while (x <= 200000) {
T[x]++;
x += ((x) & (-(x)));
}
}
int query(int x) {
int res = 0;
while (x) {
res += T[x];
x -= ((x) & (-(x)));
}
return res;
}
int N;
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d%d", &A[i].x, &A[i].y);
M[A[i].x] = 0;
}
int cnt = 0;
for (map<int, int>::iterator i = M.begin(); i != M.end(); i++) {
(i->second) = ++cnt;
}
sort(A + 1, A + N + 1);
A[0].x = -1;
A[0].y = -1;
A[N + 1].x = -1;
A[N + 1].y = -1;
long long ANS = 0;
for (int i = 1; i <= N; i++) {
int l = M[A[i].x];
if (S.find(A[i].x) == S.end()) {
S.insert(A[i].x);
add(l);
}
if (A[i + 1].y != A[i].y) {
ANS += (long long)(query(l)) * (query(200000) - query(l) + 1);
} else {
ANS += (long long)(query(l)) * (query(M[A[i + 1].x] - 1) - query(l) + 1);
}
}
printf("%lld", ANS);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int dr[4] = {0, 1, 0, -1};
const int dc[4] = {-1, 0, 1, 0};
int R, C;
char B[(1505)][(1505)];
int r0, c0;
int vr[(1505)][(1505)];
int vc[(1505)][(1505)];
queue<pair<int, int> > Q;
inline int modn(int a, int n) {
if (a < 0) {
a = (-a) % n;
a = (n - a) % n;
return a;
} else
return a % n;
}
int main() {
int i, j;
scanf("%d %d", &R, &C);
for (i = 0; i < R; ++i) {
scanf("%s", B[i]);
for (j = 0; j < C; ++j) {
if (B[i][j] == 'S') {
r0 = i;
c0 = j;
}
vr[i][j] = (10000000);
vc[i][j] = (10000000);
}
}
Q.push(pair<int, int>(r0, c0));
vr[r0][c0] = r0;
vc[r0][c0] = c0;
while (!Q.empty()) {
pair<int, int> p = Q.front();
Q.pop();
int r = p.first;
int c = p.second;
for (i = 0; i < 4; ++i) {
int rr = r + dr[i];
int cc = c + dc[i];
int rm = modn(rr, R);
int cm = modn(cc, C);
if (B[rm][cm] != '#') {
if (vr[rm][cm] == (10000000)) {
Q.push(pair<int, int>(rr, cc));
vr[rm][cm] = rr;
vc[rm][cm] = cc;
} else if (vr[rm][cm] != rr || vc[rm][cm] != cc) {
printf("Yes\n");
return 0;
}
}
}
}
printf("No\n");
return 0;
}
| 4 |
#include<stdio.h>
int main()
{
long long n, K, A, B;
scanf("%lld %lld %lld", &K, &A, &B);
if(K<=A)
n = 1;
else if(A<=B)
n = -1;
else
n = (K-B)/(A-B);
if(n>0)
{
while(n*A-(n-1)*B<K)
n++;
printf("%lld\n", n*2-1);
}
else
printf("-1\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N,M;
const ll mo=998244353;
const int NUM_=4400001;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
ll comb(ll N_, ll C_) {
if (fact[0]==0) {
inv[1]=fact[0]=factr[0]=1;
for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
}
if(C_<0 || C_>N_) return 0;
return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}
ll hcomb(int P_,int Q_) { return (P_==0&&Q_==0)?1:comb(P_+Q_-1,Q_);}
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N>>M;
ll ret=0;
for(i=0;i<=min(N,M);i++) {
int lef=3*M-i;
if(lef%2) continue;
lef=lef/2;
ret+=comb(N,i)*hcomb(N,lef)%mo;
}
for(i=2*M+1;i<=3*M;i++) ret-=N*hcomb(N-1,3*M-i)%mo;
cout<<(ret%mo+mo)%mo<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116, EPS = 1e-9,
Gold = ((1 + sqrt(5)) / 2);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
long long keyCount = sizeof(keymod) / sizeof(long long);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcountll(s);
}
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
string cppstr_infile = "FILE.IN";
string cppstr_outfile = "FILE.OUT";
int n, p;
string s;
vector<vector<int> > A;
vector<int> val;
vector<vector<int> > PrefixSum;
vector<vector<int> > charList;
bool vis[17][17][131072], allowed[131072];
int RangeSum(int id, int L, int R) {
int sum = PrefixSum[id][R];
if (L > 0) sum -= PrefixSum[id][L - 1];
return sum;
}
int DFS(int z, vector<bool>& checked) {
int res = val[z];
checked[z] = true;
for (int i = 0; i < p; i++) {
if (getbit(z, i)) continue;
int newmask = z | (1 << i);
if (!allowed[newmask]) continue;
if (checked[newmask]) continue;
res = min(res, DFS(newmask, checked));
}
return res;
}
void Spray(int z, int x, int y) {
vis[x][y][z] = true;
vis[y][x][z] = true;
allowed[z] = false;
for (int i = 0; i < p; i++) {
if (i == x || i == y) continue;
if (getbit(z, i)) continue;
int newmask = z | (1 << i);
if (vis[x][y][newmask]) continue;
Spray(newmask, x, y);
}
}
void Input() {
for (int i = 0; i < 131072; i++) allowed[i] = true;
cin >> n >> p >> s;
A.resize(p, vector<int>(p));
val.resize(1 << p, n);
PrefixSum.resize(p, vector<int>(n, 0));
charList.resize(p);
for (int i = 0; i < p; i++) {
for (int j = 0; j < p; j++) {
cin >> A[i][j];
}
}
for (int i = 0; i < n; i++) {
int id = s[i] - 'a';
charList[id].push_back(i);
PrefixSum[id][i]++;
}
for (int i = 0; i < p; i++) {
for (int j = 1; j < n; j++) {
PrefixSum[i][j] += PrefixSum[i][j - 1];
}
}
}
void Solve() {
for (int mask = 0; mask < (1 << p); mask++) {
for (int i = 0; i < p; i++) {
if (!getbit(mask, i)) continue;
val[mask] -= charList[i].size();
}
}
for (int i = 0; i < p; i++) {
for (int j = 0; j < p; j++) {
if (A[i][j]) continue;
int ptr1 = 0, ptr2 = 0;
while (ptr1 < charList[i].size()) {
while (ptr2 < charList[j].size() &&
charList[i][ptr1] >= charList[j][ptr2])
ptr2++;
if (ptr2 == charList[j].size()) break;
while (ptr1 + 1 < charList[i].size() &&
charList[i][ptr1 + 1] < charList[j][ptr2])
ptr1++;
if (ptr1 == charList[i].size()) break;
int L = charList[i][ptr1], R = charList[j][ptr2];
if (L + 1 >= R) {
ptr1++;
continue;
}
int mask = 0;
for (int x = 0; x < p; x++) {
if (RangeSum(x, L + 1, R - 1) == 0) continue;
mask |= (1 << x);
}
allowed[mask] = false;
ptr1++;
if (!vis[i][j][mask]) Spray(mask, i, j);
}
}
}
vector<bool> checked(1 << p, false);
cout << DFS(0, checked) << '\n';
}
int main(int argc, char* argv[]) {
ControlIO(argc, argv);
Input();
TimerStart();
Solve();
TimerStop();
return 0;
}
void ControlIO(int argc, char* argv[]) {
char* infile = new char[cppstr_infile.size() + 1];
char* outfile = new char[cppstr_outfile.size() + 1];
strcpy(infile, cppstr_infile.c_str());
strcpy(outfile, cppstr_outfile.c_str());
}
void TimerStart() {}
void TimerStop() {}
void Exit() {
TimerStop();
exit(0);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd1(long long int x, long long int y) {
if (y == 0) {
return x;
}
return gcd1(y, x % y);
}
long long int mult(long long int a, long long int b) {
return (1LL * a * b) % int(1e9 + 7);
}
long long int msum(long long int a, long long int b) {
long long int s = a + b;
if (s >= int(1e9 + 7)) s -= int(1e9 + 7);
return s;
}
struct pos {
long long int n, w, e, s;
};
int main() {
long long int n, m;
cin >> n >> m;
vector<string> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
long long int k;
cin >> k;
vector<char> dir(k);
vector<long long int> l(k);
for (int i = 0; i < k; i++) {
cin >> dir[i] >> l[i];
}
vector<vector<pos> > x(n, vector<pos>(m));
vector<pair<long long int, long long int> > z;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] != '.' && v[i][j] != '#') {
z.push_back(make_pair(i, j));
}
if (j == 0)
x[i][j].w = 0;
else {
if (v[i][j - 1] != '#') {
x[i][j].w = x[i][j - 1].w + 1;
} else {
x[i][j].w = 0;
}
}
}
}
for (int i = 0; i < n; i++) {
for (long long int j = m - 1; j >= 0; j--) {
if (j == m - 1)
x[i][j].e = 0;
else {
if (v[i][j + 1] != '#') {
x[i][j].e = x[i][j + 1].e + 1;
} else {
x[i][j].e = 0;
}
}
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (i == 0)
x[i][j].n = 0;
else {
if (v[i - 1][j] != '#') {
x[i][j].n = x[i - 1][j].n + 1;
} else {
x[i][j].n = 0;
}
}
}
}
for (int j = 0; j < m; j++) {
for (long long int i = n - 1; i >= 0; i--) {
if (i == n - 1)
x[i][j].s = 0;
else {
if (v[i + 1][j] != '#') {
x[i][j].s = x[i + 1][j].s + 1;
} else {
x[i][j].s = 0;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
}
}
vector<char> ans;
for (int i = 0; i < int(z.size()); i++) {
long long int xc = z[i].first;
long long int yc = z[i].second;
long long int flag = 1;
for (int j = 0; j < k; j++) {
if (dir[j] == 'N') {
if (x[xc][yc].n >= l[j]) {
xc -= l[j];
} else {
flag = 0;
break;
}
}
if (dir[j] == 'S') {
if (x[xc][yc].s >= l[j]) {
xc += l[j];
} else {
flag = 0;
break;
}
}
if (dir[j] == 'E') {
if (x[xc][yc].e >= l[j]) {
yc += l[j];
} else {
flag = 0;
break;
}
}
if (dir[j] == 'W') {
if (x[xc][yc].w >= l[j]) {
yc -= l[j];
} else {
flag = 0;
break;
}
}
}
if (flag) {
ans.push_back(v[z[i].first][z[i].second]);
}
}
sort((ans).begin(), (ans).end());
if (int(ans.size()) == 0) {
cout << "no solution" << endl;
}
for (int i = 0; i < int(ans.size()); i++) {
cout << ans[i];
}
cout << endl;
}
| 4 |
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
while(scanf("%d", &n), n){
unsigned s = 0, p;
vector<unsigned> j(n - 1);
for(int i = 0; i < n; ++i){
scanf("%u", &p);
s += p;
}
for(int i = 0; i < n - 1; ++i){
scanf("%u", &j[i]);
}
sort(j.begin(), j.end());
unsigned ans = s * n;
for(int i = n - 2; i >= 0; --i){
s += j[i];
ans = max(ans, s * (i + 1));
}
printf("%u\n", ans);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int fa[500005];
struct node {
int opt, u, v, w, uu, vv;
bool operator<(const node &a) const { return w < a.w; }
} E[500005];
int n, m, Q;
int find(int x) {
while (x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
pair<int, int> stck[500005];
int top;
void merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
fa[x] = y;
}
bool cmp(node x, node y) { return x.opt < y.opt; }
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
E[i] = (node){i, u, v, w, u, v};
}
for (int i = 1; i <= n; i++) fa[i] = i;
sort(E + 1, E + m + 1);
for (int i = 1; i <= m;) {
int j = i;
while (E[j].w == E[i].w) {
E[j].uu = find(E[j].u), E[j].vv = find(E[j].v);
j++;
}
while (i < j) {
merge(E[i].u, E[i].v);
i++;
}
}
sort(E + 1, E + m + 1, cmp);
for (int i = 1; i <= n; i++) fa[i] = i;
cin >> Q;
while (Q--) {
int k;
scanf("%d", &k);
vector<node> V;
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
V.push_back(E[x]);
}
sort(V.begin(), V.end());
int j = 0;
bool flag = 0;
for (int i = 0; i < V.size();) {
if (V[i].uu == V[i].vv) {
flag = 1;
break;
}
merge(V[i].uu, V[i].vv);
j = i + 1;
while (j < V.size() && V[j].w == V[i].w) {
int u = find(V[j].uu), v = find(V[j].vv);
if (u == v) {
flag = 1;
break;
}
fa[u] = v;
j++;
}
while (i < j) fa[V[i].uu] = V[i].uu, fa[V[i].vv] = V[i].vv, i++;
if (flag) break;
}
if (!flag)
puts("YES");
else
puts("NO");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long long int x, y, z;
cin >> x >> y >> z;
if (x + y < z)
cout << "0 0";
else
cout << (x + y) / z << " "
<< ((x + y) / z > x / z + y / z
? min(min(x % z, y % z), min(z - x % z, z - y % z))
: 0);
return 0;
}
| 1 |
//begin #include <Core>
/*
* Package: StandardCodeLibrary.Core
* Last Update: 2012-12-21
* */
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <utility>
#include <vector>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#if __GNUC__>=4 and __GNUC_MINOR__>=6
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#endif
using namespace std;
#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repf(i,0,n)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=b;--i)
#define feach(e,s) for (typeof((s).begin()) e=(s).begin();e!=(s).end();++e)
#define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set)
#define forin(i,charset) for (cstr i=charset;*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,char(0))
#define cpy(x,y) memcpy(x,y,sizeof(x))
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define x first
#define y second
#define sz(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x),(x).erase(unique(all(x)),x.end())
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define sf scanf
#define pf printf
#define pdb(prcs,x) (cout<<setprecision(prcs)<<fixed<<(x))
#ifdef DEBUG
#define prt(x) cerr<<#x"="<<(x)<<endl
#define asrtWA(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0)
#define asrtTLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0)
#define asrtMLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0)
#define asrtOLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0)
#define asrtRE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0)
#define runtime() cerr<<"Used: "<<db(clock())/CLOCKS_PER_SEC<<"s"<<endl
#define input(in) do{}whl(0)
#define output(out) do{}whl(0)
#else
#define endl (char('\n'))
#define prt(x) (cerr)
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() cerr
#define input(in) freopen(in,"r",stdin)
#define output(out) freopen(out,"w",stdout)
#endif
typedef long long int lli;
typedef double db;
typedef const char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef pr<str,int> psi;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;
typedef prq<int> pqi;
#if __GNUC__>=4 and __GNUC_MINOR__>=7
template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};
template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};
#elif __GNUC__>=4 and __GNUC_MINOR__>=6
template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};
template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_mapped_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};
#endif
const int oo=(~0u)>>1;
const lli ooll=(~0ull)>>1;
const db inf=1e+10;
const db eps=1e-10;
const db pi=acos(-1.0);
const int MOD=1000000007;
template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
inline int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);}
inline int sgn(const db& x){rtn dbcmp(x,0);}
template<typename ostream,typename type>ostream& operator<<(ostream& cout,const pr<type,type>& x){rtn cout<<"("<<x.x<<","<<x.y<<")";}
template<typename type>pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);}
template<typename type>pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);}
template<typename type>pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);}
template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b,const pr<type,type>& c){rtn (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b,const pr<type,type>& c){rtn (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& st){rep(i,sz(st))st[i]=i;}
inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;}
inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;}
template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.insert(*b.begin()),b.erase(b.begin());}
template<typename type>inline void merge(prq<type>& a,prq<type>& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.push(b.top()),b.pop();}
struct Initializer{Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}~Initializer(){runtime();}}initializer;
//end #include <Core>
#define idx(l,r) (((l)+(r))|((l)!=(r)))
#define rt idx(l,r)
#define lrt idx(l,m)
#define rrt idx(m+1,r)
const int MAXN=200000;
typedef int node;
int x[MAXN];
si ys[MAXN];
mii xidx;
node st[(MAXN<<1)-1];
void upd(int l,int r,int p,int v)
{
if (p<l||r<p) ;
else if (p<=l&&r<=p)
{
if (v>0) ys[p].ins(v);
else ys[p].ers(-v);
if (sz(ys[p])) st[rt]=*ys[p].rbegin();
else st[rt]=-oo;
}
else
{
int m=(l+r)>>1;
upd(l,m,p,v),upd(m+1,r,p,v);
st[rt]=max(st[lrt],st[rrt]);
}
}
pii qry(int l,int r,int p,int v)
{
if (r<=p||st[rt]<=v) rtn mp(-oo,-oo);
else if (l==r) rtn mp(x[l],*ys[l].ub(v));
else
{
int m=(l+r)>>1;
pii ret=qry(l,m,p,v);
if (ret!=mp(-oo,-oo)) rtn ret;
else rtn qry(m+1,r,p,v);
}
}
#undef rc
#undef lc
#undef p
#undef idx
char op[MAXN][7];
pii p[MAXN];
int main()
{
int n;
sf("%d",&n);
rep(i,n) sf("%s%d%d",op+i,&p[i].x,&p[i].y);
rep(i,n) x[i]=p[i].x;
sort(x,x+n);
rep(i,n) if (!xidx.count(x[i])) xidx.insert(mp(x[sz(xidx)]=x[i],sz(xidx)));
rep(i,n)
{
if (strcmp(op[i],"add")==0)
upd(0,sz(xidx)-1,xidx[p[i].x],p[i].y);
else if (strcmp(op[i],"remove")==0)
upd(0,sz(xidx)-1,xidx[p[i].x],-p[i].y);
else
{
pii ans=qry(0,sz(xidx)-1,xidx[p[i].x],p[i].y);
if (ans!=mp(-oo,-oo)) pf("%d %d\n",ans.x,ans.y);
else puts("-1");
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, p, k, y, r, b[1000], c[10000], ok, of;
char a[100000];
int main() {
cin >> n;
for (int i = 0; i < 2 * n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
b[i] = (int)a[i] - '0';
}
for (int i = n; i < 2 * n; i++) {
c[k] = a[i] - '0';
k++;
}
sort(b, b + n);
sort(c, c + k);
for (int i = 0; i < n; i++) {
if (b[i] < c[i]) {
of++;
}
if (b[i] > c[i]) {
ok++;
}
}
if (ok == n || of == n) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 2 |
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()
const int MAX = 10010;
bool p[MAX];
int main()
{
p[0] = p[1] = 1;
rep(i,MAX)if(!p[i])for(int j= i*2;j<MAX;j+=i)p[j] = 1;
int n;
while(cin >> n && n)
{
for(int i = n;i!=0;i--)if(!p[i] && !p[i-2]){ cout << i-2 << " " << i << endl;break;}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
long long MOD = 998244353;
const char nl = '\n';
const long long inf = 1e15;
long long power(long long x, long long y) {
long long z = 1;
while (y > 0) {
if (y % 2) z = z * x;
x = x * x;
y /= 2;
}
return z;
}
long long gcd(long long a, long long b) {
if (a < b) return gcd(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}
long long sq(long long a) {
long long ans = (1ll * a * a);
return ans;
}
bool isprime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long digitsum(long long x) {
long long ans = 0;
while (x > 0) {
ans += (x % 10);
x /= 10;
}
return ans;
}
set<int> Divisors(int n) {
set<int> s;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
s.insert(i);
} else {
s.insert(i);
s.insert(n / i);
}
}
}
return s;
}
int n;
int a[100001];
bool solve(int k) {
if (n / k < 3) return false;
for (int i = 0; i < k; ++i) {
bool ok = true;
for (int j = i; j < n; j += k) {
ok &= a[j];
}
if (ok) return true;
}
return false;
}
void solve() {
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0 && (solve(i) || solve(n / i))) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long TC = 1;
while (TC--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
long long a[N];
struct t {
int lval, rval, mval;
} tr[N * 4];
int sign(long long x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
inline void cal(int cur, int l, int r) {
int m = (l + r) / 2;
int dcur = cur + cur;
tr[cur].mval = max(tr[dcur].mval, tr[dcur + 1].mval);
if (!a[m] || !a[m + 1] || sign(a[m]) < sign(a[m + 1])) {
tr[cur].lval = tr[dcur].lval;
tr[cur].rval = tr[dcur + 1].rval;
} else {
tr[cur].mval = max(tr[cur].mval, tr[dcur].rval + tr[dcur + 1].lval);
if (tr[dcur].mval == m - l + 1) {
tr[cur].lval = tr[dcur].lval + tr[dcur + 1].lval;
} else {
tr[cur].lval = tr[dcur].lval;
}
if (tr[dcur + 1].mval == r - m) {
tr[cur].rval = tr[dcur + 1].rval + tr[dcur].rval;
} else {
tr[cur].rval = tr[dcur + 1].rval;
}
}
}
void build(int pos, int l, int r) {
if (l == r) {
int x = !!a[l];
tr[pos].lval = tr[pos].rval = tr[pos].mval = x;
return;
}
int mid = (l + r) >> 1;
build(pos * 2, l, mid);
build(pos * 2 + 1, mid + 1, r);
cal(pos, l, r);
}
void update(int pos, int l, int r, int k, int d) {
int mid = (l + r) >> 1;
if (l == r) {
a[l] += d;
int x = !!a[l];
tr[pos].lval = tr[pos].rval = tr[pos].mval = x;
return;
} else {
int mid = (l + r) >> 1;
if (k <= mid)
update(pos * 2, l, mid, k, d);
else
update(pos * 2 + 1, mid + 1, r, k, d);
cal(pos, l, r);
}
}
int main() {
int u, v, n, i, m, l, r, d;
cin >> n;
cin >> u;
for (i = 1; i < n; ++i) scanf("%d", &v), a[i - 1] = v - u, u = v;
if (n > 1) build(1, 0, n - 2);
cin >> m;
while (m--) {
scanf("%d%d%d", &l, &r, &u);
if (n == 1) {
printf("1\n");
} else {
if (l > 1) update(1, 0, n - 2, l - 2, u);
if (r < n) update(1, 0, n - 2, r - 1, -u);
printf("%d\n", tr[1].mval + 1);
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long oo = 100000000007;
vector<long long> v;
map<long long, long long> m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long i, a, b, n, k, s;
string s1, s2, s3;
cin >> s1;
cin >> s2;
n = s1.size();
for (i = 0; i < n; i++) {
if (s1[i] < s2[i]) {
cout << -1;
return 0;
}
}
cout << s2;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long double n, l, v1, v2, k;
cin >> n;
cin >> l;
cin >> v1;
cin >> v2;
cin >> k;
int rounds = ceil(n / k);
long double d = l / (((2 * v1) / (v1 + v2)) * (rounds - 1) + 1);
long double time = (((l - d) / v1) + d / v2);
cout << setprecision(30) << time;
}
| 1 |
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int markpower[128];
int numpower[128];
int main()
{
for (char c = '0'; c <= '9'; c++) {
numpower[c] = c - '0';
}
numpower['T'] = 10;
numpower['J'] = 11;
numpower['Q'] = 12;
numpower['K'] = 13;
numpower['A'] = 14;
char trump;
while (cin >> trump, trump != '#') {
string input[4][13];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
cin >> input[i][j];
}
}
int win = 3;
int ns = 0, ew = 0;
for (int j = 0; j < 13; j++) {
priority_queue<pair<int, int>> que;
for (int i = 0; i < 4; i++) {
fill_n((int *)markpower, 128, 0);
markpower[input[win][j][1]] = 100;
markpower[trump] = 200;
int power = markpower[input[i][j][1]]+numpower[input[i][j][0]];
que.push({power, i});
}
win = que.top().second;
if (win % 2 == 0) ns++;
else ew++;
}
if (ns > ew)
cout << "NS " << ns % 6 << endl;
else
cout << "EW " << ew % 6 << endl;
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
#define N 333
#define mod 1000000007
int n,m,f[N][N][N],ans;
vector<pair<int,int> > q[N];
void pls(int &x,int y){x=(x+y)%mod;}
int read(){
int x=0,f=1;char ch=getchar();
for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-f;
for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x*f;
}
int main(){
n=read();m=read();
for (int i=1;i<=m;i++){
int x=read(),y=read(),z=read();
q[y].push_back(make_pair(x,z));
}
f[1][0][0]=3;
for (int i=0;i<=n;i++)
for (int j=0;j<=n;j++)
for (int k=0;k<=n;k++){
for (int l=0;l<(int)q[i].size();l++)
if (1+(j>=q[i][l].first)+(k>=q[i][l].first)!=q[i][l].second) f[i][j][k]=0;
if (i==n) pls(ans,f[i][j][k]);
else pls(f[i+1][j][k],f[i][j][k]),
pls(f[i+1][i][k],f[i][j][k]),
pls(f[i+1][i][j],f[i][j][k]);
}
printf("%d\n",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, x[1005], y[1005], v[1005];
int solve() {
cin >> a >> b >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> v[i];
double ans = 2e18;
for (int i = 0; i < n; i++)
ans = min(sqrt((x[i] - a) * (x[i] - a) + (y[i] - b) * (y[i] - b)) / v[i],
ans);
cout << setprecision(8) << ans << endl;
return 0;
}
int main() {
ios::sync_with_stdio(0);
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, a[1005];
int main() {
std::ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
m = n - 1 - a[0] + 1;
for (int i = 1; i < n; i++) {
if (i % 2 == 1)
a[i] = (a[i] - m + n) % n;
else
a[i] = (a[i] + m) % n;
if (a[i] != i) {
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;
}
| 2 |
#include<cstdio>
int solve(int a,int b){
if(b==4){
if(a==0) return 1;
else return 0;
}
else{
int res=0;
for(int i=0;i<=a&&i<10;i++){
res+=solve(a-i,b+1);
}
return res;
}
}
int main(){
int n;
while(~scanf("%d",&n)){
printf("%d\n",solve(n,0));
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t = 1;
while (t--) {
long long n, m;
cin >> n >> m;
long long a[n], b[m];
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < m; i++) cin >> b[i];
vector<long long> v;
for (long long i = 0; i < n; i++) {
long long x = LLONG_MIN;
for (long long j = 0; j < m; j++) x = max(x, a[i] * b[j]);
v.push_back(x);
}
sort(v.begin(), v.end());
cout << v[v.size() - 2] << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int a[100000] = {0};
int main() {
int z;
for (z = 0; z < 1000000; z++)
;
for (int n, m, k; cin >> n >> m >> k;) {
int i;
for (i = 0; i < n && cin >> a[i]; i++)
;
long long int t = m / ((n + 1) / 2);
if (n % 2 == 0) {
cout << 0 << endl;
continue;
}
if (t == 0) {
cout << 0 << endl;
continue;
}
long long int tt = a[0];
for (i = 2; i < n; i += 2) {
if (a[i] < tt) tt = a[i];
}
cout << (k * t > tt ? tt : k * t) << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265359;
const long long MOD = (long long)998244353ll;
const long long MAXN = (long long)1e6 + 10;
const long long INF = (long long)2242545357980376863;
const long double EPS = (long double)1e-8;
vector<pair<long long, long long> > G[MAXN];
long long a[MAXN], b[MAXN];
long long MN = 3e18;
long long mul(long long a, long long b) {
if (a > MN / b + 5) return MN;
return a * b;
}
void DFS(long long u) {
long long adj, w;
for (auto E : G[u]) {
adj = E.first;
w = E.second;
DFS(adj);
if (a[adj] < b[adj]) {
long long x = b[adj] - a[adj];
b[adj] -= x;
b[u] += x;
} else if (a[adj] > b[adj]) {
long long x = a[adj] - b[adj];
b[adj] += x;
b[u] -= mul(x, w);
b[u] = max(b[u], -MN);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) cin >> a[i];
long long u, w;
for (int i = 2; i <= n; i++) {
cin >> u >> w;
G[u].push_back({i, w});
}
DFS(1);
if (b[1] < a[1])
cout << "NO";
else
cout << "YES";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N];
int f[N][2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) {
f[i][0] = 1;
if (a[i] > a[i - 1]) {
f[i][0] = max(f[i][0], f[i - 1][0] + 1);
f[i][1] = max(f[i][1], f[i - 1][1] + 1);
}
if (i - 2 >= 1 && a[i] > a[i - 2]) f[i][1] = max(f[i][1], f[i - 2][0] + 1);
}
int res = 0;
for (int i = 1; i <= n; ++i) {
res = max(res, max(f[i][0], f[i][1]));
}
printf("%d\n", res);
return 0;
}
| 4 |
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a;
cout<<24-a+24<<endl;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long inf = 1e18;
const long double pi = 3.141592653589793238;
long long MOD = 1000000007;
const long long N = 3e5 + 10;
long long n, m;
long long score[N];
vector<long long> graph[N];
bool vis[N] = {0};
long long dp[N];
long long Free = 0, best = 0;
bool dfs(long long u, long long p) {
vis[u] = 1;
dp[u] = score[u];
bool check = 1;
for (auto v : graph[u]) {
if (v == p) continue;
if (vis[v])
check = 0;
else {
check &= dfs(v, u);
dp[u] = max(dp[u], score[u] + dp[v]);
}
}
if (!check) Free += score[u];
if (check) best = max(best, dp[u]);
return check;
}
void solve() {
cin >> n >> m;
for (long long i = 1; i <= n; i++) cin >> score[i];
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
long long src;
cin >> src;
dfs(src, src);
cout << Free + best << '\n';
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
}
| 5 |
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
int n,m;
while(scanf("%d %d", &n, &m) && n != 0 ){
vector<int > a;
int temp;
int ans = 0;
for(int i = 0; i < n; i++){
scanf("%d", &temp);
a.push_back(temp);
}
sort(a.begin(),a.end(),greater<int>());
for(int i = 0; i < n; i++){
if( ( i+1)%m != 0 ) ans += a[i];
}
printf("%d\n",ans);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
const ll nax = 1e5;
vector<ll> v[nax];
const ll mod = 1e9 + 7;
// (black, white)
pair<ll, ll> dfs(ll s, ll p){
ll white = 1;
ll black = 0;
for(ll &c : v[s]){
if(c == p) continue;
pair<ll, ll> pf = dfs(c, s);
black = ((1LL*(white+black)*pf.first)%mod + (1LL*black*pf.second)%mod)%mod;
white = (white*pf.second)%mod;
}
return make_pair(white, (white + black)%mod);
}
int main(){
ll n; cin >> n;
for (ll i = 1; i < n; ++i)
{
ll a, b; cin >> a >> b;
v[--a].push_back(--b);
v[b].push_back(a);
}
auto p = dfs(0, -1);
ll ans = (p.first+p.second)%mod;
cout << ans;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int H,W;
cin >> H >> W;
vector<vector<string>>S(H,vector<string>(W));
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
cin >> S[i][j];
if(S[i][j] == "snuke") {
cout << (char)(j+'A') << i+1 << endl;
}
}
}
}
| 0 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair
static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;
template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T &t = T()){
v.assign(a,vector<T>(b,t));
}
//v.erase(unique(v.begin(),v.end()),v.end());
int n;
int dx[]={1,0,1,-1};
int dy[]={0,1,1,1};
vector<vint> vv;
int foo(int a,int b,int c){
int ret=0;
int now=0;
// return 0;
while(0<=a&&a<n&&0<=b&&b<n){
if(vv[a][b]==1){
now++;
}
else{
now=0;
}
// cout<<now<<endl;
ret=max(ret,now);
a+=dx[c];
b+=dy[c];
}
return ret;
}
int main(){
// vector<vint> vv;
// int n;
while(cin>>n,n){
initvv(vv,n,n);
rep(i,n){
rep(j,n){
char t;
cin>>t;
vv[i][j]=t-'0';
// cout<<vv[i][j]<<" ";
}
// cout<<endl;
}
int ans=0;
rep(i,n){
ans=max(ans,foo(0,i,0));
ans=max(ans,foo(i,0,1));
ans=max(ans,foo(0,i,2));
ans=max(ans,foo(i,0,2));
ans=max(ans,foo(i,0,3));
ans=max(ans,foo(n-1,i,3));
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
namespace Debug {
template <class A, class B>
ostream &operator<<(ostream &out, pair<A, B> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <class T>
ostream &operator<<(ostream &out, vector<T> &v) {
out << "{";
string sep;
for (T el : v) out << sep << el, sep = ", ";
out << "}";
return out;
}
void debug_out() { cerr << endl; }
template <class Head, class... Tail>
void debug_out(Head head, Tail... tail) {
cerr << ' ' << head;
debug_out(tail...);
}
} // namespace Debug
using namespace Debug;
ll get(ll n, ll val) {
if (val > n) return 0;
ll ans = 1 + (val + 1 <= n && val % 2 == 0);
;
for (ll k = 1; (val << k) <= n; k++) {
ll cur_val = (val << k);
ll cnt = (1LL << k);
ans += min(cnt, n - cur_val + 1);
;
if (val % 2 == 0) ans += min(cnt, max(0LL, n - cur_val - (1LL << k) + 1));
;
};
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
get(20, 2);
ll n, k;
cin >> n >> k;
ll L = 0, R = (n + 1) / 2 + 1;
while (R - L > 1) {
ll mid = (L + R) / 2;
if (get(n, mid * 2 + 1) >= k)
L = mid;
else
R = mid;
}
ll ans = 2 * L + 1;
L = 0, R = (n + 1) / 2 + 1;
while (R - L > 1) {
ll mid = (L + R) / 2;
if (get(n, mid * 2) >= k)
L = mid;
else
R = mid;
}
ans = max(ans, 2 * L);
cout << ans << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
cout << abs(a[i]) << " ";
else
cout << -abs(a[i]) << " ";
}
cout << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const long long SZ = 1e5 + 7;
const long long inf = 1e18;
const long long MOD = 1e9 + 7;
#define int long long
#define endl '\n'
#define krosuru int __T; cin>>__T; while (__T-- > 0)
#define yehbhitheekhai ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
void krdiyasuru()
{
int n, m, x;
cin >> n >> m >> x;
// 1 2 3
// 4 5 6
// 1 3 5
// 2 4 6
// ----- x % n
// | x / n
cout << ((x - 1) % n) * m + (x - 1) / n + 1 << endl;
return;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
yehbhitheekhai;
krosuru
{
krdiyasuru();
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, i, j, temp;
cin >> n;
bool check = 0;
if (n % 7 == 0 || n % 3 == 0) {
check = 1;
} else {
temp = n;
while (1) {
temp -= 3;
if (temp > 0 && temp % 7 == 0) {
check = 1;
break;
}
if (temp <= 0) {
break;
}
}
if (check == 0) {
temp = n;
while (1) {
temp -= 7;
if (temp > 0 && temp % 3 == 0) {
check = 1;
break;
}
if (temp <= 0) {
break;
}
}
}
}
if (check == 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long t, d, g, k, s, n, m;
vector<long long> v1(500005), v2(500005), v3(500005), ans(500005);
bool check(long long x, long long y) {
long long z = (x - 1) / k + 1;
if (y - g <= z * k) {
for (long long j = (z - 1) * k + 1; j < x && t < g; ++j) {
t++;
ans[t] = j;
}
for (long long j = x; j <= y && t < g; ++j) {
if (v3[v1[j]]-- > v2[v1[j]]) {
t++;
ans[t] = j;
}
}
return true;
}
return false;
}
int main() {
long long tek = 0, x;
cin >> m >> k >> n >> s;
g = m - k * n;
for (long long i = 1; i < m + 1; i++) {
cin >> v1[i];
}
for (long long i = 1; i < s + 1; i++) {
cin >> x;
v2[x]++;
}
for (long long i = 1; i < m + 1; i++) {
for (; d < s && ++tek <= m;) {
v3[v1[tek]]++;
if (v3[v1[tek]] <= v2[v1[tek]]) d++;
}
if (d == s && check(i, tek)) {
cout << t << endl;
for (long long l = 1; l < t + 1; l++) {
cout << ans[l] << " ";
}
return 0;
}
if (v3[v1[i]]-- <= v2[v1[i]]) {
d--;
}
}
cout << -1;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int l, r, i;
} a[2000010], b[2000010];
int n, M, m, ans, r;
bool c[2000010];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
bool cmp(Node a, Node b) {
if (a.l != b.l) return a.l < b.l;
return a.r > b.r;
}
int main() {
n = read();
m = read();
for (int i = 1; i <= m; i++) {
a[i].l = read();
a[i].r = read();
a[i].r = a[i].l + a[i].r - 1;
a[i].i = i;
}
sort(a + 1, a + 1 + m, cmp);
r = a[1].r;
for (int i = 2; i <= m; i++)
if (a[i].r <= r)
c[i] = true;
else
r = a[i].r;
r = 0;
for (int i = 1; i <= m; i++)
if (!c[i]) b[++r] = a[i];
M = m;
m = r;
memset(c, 0, sizeof(c));
int l = 1;
r = 2;
c[a[1].i] = true;
while (l <= m) {
while (r <= m && b[l].r + 1 >= b[r].l) r++;
r--;
if (l == r)
l++, c[b[l].i] = 1, ans++;
else
c[b[r].i] = 1, l = r, ans++;
}
printf("%d\n", M - ans);
for (int i = 1; i <= M; i++)
if (!c[i]) printf("%d ", i);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long x,y,z;
cin>>x>>y>>z;
cout<<((x-z)/(y+z))<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int BITS = 62;
long long N, K;
vector<long long> first, second;
void flip_bit(int bit) {
vector<long long> A, B;
for (int i = 0; i < N; i++)
(second[i] & 1LL << bit ? A : B).push_back(second[i] ^ 1LL << bit);
merge(A.begin(), A.end(), B.begin(), B.end(), second.begin());
}
long long starting_with(int low_bit) {
long long total = 0;
int pos = 0;
for (int i = 0, j = 0; i < N; i = j) {
while (j < N && (first[i] >> low_bit) == (first[j] >> low_bit)) j++;
while (pos < N && (second[pos] >> low_bit) <= (first[i] >> low_bit)) {
if ((second[pos] >> low_bit) == (first[i] >> low_bit)) total += j - i;
pos++;
}
}
return total;
}
int main() {
scanf("%lld %lld", &N, &K);
first.assign(N, 0);
for (int i = 1; i < N; i++) {
long long parent, weight;
scanf("%lld %lld", &parent, &weight);
first[i] = first[parent - 1] ^ weight;
}
sort(first.begin(), first.end());
second = first;
long long answer = 0;
for (int bit = BITS - 1; bit >= 0; bit--) {
long long count = starting_with(bit);
if (K <= count) {
continue;
} else {
K -= count;
answer += 1LL << bit;
flip_bit(bit);
}
}
printf("%lld\n", answer);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int x[8], a[7], key, buff, sum = 1000000007, h[8];
string ham[4],
str[] = {"", "abbb", "aabb", "abab", "abaa", "aaab", "aaba", "abba"};
bool flag;
int main() {
for (int i = 0; i < 6; i++) cin >> a[i];
if ((a[5] - a[2] + a[1]) % 2 != 0) {
cout << -1;
return 0;
}
key = (a[5] - a[2] + a[1]) / 2;
if (key < 0) {
cout << -1;
return 0;
}
for (x[6] = 0; x[6] <= key; x[6]++) {
flag = 0;
x[7] = key - x[6];
buff = a[2] - a[1] + x[6] + x[7];
if (buff < 0) {
flag = 1;
continue;
}
if ((a[1] - a[0] + a[3] - 2 * x[6]) % 2 != 0) {
flag = 1;
continue;
}
x[2] = (a[1] - a[0] + a[3] - 2 * x[6]) / 2;
if (x[2] < 0) {
flag = 1;
continue;
}
x[1] = a[2] - x[2] - buff;
if (x[1] < 0) {
flag = 1;
continue;
}
if ((a[0] + a[4] - x[1] - x[2] - buff - 2 * x[7]) % 2 != 0) {
flag = 1;
continue;
}
x[4] = (a[0] + a[4] - x[1] - x[2] - buff - 2 * x[7]) / 2;
if (x[4] < 0) {
flag = 1;
continue;
}
x[5] = a[4] - x[2] - x[4] - x[7];
if (x[5] < 0) {
flag = 1;
continue;
}
x[3] = buff - x[5];
if (x[3] < 0) {
flag = 1;
continue;
}
if (!flag && (x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7]) < sum) {
sum = (x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7]);
for (int j = 1; j <= 7; j++) h[j] = x[j];
}
}
if (sum == 1000000007) {
cout << -1;
return 0;
}
cout << sum << endl;
for (int j = 1; j <= 7; j++) {
for (int k = 0; k < h[j]; k++) {
for (int i = 0; i < 4; i++) {
ham[i] += str[j][i];
}
}
}
for (int i = 0; i < 4; i++) cout << ham[i] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string fix = "0123456789";
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int main() {
long long n, m;
cin >> n >> m;
long long pairr = n / m;
long long mod = n % m;
long long mn = n - (m - 1);
mn--;
pairr--;
long long pp = pairr + 1;
cout << ((pairr * (pairr + 1) / 2) * (m - mod)) + (pp * (pp + 1) / 2) * mod
<< " ";
cout << mn * (mn + 1) / 2 << " ";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, a[((int)2001 * 1000)], b[((int)2001 * 1000)], comp[((int)2001 * 1000)];
vector<int> v, e[((int)2001 * 1000)], vec[((int)2001 * 1000)];
bool mark[((int)2001 * 1000)], dead[((int)2001 * 1000)],
type[((int)2001 * 1000)];
void dfs(int x, int root) {
mark[x] = 1;
comp[x] = root;
vec[root].push_back(x);
for (int i = 0; i < e[x].size(); i++)
if (!mark[e[x][i]]) dfs(e[x][i], root);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &a[i], &b[i]), v.push_back(a[i]), v.push_back(b[i]);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(),
b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin(),
e[a[i]].push_back(b[i]), e[b[i]].push_back(a[i]);
for (int i = 0; i < v.size(); i++)
if (!mark[i]) {
dfs(i, i);
int cnt = 0;
for (int j = 0; j < vec[i].size(); j++) cnt += e[vec[i][j]].size();
if (cnt > 2 * (int)vec[i].size()) return cout << "-1\n", 0;
type[i] = (cnt == 2 * (int)vec[i].size());
}
int l = 0, r = 1e9 + 10;
while (l < r - 1) {
int mid = (l + r) / 2;
memset(dead, 0, sizeof dead);
bool flg = 1;
for (int i = (int)v.size() - 1; i >= 0; i--) {
if (v[i] <= mid) break;
if (type[comp[i]] || dead[comp[i]]) {
flg = 0;
break;
}
dead[comp[i]] = 1;
}
if (flg)
r = mid;
else
l = mid;
}
cout << r << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int size;
cin >> size;
long long a[size][size];
map<int, long long> r;
map<int, long long> c;
long long d = 0, row;
long long ad = 0, col;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cin >> a[i][j];
if (a[i][j] == 0) {
col = i;
row = j;
}
r[i] = r[i] + a[i][j];
c[j] = c[j] + a[i][j];
if (i == j) d = d + a[i][j];
if ((i + j) == (size - 1)) ad = ad + a[i][j];
}
}
long long ans;
if (col != 0)
ans = r[0] - r[col];
else {
if (size != 1)
ans = r[col + 1] - r[col];
else
ans = 1;
}
r[col] += ans;
c[row] += ans;
if (col == row) d = d + ans;
if ((row + col) == (size - 1)) ad = ad + ans;
long long search = r[0];
for (int i = 0; i < size; i++) {
if ((r[i] != search) || (c[i] != search) || ans <= 0 || d != search ||
ad != search) {
cout << "-1";
return 0;
}
}
cout << ans;
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
int s=0,f=1;char ch=getchar();
while(!isdigit(ch) && ch!='-')ch=getchar();
if(ch=='-')ch=getchar(),f=-1;
while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
return ~f?s:-s;
}
const int maxn=3e5+20;
int n,b[maxn],a[maxn];
int main()
{
n=read();
REP(i,1,n)a[i]=read(),b[i]=a[i]==i;
REP(i,2,n-1)
{
if(!b[i-1] && !b[i] && !b[i+1])puts("No"),exit(0);
}
REP(i,1,n)
{
if(b[i]==0)
{
int j=i;
while(j<n && b[j]!=b[j+1])++j;
REP(k,i,j)if(i<=a[k] && a[k]<=j);
else puts("No"),exit(0);
int mx1=0,mx2=0;
REP(k,i,j)
{
if(k<a[k])
{
if(a[k]<mx1)puts("No"),exit(0);
mx1=a[k];
}
else if(k>a[k])
{
if(a[k]<mx2)puts("No"),exit(0);
mx2=a[k];
}
}
i=j;
}
}
puts("Yes");
return 0;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
int main() {
long long N, S=0; cin >> N;
vector<int> A(N),B(N);
for(int i=0;i<N;++i) {
cin >> A[i]; S+=A[i];
}
if(S%(N*(N+1)/2)) {
cout << "NO" << endl; return 0;
}
int a=S/(N*(N+1)/2);
for(int i=0;i<N;++i) B[i] = A[(i+1)%N]-A[i];
for(int i=0;i<N;++i) {
if((B[i]-a)%N||B[i]>a) {
cout << "NO" << endl; return 0;
}
}
cout << "YES" << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int ceil(int a, int b) {
if (a % b == 0) {
return a / b;
}
return (a / b) + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t > 0) {
t--;
int n, m, k;
cin >> n >> m >> k;
int lg = ceil(n, m), sm = n / m;
int lt = n - sm * m;
int st = m - lt;
int bas = 0;
for (int i = 0; i < k; i++) {
int c = bas;
for (int j = 0; j < lt; j++) {
cout << lg;
for (int k = 0; k < lg; k++) {
cout << ' ' << c + 1;
c++;
c %= n;
}
cout << '\n';
}
for (int j = 0; j < st; j++) {
cout << sm;
for (int k = 0; k < sm; k++) {
cout << ' ' << c + 1;
c++;
c %= n;
}
cout << '\n';
}
bas += (lg * lt);
bas %= n;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % mod;
y >>= 1ll;
x = x * x % mod;
}
return ret;
}
long long qpow(long long x, long long y, long long m) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % m;
y >>= 1ll;
x = x * x % m;
}
return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int m, n, k, a[110][110], _max, p, cnt, res, g, ans;
int main() {
cin >> n >> m >> k;
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= m; ++j) a[i][j] = read();
for (register int j = 1; j <= m; ++j) {
_max = p = g = 0;
for (register int i = 1; i <= n; ++i)
if (a[i][j] == 1) {
cnt = 0;
for (register int _ = i; _ <= min(i + k - 1, n); ++_)
if (a[_][j] == 1) cnt++;
if (cnt > _max) _max = cnt, g = p;
p++;
}
res += _max;
ans += g;
}
cout << res << ' ' << ans << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
struct st{int a,b,c;};
bool operator<(st&a,st&b){
if(a.a!=b.a)return a.a<b.a;
if(a.b!=b.b)return a.b<b.b;
if(a.c!=b.c)return a.c<b.c;
return false;
}
bool operator==(st&a,st&b){
return a.a==b.a&&a.b==b.b&&a.c==b.c;
}
int main(){
st a,b;cin>>a.a>>a.b>>a.c>>b.a>>b.b>>b.c;
if(a<b)swap(a,b);//a??????????????\????????????????????????
if(a==b){puts("0");return 0;}
if(P(a.b,a.c)<=P(b.b,b.c))cout<<a.a-b.a<<endl;
else cout<<a.a-b.a+1<<endl;
} | 0 |
#include <bits/stdc++.h>
std::string a, b;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b;
int i = 0, j = 0;
while (a[i] == '0' && i < a.size()) i++;
while (b[j] == '0' && j < b.size()) j++;
int s, k;
int c = 0;
if (a.size() - i > b.size() - j)
c = 1;
else if (a.size() - i < b.size() - j)
c = -1;
else {
for (k = i, s = j; k < a.size() && s < b.size(); k++, s++) {
int r, t;
r = a[k] - 48;
t = b[s] - 48;
if (r > t) {
c = 1;
break;
}
if (r < t) {
c = -1;
break;
}
}
}
if (c == -1)
cout << "<";
else if (c == 1)
cout << ">" << endl;
else
cout << "=" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int N, M, i, j, k;
string A, B;
bool ok[25];
char c[25];
bool Check(int i, int j) {
string s, sir;
int cnt;
if (i == N - 1) return true;
for (cnt = i + 1; cnt < N; cnt++) s += B[cnt];
for (cnt = 1; cnt <= M; cnt++)
if (ok[cnt] == false && cnt != j) sir += c[cnt];
sort(sir.begin(), sir.end());
return sir <= s;
}
int main() {
cin >> A >> B;
N = A.size();
for (i = 0; i < N; i++) c[++M] = A[i];
sort(c + 1, c + M + 1);
if (A.size() < B.size()) {
for (j = M; j >= 1; j--) cout << c[j];
return 0;
}
for (i = 0; i < N; i++) {
for (j = M; j >= 1; j--) {
if (c[j] > B[i]) continue;
if (c[j] == B[i] && ok[j] == false && Check(i, j) == true) {
ok[j] = true;
cout << c[j];
break;
}
if (c[j] < B[i] && ok[j] == false) {
cout << c[j];
ok[j] = true;
for (k = M; k >= 1; k--) {
if (ok[k] == false) cout << c[k];
}
return 0;
}
}
}
return 0;
}
| 3 |
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include <math.h>
#include<algorithm>
#include<functional>
#define ll long long
#define inf 999999999
#define pa pair<int,int>
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
using namespace std;
class Point{
public:
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (Point p) {return Point(x+p.x,y+p.y);}
Point operator - (Point p) {return Point(x-p.x,y-p.y);}
Point operator * (double a) {return Point(x*a,y*a);}
Point operator / (double a) {return Point(x/a,y/a);}
double absv() {return sqrt(norm());}
double norm() {return x*x+y*y;}
bool operator < (const Point &p) const{
return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
}
};
typedef Point Vector;
struct Segment{
Point p1,p2;
};
double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
//----------------kokomade temple------------
int main(){
int n;
cin>>n;
double a1,a2,a3,a4,a5,a6,a7,a8;
Point p0,p1,p2,p3;
Vector v01,v31,v02,v03,v23,v21,v20;
for(int i=0;i<n;i++){
cin>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8;
p0.x=a1,p0.y=a2;
//cout<<a2<<endl;
p1.x=a3,p1.y=a4;
p2.x=a5,p2.y=a6;
p3.x=a7,p3.y=a8;
v01=p0-p1;
v02=p0-p2;
v03=p0-p3;
v23=p2-p3;
v21=p2-p1;
v20=p2-p0;
v31=p3-p1;
if(fabs(cross(v01,v02))<EPS && fabs(cross(v01,v03))<EPS){
if(dot(p0-p3,p0-p2)>EPS && dot(p1-p3,p1-p2)>EPS &&dot(p0-p3,p1-p3)>EPS) cout<<"0"<<endl;
else cout<<"1"<<endl;
//cout<<dot(p0-p3,p0-p2)<<" "<<dot(p1-p3,p1-p2)<<endl;
}
else if(cross(v01,v02)*cross(v01,v03)<=0 && cross(v23,v21)*cross(v23,v20)<=0) cout<<"1"<<endl;
else cout<<"0"<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i = 2, j, high = 0, Si = 1;
scanf("%d", &n);
while (n >= Si) {
n -= Si;
Si = i * (i + 1) / 2;
high++;
i++;
}
printf("%d\n", high);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int rows[4] = {-1, 0, 0, 1};
int cols[4] = {0, -1, 1, 0};
bool withinGrid(int x, int y, int m, int n) {
return ((x >= 0) && (y >= 0) && (x < m) && (y < n));
}
bool isSafe(int x, int y, int m, int n, vector<vector<bool>> &visited,
vector<string> &grid) {
return ((x >= 0) && (y >= 0) && (x < m) && (y < n) && !visited[x][y] &&
(grid[x][y] != '#'));
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int test;
cin >> test;
while (test--) {
int m, n;
cin >> m >> n;
vector<string> grid(m);
for (int i = 0; i < m; i++) {
cin >> grid[i];
}
bool f = true;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'B') {
for (int idx = 0; idx < 4; idx++) {
if (withinGrid(i + rows[idx], j + cols[idx], m, n)) {
if (grid[i + rows[idx]][j + cols[idx]] == 'G') {
f = false;
break;
} else if (grid[i + rows[idx]][j + cols[idx]] == '.') {
grid[i + rows[idx]][j + cols[idx]] = '#';
}
}
}
}
if (!f) {
break;
}
}
if (!f) {
break;
}
}
if (grid[m - 1][n - 1] == '#') {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'G') {
f = false;
break;
}
}
if (!f) {
break;
}
}
}
if (f) {
vector<vector<bool>> visited(m, vector<bool>(n, false));
queue<pair<int, int>> q;
if (grid[m - 1][n - 1] == '.') {
q.push({m - 1, n - 1});
visited[m - 1][n - 1] = true;
}
while (!q.empty()) {
pair<int, int> box = q.front();
q.pop();
int x_co = box.first, y_co = box.second;
for (int idx = 0; idx < 4; idx++) {
if (isSafe(x_co + rows[idx], y_co + cols[idx], m, n, visited, grid)) {
q.push({x_co + rows[idx], y_co + cols[idx]});
visited[x_co + rows[idx]][y_co + cols[idx]] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'G' && !visited[i][j]) {
f = false;
break;
} else if (grid[i][j] == 'B' && visited[i][j]) {
f = false;
break;
}
}
if (!f) {
break;
}
}
}
if (f) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
for (int i = 0; i <= 9; i++) {
cout << i << endl;
getline(cin, s);
if (s == "great!" || s == "cool" || s == "not bad" ||
s == "don't touch me" || s == "don't think so") {
cout << "normal";
cout << flush;
return 0;
}
if (s == "worse" || s == "go die in a hole" || s == "terrible" ||
s == "are you serious?") {
cout << "grumpy";
cout << flush;
return 0;
}
}
}
| 2 |
#include <iostream>
using namespace std;
const int N=55;
int n,a[N],h=0;//head
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(abs(a[i])>abs(a[h]))h=i;
}
cout<<2*n-1<<'\n';
for(int i=0;i<n;i++){
cout<<h+1<<' '<<i+1<<'\n';
}
if(a[h]>0){
for(int i=0;i<n-1;i++){
cout<<i+1<<' '<<i+2<<'\n';
}
}else{
for(int i=n;i>1;i--){
cout<<i<<' '<<i-1<<'\n';
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s, n;
cin >> s >> n;
vector<pair<int, int>> vp;
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
vp.push_back(make_pair(x, y));
}
sort(vp.begin(), vp.end());
for (int i = 0; i < n; i++) {
if (s > vp[i].first)
s += vp[i].second;
else {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, L, a[N], c[N];
bool check(int P) {
memset(c, 0, sizeof(c));
for (int i = (L); i < (n + 1 - L + 1); ++i) {
if (P < i && i < n + 1 - i && a[i] != a[n + 1 - i]) return false;
if (i <= P)
++c[a[i]];
else if (n + 1 - i <= P && --c[a[i]] < 0)
return false;
}
return true;
}
int getpre() {
int l = L, r = n - L + 1;
while (l + 1 < r) {
int z = (l + r) >> 1;
check(z) ? r = z : l = z;
}
return (check(l) ? l : r);
}
long long solve() {
for (int i = (1); i < (n + 1); ++i) ++c[a[i]];
int odd = 0;
for (int i = (1); i < (n + 1); ++i) odd += c[i] & 1;
if (odd > 1) return 0;
L = 1;
while (L <= n && a[L] == a[n + 1 - L]) ++L;
if (L > n) return 1ll * n * (n + 1) / 2;
long long ret = 1ll * L * (n + 1 - getpre());
reverse(a + 1, a + 1 + n);
ret += 1ll * L * (n + 1 - L - getpre());
return ret;
}
int main() {
scanf("%d", &n);
for (int i = (1); i < (n + 1); ++i) scanf("%d", a + i);
cout << solve() << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#pragma GCC target("sse2")
void opting() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
}
long long inp() {
long long x;
cin >> x;
return x;
}
void comping(vector<long long> &vec) {
sort((vec).begin(), (vec).end());
vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
const long long maxn = 2e3 + 69, inf = 1e9 + 420, lg = 21, delta = 998244353;
long long fact[maxn];
long long dera[maxn];
void calc_fact() {
fact[0] = 1;
for (long long i = 1; i < maxn; i++) fact[i] = (i * fact[i - 1]) % delta;
}
void calc_dera() {
long long tmp[maxn];
fill(tmp, tmp + maxn, 0);
tmp[0] = tmp[1] = 0;
dera[0] = 1;
dera[1] = 0;
for (long long i = 2; i < maxn; i++) {
tmp[i] = (dera[i - 2] + (i - 2) * tmp[i - 1]) % delta;
dera[i] = ((i - 1) * tmp[i]) % delta;
}
}
long long dp[maxn][maxn];
void calc_DP() {
for (long long i = 0; i < maxn; i++) {
dp[0][i] = fact[i];
dp[i][0] = dera[i];
dp[1][i] = (i * fact[i]) % delta;
}
for (long long a = 2; a < maxn; a++)
for (long long b = 1; b < maxn - 2; b++)
dp[a][b] = ((((a - 1) * dp[a - 2][b + 1]) % delta) +
((b * dp[a - 1][b]) % delta)) %
delta;
}
void predo() {
calc_dera();
calc_fact();
calc_DP();
}
long long A_fen[maxn], B_fen[maxn];
void add_A(long long idx, long long val) {
if (!idx) return;
for (; idx < maxn; idx += (idx & -idx)) A_fen[idx] += val;
}
void add_B(long long idx, long long val) {
if (!idx) return;
for (; idx < maxn; idx += (idx & -idx)) B_fen[idx] += val;
}
long long sum_A(long long idx) {
long long sm = 0;
for (; idx; idx -= (idx & -idx)) sm += A_fen[idx];
return sm;
}
long long sum_B(long long idx) {
long long sm = 0;
for (; idx; idx -= (idx & -idx)) sm += B_fen[idx];
return sm;
}
void reset_fen() {
memset(A_fen, 0, sizeof A_fen);
memset(B_fen, 0, sizeof B_fen);
}
long long ans;
long long Xe = 1;
long long n;
long long mat[maxn][maxn];
void calc_line(long long id) {
set<long long> above, below;
long long same = 0;
reset_fen();
long long Ye = 0;
for (long long i = n - 1; i > -1; i--) {
long long v = mat[id - 1][i];
long long u = mat[id][i];
if (above.find(u) != above.end()) {
add_A(u, 1);
same++;
} else
add_B(u, 1);
long long st_a = sum_A(u - 1);
long long st_b = sum_B(u - 1) - (below.find(v) != below.end() and v < u);
long long a = same;
long long b = (n - i) - same;
Ye = (Ye + st_a * dp[a - 1][b] + st_b * dp[a][b - 1]) % delta;
if (below.find(v) != below.end()) {
add_B(v, -1);
add_A(v, 1);
same++;
}
above.insert(v);
below.insert(u);
}
ans = (ans + Xe * Ye) % delta;
Xe = (Xe * dera[n]) % delta;
}
void input() {
cin >> n;
for (long long i = 1; i <= n; i++)
for (long long j = 0; j < n; j++) cin >> mat[i][j];
}
void solve() {
for (long long i = n; i; i--) calc_line(i);
cout << ans << endl;
}
int main() {
opting();
predo();
while (0) {
long long a, b;
cin >> a >> b;
cout << dp[a][b] << endl;
cout << fact[a] << endl;
cout << dera[a] << endl;
}
input();
solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int fx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int fy[] = {1, 1, 0, -1, -1, -1, 0, 1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, i, j, k;
cin >> n;
long long ara[100005] = {0};
for (i = 0; i < n; i++) {
cin >> k;
ara[k]++;
}
long long cal[100005] = {0};
cal[1] = ara[1];
for (i = 2; i < 100005; i++) {
cal[i] = max(cal[i - 1], cal[i - 2] + ara[i] * i);
}
cout << cal[100004] << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e2 + 10;
int a[MAX_N][MAX_N], n;
bool mark[MAX_N];
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) cin >> a[i][j];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 3) {
mark[i] = 1;
mark[j] = 1;
} else if (a[i][j] == 2)
mark[j] = 1;
else if (a[i][j] == 1)
mark[i] = 1;
}
}
cout << n - count(mark, mark + n, 1) << endl;
for (int i = 0; i < n; ++i) {
if (!mark[i]) cout << i + 1 << " ";
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,i;
cin>>n;
int b[n-1];
for(i=0;i<n-1;i++)
cin>>b[i];
int sum=b[0]+b[n-2];
for(i=1;i<n-1;i++)
{
sum+=min(b[i],b[i-1]);
}
cout<<sum;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int t, i, a[1001], n, k;
int main() {
cin >> n;
while (i++ < n) cin >> t, k = max(++a[t], k);
cout << n - k;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
char c[4] = {'a', 'b', 'c', 'd'};
for (int i = 0; i < n; i++) {
cout << c[i % 4];
}
cout << endl;
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
typedef vector<string> vstring;
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
const int INF = 1 << 30;
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,-1,1};
#define stp(x) setprecision(x)
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,c;
cin>>n>>c;
int a,sum=0;
rep(i,c){
cin>>a;
sum+=a;
}
if(sum%(n+1)!=0){
cout<<sum/(n+1)+1<<'\n';
}
else{
cout<<sum/(n+1)<<'\n';
}
return(0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r, x;
long long sum, lazy;
node *lt, *rt;
node(int a, int b) : l(a), r(b), x(-1), sum(0), lt(NULL), rt(NULL), lazy(0) {
if (r == l)
x = l + 1;
else {
int mid = (l + r) >> 1;
lt = new node(l, mid);
rt = new node(mid + 1, r);
}
}
~node() {
if (lt) delete lt, delete rt;
}
void pass() {
lt->x = rt->x = x;
lt->sum += lazy * (lt->r - lt->l + 1);
rt->sum += lazy * (rt->r - rt->l + 1);
lt->lazy += lazy;
rt->lazy += lazy;
lazy = 0;
}
void update(int y, int a, int b) {
if (a <= l && b >= r && x >= 0) {
sum += (long long)(r - l + 1) * abs(y - x);
lazy += abs(y - x);
x = y;
} else {
if (x >= 0 && lazy) pass();
int mid = (l + r) >> 1;
if (a <= mid) lt->update(y, a, b);
if (b > mid) rt->update(y, a, b);
sum = lt->sum + rt->sum;
if (rt->x == lt->x)
x = lt->x;
else
x = -1;
}
}
long long query(int a, int b) {
if (a <= l && b >= r) return sum;
if (x >= 0) pass();
long long res = 0;
int mid = (l + r) >> 1;
if (a <= mid) res += lt->query(a, b);
if (b > mid) res += rt->query(a, b);
return res;
}
};
int main() {
int n, m;
cin >> n >> m;
node t(0, n - 1);
for (int i = 0; i < m; i++) {
int op;
cin >> op;
if (op == 1) {
int l, r, x;
cin >> l >> r >> x;
t.update(x, l - 1, r - 1);
} else {
int l, r;
cin >> l >> r;
cout << t.query(l - 1, r - 1) << endl;
;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long dist[N], a[N];
vector<vector<pair<int, long long> > > g(N);
int n, m;
void dij() {
priority_queue<pair<long long, int> > pq;
for (int i = 1; i <= n; i++) {
pq.push(make_pair(-a[i], i));
dist[i] = a[i];
}
while (pq.size()) {
pair<long long, int> t = pq.top();
pq.pop();
int node = t.second;
if (dist[node] != -t.first) continue;
for (int i = 0; i < g[node].size(); i++) {
int v = g[node][i].first;
long long w = g[node][i].second;
if (dist[v] > dist[node] + w) {
dist[v] = dist[node] + w;
pq.push(make_pair(-dist[v], v));
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long w;
int u, v;
cin >> u >> v >> w;
g[u].push_back(make_pair(v, w * 2));
g[v].push_back(make_pair(u, w * 2));
}
for (int i = 1; i <= n; i++) cin >> a[i];
dij();
for (int i = 0; i < n; i++) cout << dist[i + 1] << " ";
return 0;
}
| 4 |
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#define ll long long
#define ull unsigned long long
#define mod 1000000007
using namespace std;
int main() {
while(true){
char x[4];
for (int i = 0; i < 4; ++i) {
cin >> x[i];
x[i] -= '0';
}
if (x[0] == x[1] && x[1] == x[2] && x[2] == x[3]) {
if(x[0] == 0) break;
else cout << "NA" << "\n";
continue;
}
int counter = 0;
int t = x[0] * 1000 + x[1] * 100 + x[2] * 10 + x[3];
while (t != 6174) {
++counter;
sort(x, x + 4);
t = x[3] * 999 + x[2] * 90 - x[1] * 90 - x[0] * 999;
x[0] = t / 1000;
x[1] = (t / 100) - x[0] * 10;
x[2] = (t / 10) - x[0] * 100 - x[1] * 10;
x[3] = t - x[0] * 1000 - x[1] * 100 - x[2] * 10;
}
cout << counter << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e8 + 1;
bitset<maxn> have;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> ans = {1};
int cur = 0;
while (cur < m && ans.size() < n) {
int s = ans.size() / 2;
if (cur + s > m) {
int need = m - cur;
int d = need * 2;
int curp = ans.size() - 1;
ans.push_back(ans[curp] + ans[curp - d]);
cur += need;
} else
ans.push_back(ans.back() + 1), cur += s;
}
if (cur < m) {
puts("-1");
return 0;
}
for (int i = 0; i < ans.size(); i++) {
have[ans[i]] = true;
for (int j = i + 1; j < ans.size(); j++)
have[ans[j]] = have[ans[i] + ans[j]] = true;
}
int l = ans.back();
while (ans.size() < n) {
while (have[l]) l++;
have[l] = true;
for (int i = 0; i < ans.size(); i++) have[ans[i] + l] = true;
ans.push_back(l);
}
sort(ans.begin(), ans.end());
for (int v : ans) printf("%d ", v);
puts("");
return 0;
}
| 5 |
/*
神题
1.b[i]=a[i]~a[n*2-i]
2.b中i之前所有数都不能在b[i]~b[i+1]之间(不包括端点)
f[i][j][k]=填完了b的后i个数,b的倒数第i个数有j种选择,它在这j种选择中排第k小,方案数
转移时计算选择种类数,枚举当前选择哪种
O(n^4)
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110
#define mod 1000000007
using namespace std;
int n, w[N], f[N][N][N], sum, ans, now, x, y, x1, x2;
int main(){
scanf("%d", &n); for(int i=1; i<=n*2-1; i++)scanf("%d", &w[i]);
sort(w+1, w+n*2);
sum=1; for(int i=2; i<=n*2-1; i++)if(w[i-1]<w[i])sum++;
memset(f, 0, sizeof(f)); f[1][1][1]=1; now=1;
for(int i=1; i<=n-1; i++){
x1=w[n-i+1]==w[n-i]?0:1;
x2=w[n+i-1]==w[n+i]?0:1;
for(int j=1; j<=now; j++)
for(int k=1; k<=j; k++){
x=j+x1+x2; y=k+x1;
for(int ii=1; ii<=y-1; ii++)f[i+1][x-y+ii+1][ii]=(f[i+1][x-y+ii+1][ii]+f[i][j][k])%mod;
f[i+1][x][y]=(f[i+1][x][y]+f[i][j][k])%mod;
for(int ii=y+1; ii<=x; ii++)f[i+1][x-ii+y+1][y+1]=(f[i+1][x-ii+y+1][y+1]+f[i][j][k])%mod;
}
now+=x1+x2;
}
ans=0;
for(int i=1; i<=sum; i++)
for(int j=1; j<=i; j++)ans=(ans+f[n][i][j])%mod;
printf("%d", ans);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, D = N * 22;
int n, px[N], py[N], a[N], root[N];
int sum[D], l[D], r[D], dn;
int m, key, ans;
int insert(int d, int lt, int rt, int w) {
int md = (lt + rt) >> 1;
int nd = ++dn;
sum[nd] = sum[d] + 1;
if (lt != rt) {
if (w <= md) {
l[nd] = insert(l[d], lt, md, w);
r[nd] = r[d];
} else {
r[nd] = insert(r[d], md + 1, rt, w);
l[nd] = l[d];
}
}
return nd;
}
int query(int d, int lt, int rt, int lq, int rq) {
if (!d) {
return 0;
}
if (lq <= lt && rt <= rq) {
return sum[d];
}
int md = (lt + rt) >> 1;
int ret = 0;
if (lq <= md) {
ret += query(l[d], lt, md, lq, rq);
}
if (md < rq) {
ret += query(r[d], md + 1, rt, lq, rq);
}
return ret;
}
int init() {
int x;
scanf("%d", &x);
return (x - 1 + key) % n + 1;
}
int main() {
scanf("%d", &n);
for (int i = 1, v; i <= n; ++i) {
scanf("%d", &v), px[v] = i;
}
for (int i = 1, v; i <= n; ++i) {
scanf("%d", &v), py[v] = i;
}
for (int v = 1; v <= n; ++v) {
a[px[v]] = v;
}
root[0] = ++dn;
for (int i = 1; i <= n; ++i) {
root[i] = insert(root[i - 1], 1, n, py[a[i]]);
}
scanf("%d", &m);
ans = -1;
for (int im = 1; im <= m; ++im) {
key = ans + 1;
int l1 = init(), r1 = init(), l2 = init(), r2 = init();
if (l1 > r1) swap(l1, r1);
if (l2 > r2) swap(l2, r2);
ans = query(root[r1], 1, n, l2, r2) - query(root[l1 - 1], 1, n, l2, r2);
printf("%d\n", ans);
}
}
| 3 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MOD 10000
string s;
short dp[2][3][10][500][501]; //[free][増減][prev][余り][index]
int m;
int solve( bool fr, int updw, int pre, int mod, int index){
if( index == s.size() ) return !mod;
if( dp[fr][updw][pre][mod][index] != -1 ) return dp[fr][updw][pre][mod][index];
int ret = 0, end = fr ? 9 : s[index];
for(int i = 0, u ; i <= end ; i++ ){
switch(updw){
case 0: //さいしょ
if( pre && pre == i ) continue;
else if( pre == 0 ) u = 0;
else if( pre > i ) u = 1;
else u = 2;
break;
case 1: //次あっぷ
if( pre >= i ) continue;
else u = 2;
break;
case 2: //次だうん
if( pre <= i ) continue;
else u = 1;
break;
}
ret += solve( fr|(i!=s[index]), u, i, (mod*10+i)%m, index+1);
}
return dp[fr][updw][pre][mod][index] = ret % MOD;
}
void java(){
for(int i = s.size() - 1 ; i >= 0 ; i-- ){
if(s[i] == 0) s[i] = 9;
else{
s[i]--;
break;
}
}
}
int main(){
string s1;
cin >> s >> s1 >> m;
for(int i = 0 ; i < s.size() ; i++ ) s[i] -= '0';
java();
fill_n( ****dp, 2 * 3 * 10 * 500 * 501, -1);
int an = solve( 0, 0, 0, 0, 0);
s = s1;
for(int i = 0 ; i < s.size() ; i++ ) s[i] -= '0';
fill_n( ****dp, 2 * 3 * 10 * 500 * 501, -1);
int bn = solve( 0, 0, 0, 0, 0);
cout << ( bn - an + MOD ) % MOD << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> graph[500005];
vector<int> tgraph[500005];
int degree[500005];
int uf[500005];
int pre[500005];
int onenode[500005];
vector<int> onenode_conn[500005];
int dominated_by[500005];
int onelink[500005];
int fin(int a) {
if (uf[a] == a) return a;
return uf[a] = fin(uf[a]);
}
void un(int a, int b) {
a = fin(a);
b = fin(b);
uf[a] = b;
}
void answer(vector<int>& ans) {
printf("Yes\n%d\n", (int)ans.size());
for (int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("\n");
}
void findpath(set<int>& path, int from, int to, bool indirect = false) {
queue<int> bfs;
bfs.push(from);
pre[from] = -1;
while (!bfs.empty()) {
int cur = bfs.front();
bfs.pop();
for (int i = 0; i < graph[cur].size(); i++) {
int nxt = graph[cur][i];
if (degree[nxt] == 1 && nxt != to) continue;
if (indirect && cur == from && nxt == to) continue;
if (pre[nxt] == 0) {
pre[nxt] = cur;
bfs.push(nxt);
}
}
}
while (to != -1) {
path.insert(to);
to = pre[to];
}
for (int i = 1; i <= n; i++) pre[i] = 0;
}
void findpath_onenode(set<int>& path, int from, int onenodeid) {
queue<int> bfs;
bfs.push(from);
pre[from] = -1;
int to = 0;
while (!bfs.empty()) {
int cur = bfs.front();
bfs.pop();
for (int i = 0; i < tgraph[cur].size(); i++) {
int nxt = tgraph[cur][i];
if (pre[nxt] == 0) {
pre[nxt] = cur;
bfs.push(nxt);
}
if (onelink[nxt] == onenodeid && nxt != from) {
to = nxt;
goto loop_end;
}
}
}
loop_end:
while (to != -1) {
path.insert(to);
to = pre[to];
}
for (int i = 1; i <= n; i++) pre[i] = 0;
}
void work(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
degree[u]++;
degree[v]++;
}
if (n <= 2) {
printf("No\n");
return;
}
int node0 = -1;
int hnodes = 0;
for (int i = 1; i <= n; i++) {
uf[i] = i;
if (degree[i] >= 3) hnodes++;
degree[i] %= 3;
if (!degree[i]) node0 = i;
}
if (!hnodes) {
printf("No\n");
return;
}
if (node0 != -1) {
vector<int> ans;
for (int i = 1; i <= n; i++)
if (i != node0) ans.push_back(i);
answer(ans);
return;
}
for (int i = 1; i <= n; i++) {
if (degree[i] != 1) continue;
for (int j = 0; j < graph[i].size(); j++) {
if (degree[graph[i][j]] == 1) {
vector<int> ans;
for (int k = 1; k <= n; k++)
if (k != i && k != graph[i][j]) ans.push_back(k);
answer(ans);
return;
} else {
onelink[graph[i][j]] = i;
}
}
}
for (int i = 1; i <= n; i++) {
if (degree[i] != 2) continue;
vector<int> nxt;
for (int j = 0; j < graph[i].size(); j++) {
if (degree[graph[i][j]] == 1) nxt.push_back(graph[i][j]);
if (nxt.size() >= 2) break;
}
if (nxt.size() >= 2) {
vector<int> ans;
for (int k = 1; k <= n; k++)
if (k != i && k != nxt[0] && k != nxt[1]) ans.push_back(k);
answer(ans);
return;
}
}
for (int i = 1; i <= n; i++) {
if (degree[i] != 2) continue;
for (int j = 0; j < graph[i].size(); j++) {
if (degree[graph[i][j]] != 2) continue;
if (i > graph[i][j]) continue;
tgraph[i].push_back(graph[i][j]);
tgraph[graph[i][j]].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
if (degree[i] != 2) continue;
for (int j = 0; j < graph[i].size(); j++) {
if (degree[graph[i][j]] != 2) continue;
if (i > graph[i][j]) continue;
if (fin(i) == fin(graph[i][j])) {
set<int> path;
findpath(path, i, graph[i][j], true);
vector<int> ans;
for (int k = 1; k <= n; k++)
if (!path.count(k)) ans.push_back(k);
answer(ans);
return;
}
un(i, graph[i][j]);
}
}
for (int i = 1; i <= n; i++) {
if (degree[i] != 1) continue;
for (int j = 0; j < graph[i].size(); j++) {
int nxt = graph[i][j];
if (onenode[fin(nxt)] && onenode[fin(nxt)] != i) {
set<int> path;
findpath(path, i, onenode[fin(nxt)]);
vector<int> ans;
for (int k = 1; k <= n; k++)
if (!path.count(k)) ans.push_back(k);
answer(ans);
return;
} else {
onenode[fin(nxt)] = i;
onenode_conn[fin(nxt)].push_back(nxt);
}
}
}
for (int i = 1; i <= n; i++) {
if (!onenode[i]) continue;
if (dominated_by[onenode[i]]) {
set<int> keep_set;
findpath_onenode(keep_set, onenode_conn[dominated_by[onenode[i]]][0],
onenode[i]);
findpath_onenode(keep_set, onenode_conn[i][0], onenode[i]);
keep_set.insert(onenode[i]);
vector<int> ans;
for (int k = 1; k <= n; k++)
if (!keep_set.count(k)) ans.push_back(k);
if (ans.empty()) {
printf("No\n");
} else {
answer(ans);
}
return;
}
dominated_by[onenode[i]] = i;
}
printf("No\n");
}
int main() {
int T;
scanf("%d", &T);
for (int data = 1; data <= T; data++) {
work();
for (int i = 1; i <= n; i++)
graph[i].clear(), tgraph[i].clear(), degree[i] = 0, onenode[i] = 0,
onenode_conn[i].clear(),
dominated_by[i] = 0, onelink[i] = 0;
}
return 0;
}
| 6 |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
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.txt"
int main() {
vector<int>v(9);
{
for (int i = 0; i < 9; ++i)cin >> v[i];
}
vector<int>perms(9);
iota(perms.begin(), perms.end(), 0);
int ans = 0;
do {
vector<int>nums(9);
for (int i = 0; i < 9; ++i) {
nums[perms[i]] = i+1;
}
bool flag = true;
for (int i = 0; i < 9; ++i) {
if (v[i] != -1 && v[i] != nums[i])flag = false;
}
if (!flag)continue;
int aa = (nums[0] + nums[2] + nums[5]) * 1+ (nums[1] + nums[4]) * 10 + nums[3] * 100;
int bb = (nums[8]) * 1 + (nums[7]) * 10 + (nums[6]) * 100;
if (aa == bb)ans++;
} while (next_permutation(perms.begin(), perms.end()));
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
long long a[maxn], sum[maxn];
int f[maxn], ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];
f[n] = n;
f[n + 1] = n + 1;
for (int i = n - 1; i; i--)
if (a[i] == 1)
f[i] = f[i + 1];
else
f[i] = i;
for (int i = 1; i <= n; i++) {
long long mul = 1;
int j = i, tmp = i;
while (j <= n && (long long)2e18 / a[j] >= mul) {
mul *= a[j];
long long res = sum[j] - sum[i - 1];
tmp = j;
j = f[j + 1];
if (mul % k == 0 && mul / k >= res && mul / k <= res + j - tmp - 1) ans++;
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int pts[3][2], dx, dy, nx, nxx;
for (int i = 0; i < 3; i++) cin >> pts[i][0] >> pts[i][1];
cout << 3 << endl;
for (int i = 0; i < 3; i++) {
nx = (i + 1) % 3;
nxx = (i + 2) % 3;
dx = pts[i][0] - pts[nx][0];
dy = pts[i][1] - pts[nx][1];
cout << pts[nxx][0] + dx << " " << pts[nxx][1] + dy << endl;
}
return 0;
}
| 2 |
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef int W;
const int MAXV = 1005;
const int MAXE = 5005;
const int INF = 100000000;
int adj[100][100];
int main() {
int n,m,s,g1,g2,b1,b2,c;
while(~scanf("%d%d%d%d%d", &n, &m, &s, &g1, &g2),
n|m|s|g1|g2) {
s--,g1--,g2--;
for(int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j)
adj[i][j] = INF;
adj[i][i] = 0;
}
for(int i=0; i<m; ++i) {
scanf("%d%d%d", &b1, &b2, &c);
b1--,b2--;
adj[b1][b2] = c;
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i) {
const int p = adj[i][k];
for (int j = 0; j < n; ++j)
adj[i][j] = min(adj[i][j], p + adj[k][j]);
}
int minc = INF;
for(int i=0; i<n; ++i)
minc = min(minc, adj[s][i]+adj[i][g1]+adj[i][g2]);
printf("%d\n", minc);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int Aqua_Fortis, Aqua_Regia, Amalgama, Minium, Vitriol, Philosopher_stone;
int main() {
cin >> Aqua_Fortis >> Aqua_Regia >> Amalgama >> Minium >> Vitriol;
while ((Aqua_Fortis >= 1) && (Aqua_Regia >= 1) && (Amalgama >= 2) &&
(Minium >= 7) && (Vitriol >= 4)) {
Aqua_Fortis -= 1;
Aqua_Regia -= 1;
Amalgama -= 2;
Minium -= 7;
Vitriol -= 4;
Philosopher_stone++;
}
cout << Philosopher_stone;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int T, n, A[maxn];
long long k, SUM[maxn];
int main() {
scanf("%d", &T);
while (T--) {
int pos = 1;
long long sum = 0, ans = 0;
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
sum += 1ll * A[i];
}
if (sum <= k) {
puts("0");
continue;
}
sort(A + 1, A + 1 + n), SUM[0] = 0;
for (int i = 1; i <= n; i++) SUM[i] = SUM[i - 1] + 1ll * A[i];
while (A[pos] == A[1]) pos++;
for (int i = 1; i <= n; i++) {
long long s = k - (SUM[i] - SUM[1]);
if (s > 0)
s = s / (n - i + 1);
else
s = -1ll * (abs(s) / (n - i + 1) + (abs(s) % (n - i + 1) != 0));
if (s > A[1]) s = A[1];
if (i == 1) {
if (A[1] == s)
ans = n - pos + 1;
else
ans = A[1] - s + (n - 1);
} else {
if (A[1] == s) {
if (pos > i)
ans = min(ans, 1ll * n - pos + 1);
else
ans = min(ans, 1ll * n - i);
} else
ans = min(ans, A[1] - s + (n - i));
}
}
printf("%lld\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef struct segt {
long long s0, s1;
long long lazy;
int len;
} segmenttree;
segmenttree tree[524312];
long long fib[524312 >> 1];
long long fibprefix[524312 >> 1];
int n;
void build(int l, int r, int index);
void update(int pt, int l, int r, int index, long long value);
void increment(int start, int end, int l, int r, int index, long long value);
segmenttree query(int start, int end, int l, int r, int index);
void update(int start, int end, int l, int r, int index, long long value);
int arr[524312 >> 1];
int main(int argc, char *argv[]) {
int i, q;
scanf("%d%d", &n, &q);
for (i = 0; i <= n; i++) {
if (i == 0) {
fib[i] = 1;
fibprefix[i] = 1;
} else if (i == 1) {
fib[i] = 1;
fibprefix[i] = 2;
scanf("%d", &arr[i]);
} else {
fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000000;
fibprefix[i] = (fib[i] + fibprefix[i - 1]) % 1000000000;
scanf("%d", &arr[i]);
}
}
build(1, n, 1);
while (q > 0) {
int t, l, r;
long long v;
scanf("%d", &t);
if (t == 1) {
scanf("%d%lld", &l, &v);
update(l, l, 1, n, 1, v);
} else if (t == 2) {
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r, 1, n, 1).s0);
} else if (t == 3) {
scanf("%d%d%lld", &l, &r, &v);
increment(l, r, 1, n, 1, v);
}
q--;
}
return 0;
}
segmenttree merge_interval(segmenttree t1, segmenttree t2) {
segmenttree t;
t.lazy = 0ll;
t.len = t1.len + t2.len;
long long coeff0 = (t2.s1 * (t1.len > 0 ? fib[t1.len - 1] : 0ll) +
t2.s0 * (t1.len > 1 ? fib[t1.len - 2] : 0ll)) %
1000000000;
long long coeff1 =
(t2.s1 * fib[t1.len] + t2.s0 * (t1.len > 0 ? fib[t1.len - 1] : 0ll)) %
1000000000;
t.s0 = (t1.s0 + coeff0) % 1000000000;
t.s1 = (t1.s1 + coeff1) % 1000000000;
return t;
}
void lazy_propagation(int index, int l, int r) {
if (tree[index].lazy != 0) {
tree[index].s0 += fibprefix[r - l] * tree[index].lazy;
tree[index].s1 += (fibprefix[r - l + 1] - 1) * tree[index].lazy;
tree[index].s0 %= 1000000000;
tree[index].s1 %= 1000000000;
if (l != r) {
tree[index * 2].lazy =
(tree[index * 2].lazy + tree[index].lazy) % 1000000000;
tree[index * 2 + 1].lazy =
(tree[index * 2 + 1].lazy + tree[index].lazy) % 1000000000;
}
tree[index].lazy = 0ll;
}
}
segmenttree query(int start, int end, int l, int r, int index) {
segmenttree t;
t.s0 = t.s1 = 0ll, t.lazy = 0ll, t.len = 0;
if (start > end || l > r) return t;
lazy_propagation(index, l, r);
if (start > r || l > end) return t;
if (start <= l && r <= end) {
return tree[index];
}
int mid = l + (r - l) / 2;
if (end <= mid) {
return query(start, end, l, mid, index * 2);
} else if (start >= mid + 1) {
return query(start, end, mid + 1, r, index * 2 + 1);
}
segmenttree q1 = query(start, end, l, mid, index * 2);
segmenttree q2 = query(start, end, mid + 1, r, index * 2 + 1);
t = merge_interval(q1, q2);
return t;
}
void build(int l, int r, int index) {
if (l > r) return;
if (l == r) {
tree[index].s0 = tree[index].s1 = arr[l];
tree[index].len = 1;
tree[index].lazy = 0ll;
return;
}
int mid = l + (r - l) / 2;
build(l, mid, index * 2);
build(mid + 1, r, index * 2 + 1);
tree[index] = merge_interval(tree[index * 2], tree[index * 2 + 1]);
}
void increment(int start, int end, int l, int r, int index, long long value) {
if (l > r || start > end) return;
lazy_propagation(index, l, r);
if (start > r || end < l) return;
if (start <= l && r <= end) {
tree[index].s0 = (tree[index].s0 + value * fibprefix[r - l]) % 1000000000;
tree[index].s1 =
(tree[index].s1 + value * (fibprefix[r - l + 1] - 1)) % 1000000000;
if (l != r) {
tree[index * 2].lazy = (tree[index * 2].lazy + value) % 1000000000;
tree[index * 2 + 1].lazy =
(tree[index * 2 + 1].lazy + value) % 1000000000;
}
return;
}
int mid = l + (r - l) / 2;
increment(start, end, l, mid, index * 2, value);
increment(start, end, mid + 1, r, index * 2 + 1, value);
tree[index] = merge_interval(tree[index * 2], tree[index * 2 + 1]);
}
void update(int start, int end, int l, int r, int index, long long value) {
if (l > r) return;
lazy_propagation(index, l, r);
if (start > r || end < l) return;
if (start <= l && r <= end) {
tree[index].s0 = value;
tree[index].s1 = value;
return;
}
int mid = l + (r - l) / 2;
update(start, end, l, mid, index * 2, value);
update(start, end, mid + 1, r, index * 2 + 1, value);
tree[index] = merge_interval(tree[index * 2], tree[index * 2 + 1]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000008;
int n, m, k, d, i, j, e;
int tmp[N], pos[N], p[N], c[N];
char s[N];
int main() {
scanf("%s %d", s, &m);
n = strlen(s);
while (m--) {
scanf("%d%d", &k, &d);
for (i = 0; i < n; i++) {
c[i] = (i + 1) % n;
pos[i] = p[i] = i;
}
int t = 0;
for (i = 0; i < d; i++)
for (j = i; j < k; t++, j += d) p[t] = j;
for (i = 0; i < n; ++i) {
c[i] = p[c[i]];
}
for (e = n - k + 1; e; e >>= 1) {
for (i = 0; i < n; ++i) tmp[i] = c[i];
if (e & 1)
for (i = 0; i < n; ++i) pos[i] = c[pos[i]];
for (i = 0; i < n; ++i) c[i] = tmp[c[i]];
}
for (i = 0; i < n; ++i) tmp[(i + n - k + 1) % n] = s[pos[i]];
for (i = 0; i < n; ++i) putchar(s[i] = (char)tmp[i]);
putchar('\n');
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long a[k];
long long b[n + 1][2];
for (long long i = 0; i < k; i++) {
cin >> a[i];
}
for (long long i = 0; i < n + 1; i++) {
b[i][0] = -1;
b[i][1] = -1;
}
for (long long i = 0; i < k; i++) {
if (b[a[i]][0] == -1) {
b[a[i]][0] = i;
b[a[i]][1] = i;
} else {
b[a[i]][1] = i;
}
}
long long s = 0;
if (n == 1) {
if (b[1][0] == -1) s++;
} else {
for (long long i = 2; i < n; i++) {
if (b[i][0] == -1)
s = s + 3;
else {
if (b[i][0] > b[i - 1][1]) s = s + 1;
if (b[i][0] > b[i + 1][1]) s = s + 1;
}
}
if (b[1][0] == -1)
s = s + 2;
else {
if (b[1][0] > b[2][1]) s++;
}
if (b[n][0] == -1)
s = s + 2;
else {
if (b[n][0] > b[n - 1][1]) s++;
}
}
cout << s;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct camion {
int d, h, c, r;
};
camion t[250000];
int a[401];
bool sirve(int v, int idCamion) {
int litros = v;
int cantRecargas = t[idCamion].r;
for (int i = t[idCamion].d + 1; i <= t[idCamion].h; i++) {
if (v < (a[i] - a[i - 1])) {
return false;
}
if (litros < (a[i] - a[i - 1])) {
if (cantRecargas) {
litros = v;
cantRecargas--;
} else {
return false;
}
}
litros -= (a[i] - a[i - 1]);
}
return true;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i + 1]);
for (int i = 0; i < m; i++)
scanf("%d %d %d %d", &t[i].d, &t[i].h, &t[i].c, &t[i].r);
set<int> dif;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) dif.insert(a[j] - a[i]);
vector<int> diferencias;
diferencias.reserve(dif.size());
for (auto i : dif) diferencias.push_back(i);
long long minV = 0;
for (int i = 0; i < m; i++) {
int ini = 0;
int fin = diferencias.size() - 1;
while (ini + 1 < fin) {
int m = (ini + fin) / 2;
if (sirve(diferencias[m], i)) {
fin = m;
} else {
ini = m;
}
}
if (sirve(diferencias[ini], i)) {
minV = max(minV, ((long long)diferencias[ini]) * ((long long)t[i].c));
} else {
minV = max(minV, ((long long)diferencias[fin]) * ((long long)t[i].c));
}
}
printf("%I64d\n", minV);
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a[300001],f[300001][2][2],mem[300001][2];
pair<int,int>b[300001];
set<int>s;
int main(){
r(i,300001)a[i]=1;
cin>>n>>m;
r(i,m)cin>>b[i].first>>b[i].second;
sort(b,b+m);
r(i,m){
int x=b[i].second;
if(!s.count(x)){
s.insert(x);
int x1=a[x],x2=a[x-1];
a[x]+=x2,a[x-1]+=x1;
if(f[x][0][0]){f[x][1][0]++;}
else{
mem[x][0]=a[x];
f[x][0][0]++;
}
if(f[x-1][0][1])f[x-1][1][1]++;
else{
mem[x-1][1]=a[x-1];
f[x-1][0][1]++;
}
}
else{
if(f[x][1]&&f[x-1][1]){
int x1=a[x]-mem[x][0],x2=a[x-1]-mem[x-1][1];
a[x]+=x2,a[x-1]+=x1;
mem[x][0]=a[x],mem[x-1][1]=a[x-1];
f[x][1][0]=f[x-1][1][1]=0;
}
else if(f[x][1]){
a[x-1]+=a[x]-a[x-1];
mem[x-1][1]=a[x-1];
f[x][1][0]==0;
}
else if(f[x-1][1]){
a[x]+=a[x-1]-a[x];
mem[x][1]=a[x];
f[x-1][1][1]=0;
}
}
}
r(i,n){
if(i)cout<<' ';
cout<<a[i];
}
cout<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int n;
cin >> n;
vector<int> a(n);
for (int j = 0; j < n; ++j) {
cin >> a[j];
}
int ans = 1;
sort(a.begin(), a.end());
for (int j = 1; j < n; ++j) {
if (a[j] - a[j - 1] == 1) {
ans = 2;
break;
}
}
cout << ans << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, arr[26];
memset(arr, -1, sizeof(arr));
cin >> n;
char a[n], b[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
int setno = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (arr[a[i] - 'a'] == -1 && arr[b[i] - 'a'] == -1) {
arr[a[i] - 'a'] = setno;
arr[b[i] - 'a'] = setno;
setno++;
} else if (arr[b[i] - 'a'] == -1) {
arr[b[i] - 'a'] = arr[a[i] - 'a'];
} else if (arr[a[i] - 'a'] == -1) {
arr[a[i] - 'a'] = arr[b[i] - 'a'];
} else if (arr[a[i] - 'a'] != arr[b[i] - 'a']) {
for (int j = 0; j < 26; j++)
if (arr[j] == arr[b[i] - 'a'] && j != (b[i] - 'a'))
arr[j] = arr[a[i] - 'a'];
arr[b[i] - 'a'] = arr[a[i] - 'a'];
}
}
}
vector<pair<char, char>> ans;
for (int i = 0; i < 26; i++) {
int flag = 0;
char x, y;
for (int j = 0; j < 26; j++) {
if (arr[j] == i && flag == 0) {
x = 'a' + j;
flag = 1;
} else if (arr[j] == i) {
y = 'a' + j;
ans.push_back(make_pair(x, y));
x = y;
}
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int b[152], gc[152][152];
inline int gcd(int x, int y) { return (y == 0) ? x : gcd(y, x % y); }
inline unsigned int C(int n, int m) {
int i, j, d, p;
unsigned int res = 1;
for (i = 1; i <= m; i++) b[i] = n - i + 1;
for (i = 1; i <= m; i++)
for (p = i, j = 1; (p > 1) && (j <= m); j++)
if (b[j] > 1) {
d = gc[b[j] % p][p];
if (d > 1) p /= d, b[j] /= d;
}
for (i = 1; i <= m; i++) res *= b[i];
return res;
}
unsigned int ans = 0, a[152];
int n, P, Q, u;
int main() {
for (int i = 0; i < 152; i++)
for (int j = 0; j < 152; j++) gc[i][j] = gcd(i, j);
scanf("%d%d%d", &n, &P, &Q);
int i, j;
unsigned int v, res;
u = min(n - 1, P);
for (i = 0; i <= u; i++) a[i] = C(n, i);
for (i = 1; i <= Q; i++) {
for (res = 0, v = 1, j = 0; j <= u; j++, v *= i) res += a[j] * v;
ans ^= i * res;
}
printf("%u", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
long long int mod = 2000000000 + 7;
long long int inf = (long long int)(1e18);
using namespace std;
mt19937 unlucko(chrono::steady_clock::now().time_since_epoch().count());
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
bool eq = true;
int zor = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
zor ^= a[i];
if (i && a[i] != a[i - 1]) eq = false;
}
if (eq) {
cout << "YES\n0";
return 0;
}
if (n & 1) {
cout << "YES\n" << n - 2 << "\n";
for (int i = 3; i <= n; i += 2) {
for (int j = i - 2; j <= i; j++) {
cout << j << " ";
}
cout << "\n";
}
for (int i = n - 4; i >= 1; i -= 2) {
for (int j = i; j <= i + 2; j++) {
cout << j << " ";
}
cout << "\n";
}
} else if (zor)
cout << "NO\n";
else {
cout << "YES\n" << n - 2 << "\n";
cout << "1 2 3\n";
for (int i = 4; i <= n; i += 2) {
for (int j = i - 2; j <= i; j++) {
cout << j << " ";
}
cout << "\n";
}
for (int i = n - 4; i >= 2; i -= 2) {
for (int j = i; j <= i + 2; j++) {
cout << j << " ";
}
cout << "\n";
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int maxn = 2e6 + 7;
struct Complex {
double x, y;
Complex(double _x = 0.0, double _y = 0.0) : x(_x), y(_y) {}
Complex operator-(const Complex &b) const {
return Complex(x - b.x, y - b.y);
}
Complex operator+(const Complex &b) const {
return Complex(x + b.x, y + b.y);
}
Complex operator*(const Complex &b) const {
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
};
void change(Complex *y, int len) {
for (int i = 1, j = len / 2; i < len - 1; i++) {
if (i < j) swap(y[i], y[j]);
int k = len / 2;
while (j >= k) {
j -= k;
k /= 2;
}
if (j < k) j += k;
}
}
void fft(Complex *y, int len, int on) {
change(y, len);
for (int h = 2; h <= len; h <<= 1) {
Complex wn(cos(-on * 2 * pi / h), sin(-on * 2 * pi / h));
for (int j = 0; j < len; j += h) {
Complex w(1, 0);
for (int k = j; k < j + h / 2; k++) {
Complex u = y[k];
Complex t = w * y[k + h / 2];
y[k] = u + t;
y[k + h / 2] = u - t;
w = w * wn;
}
}
}
if (on == -1)
for (int i = 0; i < len; ++i) y[i].x /= len;
}
int n, x, a[maxn], b[maxn], s[maxn];
Complex c1[maxn], c2[maxn];
int main() {
cin >> n >> x;
for (int i = 1, v; i <= n; ++i) {
cin >> v;
if (v >= x)
s[i] = s[i - 1];
else
s[i] = s[i - 1] + 1;
a[s[i]]++;
}
a[0]++;
int len = 1;
while (len <= n + n) len <<= 1;
for (int i = 0; i <= n; ++i)
c1[i] = Complex(a[i], 0), c2[i] = Complex(a[n - i], 0);
fft(c1, len, 1);
fft(c2, len, 1);
for (int i = 0; i < len; ++i) c1[i] = c1[i] * c2[i];
fft(c1, len, -1);
int p = 0;
long long res = 0;
for (int i = 1; i <= n; ++i)
if (s[i] != s[i - 1])
p = i, res += i - p;
else
res += i - p;
cout << res << " ";
for (int i = n - 1; i >= 0; --i)
cout << (long long)(c1[i].x + 0.5) << " \n"[i == 0];
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long Cubic[N], m;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> m;
for (long long i = 1; i < N; i++) {
Cubic[i] = i * i * i;
if (Cubic[i] > 2 * m) break;
}
long long x = 0, idx = 1, ans = 0;
vector<long long> v;
while (x <= m) {
while (x + Cubic[idx] >= Cubic[idx + 1] && x + Cubic[idx] <= m) {
++idx;
}
if (x + Cubic[idx] > m) break;
x += Cubic[idx], ans++;
v.push_back(idx);
}
int sz = v.size();
for (int i = sz - 1; i >= 0; i--) {
while (true) {
long long add = Cubic[v[i] + 1] - Cubic[v[i]], now = 0;
v[i]++;
bool valid = 1;
for (int j = 0; j < sz; j++) {
now += Cubic[v[j]];
if (now >= Cubic[v[j] + 1]) {
valid = 0;
break;
}
}
if (valid && now <= m)
x += add;
else {
--v[i];
break;
}
}
}
cout << ans << " " << x << "\n";
}
| 2 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
int main(){
int n,q;
cin >> n >> q;
vector<array<ll,3>> events;
rep(i,n){
ll s,t,x;
cin >> s >> t >> x;
events.push_back({s-x,1,x});
events.push_back({t-x,-1,x});
}
sort(events.begin(),events.end(),[](array<ll,3> a,array<ll,3> b){return a[0]>b[0] || (a[0]==b[0] && a[1]>b[1]);});
set<ll> stop_coord;
rep(i,q){
ll d;
cin >> d;
while(!events.empty() && (*events.rbegin())[0]<=d){
if( (*events.rbegin())[1] == 1 )stop_coord.emplace((*events.rbegin())[2]);
else stop_coord.erase((*events.rbegin())[2]);
events.pop_back();
}
if(stop_coord.empty())cout << "-1" << endl;
else cout << *stop_coord.begin() << endl;
}
return 0;
} | 0 |
#include <algorithm>
#include <iostream>
#include <complex>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
using W = long double;
typedef complex<W> P;
typedef pair<P,P> L;
typedef pair<P,W> C;
typedef vector<P> Poly;
#define X real()
#define Y imag()
const W EPS = (1e-6), INF = (1e15);
W dot(P a, P b){ return a.X * b.X + a.Y * b.Y; }
W cross(P a, P b){ return a.X * b.Y - a.Y * b.X; }
int ccw(P a, P b, P c) {
b -= a; c -= a;
if( cross(b,c) > EPS ) return +1;
if( cross(b,c) < -EPS ) return -1;
if( dot(b,c) < 0 ) return +2;
if( abs(b) + EPS < abs(c) ) return -2;
return 0;
}
W ps_dist(P a, L s){
P sf = s.first, ss = s.second;
if(dot(ss-sf,a-sf) >= 0 && dot(sf-ss,a-ss) >= 0)
return abs(cross(sf-ss,a-ss))/abs(sf-ss);
return min(abs(a-sf), abs(a-ss));
}
bool intersect(C a, C b){
W d = abs(a.first-b.first);
W ra = a.second, rb = b.second;
return d-EPS < ra+rb;
}
int in_poly(P a, const Poly &p){
int n = p.size(), c = 0;
for(int i = 0; i < n; ++i){
P s = p[i]-a, t = p[(i+1)%n]-a;
if(!ccw(s,t,P(0,0))) return 1;//辺上
if(s.Y > t.Y + EPS) swap(s,t);
if((s.Y*t.Y < 0 || (s.Y*t.Y < EPS && t.Y > EPS)) && cross(s,t) < EPS) ++c;
}
if(c%2) return 2;//内部
return 0;//外部
}
int main(){
int N;
cin >> N;
vector<vector<P>> ST(N,vector<P>(2));
vector<W> R(N);
for(int i = 0; i < N; ++i){
cin >> R[i];
for(int j = 0; j < 2; ++j){
W x, y;
cin >> x >> y;
ST[i][j] = P(x,y);
}
}
vector<int> dp(1<<N,-1);
dp[0] = 0;
for(int i = 0; i < 1<<N; ++i){
if(dp[i] < 0) continue;
for(int j = 0; j < N; ++j){
if((i>>j)&1) continue;
int i_ = i|(1<<j);
if(dp[i_] == dp[i]+1) continue;
P s = ST[j][0], t = ST[j][1];
W r = R[j];
C cs(s,r), ct(t,r);
P d = s-t;
d /= abs(d);
P n = d*P(0,1);
vector<P> rec = {s+n*r,s-n*r,t-n*r,t+n*r};
bool f = true;
for(int k = 0; k < N; ++k){
if(k == j) continue;
P c = ST[k][i>>k&1];
C ck(c,R[k]);
f &= (not intersect(ck,cs));
f &= (not intersect(ck,ct));
f &= (not in_poly(c,rec));
for(int a = 0; a < 4; ++a){
int a_ = (a+1)%4;
f &= (ps_dist(c,L(rec[a],rec[a_])) >= R[k]-EPS);
}
}
if(f) dp[i_] = max(dp[i_],dp[i]+1);
}
}
cout << *max_element(dp.begin(), dp.end()) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
const int maxd = 250010;
const int maxm = 20010;
struct edge {
int y, d, next;
} a[maxm];
struct node {
int x, y;
} q[maxd];
int n;
int len, first[maxn];
int pre[maxn][maxn][2], ans[maxd][2], tot;
bool v1[maxn][maxn];
void ins(int x, int y) {
len++;
a[len].y = y;
a[len].next = first[x];
first[x] = len;
}
void init() {
int m, x, y;
scanf("%d%d", &n, &m);
len = 0;
memset(first, -1, sizeof(first));
while (m--) {
scanf("%d%d", &x, &y);
ins(x, y);
ins(y, x);
}
}
void solve() {
int head, tail, x, y, xx, yy, z;
node tmp, now;
head = tail = 1;
q[1].x = 1;
q[1].y = n;
memset(v1, false, sizeof(v1));
v1[1][n] = true;
pre[1][n][0] = -1;
while (head <= tail) {
now = q[head++];
x = now.x;
y = now.y;
for (int k = first[x]; k != -1; k = a[k].next) {
xx = a[k].y;
if (v1[xx][y]) continue;
v1[xx][y] = true;
for (int k = first[y]; k != -1; k = a[k].next) {
yy = a[k].y;
if (xx == yy) continue;
if (!pre[xx][yy][0]) {
pre[xx][yy][0] = x;
pre[xx][yy][1] = y;
tmp.x = xx;
tmp.y = yy;
q[++tail] = tmp;
}
}
}
}
if (!pre[n][1][0])
printf("-1\n");
else {
tot = 0;
x = n;
y = 1;
while (x != -1) {
tot++;
ans[tot][0] = x;
ans[tot][1] = y;
z = x;
x = pre[x][y][0];
y = pre[z][y][1];
}
printf("%d\n", tot - 1);
for (int i = tot; i >= 1; i--) printf("%d ", ans[i][0]);
printf("\n");
for (int i = tot; i >= 1; i--) printf("%d ", ans[i][1]);
printf("\n");
}
}
int main() {
init();
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 5;
struct node {
int n4, n7, n47, n74;
void change() {
swap(n4, n7);
swap(n47, n74);
}
void mrg(node l, node r) {
n4 = l.n4 + r.n4;
n7 = l.n7 + r.n7;
n47 = max(n4, n7);
n47 = max(n47, l.n4 + r.n7);
n47 = max(n47, l.n47 + r.n7);
n47 = max(n47, l.n4 + r.n47);
n74 = max(n4, n7);
n74 = max(n74, l.n7 + r.n4);
n74 = max(n74, l.n74 + r.n4);
n74 = max(n74, l.n7 + r.n74);
}
} sgtr[MAX << 2];
int lazy[MAX << 2];
char s[MAX];
void build(int now, int L, int R) {
int mid = (L + R) >> 1;
int l_chld = now << 1;
int r_chld = l_chld | 1;
if (L == R) {
if (s[L] == '4') {
sgtr[now].n4 = 1;
sgtr[now].n7 = 0;
sgtr[now].n47 = 1;
sgtr[now].n74 = 1;
} else {
sgtr[now].n4 = 0;
sgtr[now].n7 = 1;
sgtr[now].n47 = 1;
sgtr[now].n74 = 1;
}
return;
}
build(l_chld, L, mid);
build(r_chld, mid + 1, R);
sgtr[now].mrg(sgtr[l_chld], sgtr[r_chld]);
}
void update(int now, int L, int R, int l, int r) {
int mid = (L + R) >> 1;
int l_chld = now << 1;
int r_chld = l_chld | 1;
if (lazy[now]) {
sgtr[now].change();
lazy[now] = 0;
if (L != R) {
lazy[l_chld] = (lazy[l_chld] + 1) % 2;
lazy[r_chld] = (lazy[r_chld] + 1) % 2;
}
}
if (R < L || L > r || R < l) return;
if (l <= L && R <= r) {
sgtr[now].change();
if (L != R) {
lazy[l_chld] = (lazy[l_chld] + 1) % 2;
lazy[r_chld] = (lazy[r_chld] + 1) % 2;
}
return;
}
update(l_chld, L, mid, l, r);
update(r_chld, mid + 1, R, l, r);
sgtr[now].mrg(sgtr[l_chld], sgtr[r_chld]);
}
node query(int now, int L, int R, int l, int r) {
int mid = (L + R) >> 1;
int l_chld = now << 1;
int r_chld = l_chld | 1;
if (lazy[now]) {
sgtr[now].change();
lazy[now] = 0;
if (L != R) {
lazy[l_chld] = (lazy[l_chld] + 1) % 2;
lazy[r_chld] = (lazy[r_chld] + 1) % 2;
}
}
if (l <= L && R <= r) return sgtr[now];
if (r <= mid) return query(l_chld, L, mid, l, r);
if (l > mid) return query(r_chld, mid + 1, R, l, r);
node ret;
ret.mrg(query(l_chld, L, mid, l, mid), query(r_chld, mid + 1, R, mid + 1, r));
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
scanf("%s", s);
build(1, 0, n - 1);
while (q--) {
char type[10];
scanf("%s", type);
if (type[0] == 'c') {
printf("%d\n", query(1, 0, n - 1, 0, n - 1).n47);
} else {
int l, r;
scanf("%d %d", &l, &r);
l--;
r--;
update(1, 0, n - 1, l, r);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > vp;
vector<int> d;
int countt[200005], countSum[200005];
int trck[209], trck2[209][200005];
int main() {
int i, j, k, l, temp, t, n, m, ans, caseno = 0;
while (cin >> n) {
vp.clear();
memset(countt, 0, sizeof(countt));
memset(trck, 0, sizeof(trck));
memset(trck2, 0, sizeof(trck2));
memset(countSum, 0, sizeof(countSum));
for (i = 0; i < n; i++) {
cin >> temp;
vp.push_back(make_pair(temp, 0));
countt[temp]++;
}
for (i = 0; i < n; i++) {
cin >> temp;
vp[i].second = temp;
countSum[vp[i].first] += temp;
trck[temp]++;
trck2[temp][vp[i].first]++;
}
sort(vp.begin(), vp.end());
ans = 2000000009;
int lost = 0;
for (i = n - 1; i >= 0; i -= countt[vp[i].first]) {
j = countt[vp[i].first];
int needed = i + 1 - (j * 2 - 1);
k = 1;
temp = lost;
while (needed > 0 && k < 201) {
m = trck[k] - trck2[k][vp[i].first];
m = min(needed, m);
temp += m * k;
needed -= m;
trck[k] -= trck2[k][vp[i].first];
k++;
}
while (k < 201) {
trck[k] -= trck2[k][vp[i].first];
k++;
}
lost += countSum[vp[i].first];
ans = min(ans, temp);
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void pencilcase(long long a, long long b, long long c, long long d,
long long k) {
if (k <= 1) {
cout << -1 << endl;
return;
}
long long pen;
long long pencil;
if (a % c > 0) {
pen = (a / c) + 1;
} else {
pen = (a / c);
}
if (b % d > 0) {
pencil = (b / d) + 1;
} else {
pencil = b / d;
}
if (pen + pencil <= k) {
cout << (k - pencil) << " " << pencil << endl;
} else {
cout << -1 << endl;
}
}
int main() {
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
long long a, b, c, d, k;
cin >> a >> b >> c >> d >> k;
pencilcase(a, b, c, d, k);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 77777 + 10, MOD = 777777777, M = 5;
long long seg[4 * N], a[N], dp[M][M][N];
int n;
void upd(int x, int val, int s = 0, int e = n, int id = 1) {
if (x < s || x >= e) return;
if (e - s == 1) {
seg[id] = val;
return;
}
int mid = (s + e) / 2;
upd(x, val, s, mid, 2 * id);
upd(x, val, mid, e, 2 * id + 1);
seg[id] = (seg[2 * id] * seg[2 * id + 1]) % MOD;
}
long long get(int l, int r, int s = 0, int e = n, int id = 1) {
if (r <= s || e <= l) return 1;
if (l <= s && e <= r) return seg[id];
int mid = (s + e) / 2;
return (get(l, r, s, mid, 2 * id) * get(l, r, mid, e, 2 * id + 1)) % MOD;
}
int main() {
int m;
cin >> n >> m;
n++;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
int d;
cin >> d;
dp[i][j][0] = d;
}
dp[4][i][0] = dp[i][4][0] = 1;
}
dp[4][4][0] = 1;
for (int l = 1; l <= n; l++)
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
for (int k = 1; k <= 3; k++) {
dp[i][j][l] += (dp[k][j][l - 1] * dp[i][k][0]) % MOD;
dp[i][j][l] %= MOD;
}
for (int i = 0; i <= n; i++) upd(i, 1);
set<int> st;
st.insert(0);
st.insert(n);
a[0] = 4;
a[n] = 4;
upd(0, dp[4][4][n - 1]);
while (m--) {
int id, v;
cin >> id >> v;
if (v == 0) {
if (a[id] == 0) {
cout << get(0, n) << "\n";
continue;
}
auto L = st.lower_bound(id);
L--;
auto R = st.upper_bound(id);
int l = *L, r = *R;
int t1 = a[l], t2 = a[r];
a[id] = v;
upd(l, dp[t1][t2][r - l - 1]);
upd(id, 1);
st.erase(id);
} else {
auto L = st.lower_bound(id);
L--;
auto R = st.upper_bound(id);
int l = *L, r = *R;
int t1 = a[l], t2 = a[r];
a[id] = v;
upd(id, dp[v][t2][r - id - 1]);
upd(l, dp[t1][v][id - l - 1]);
st.insert(id);
}
cout << get(0, n) << "\n";
}
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.