Nastya and an Array

输出有几种不同的数字

#pragma comment(linker, "/STACK:102400000,102400000")
#ifndef ONLINE_JUDGE
#include "stdafx.h"
#else
#include<bits/stdc++.h>
#endif
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} int p[], q[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, x;
cin >> n;
int ans = ;
for (int i = ; i < n; i++) {
cin >> x;
if (x > ) {
if (!p[x]) ans++;
p[x] = true;
}
if (x < ) {
if (!q[-x]) ans++;
q[-x] = true;
}
}
cout << ans << endl;
return ;
}

Nastya Studies Informatics

把y/x分解质因数,a和b各自占有一部分且不重复

#pragma comment(linker, "/STACK:102400000,102400000")
#ifndef ONLINE_JUDGE
#include "stdafx.h"
#else
#include<bits/stdc++.h>
#endif
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} bool f[];
int p[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
memset(f, true, sizeof(f));
f[] = f[] = false;
for (int i = ; i < ; i++) {
for (int j = i + i; j < ; j += i) f[j] = false;
}
int l, r, x, y;
cin >> l >> r >> x >> y;
if (y % x != ) {
cout << << endl;
return ;
}
int m = y / x, sz = ;
for (int i = ; i < ; i++) {
if (m == ) break;
if (!f[i]) continue;
if (m % i == ) {
int tmp = ;
while (m % i == ) {
m /= i;
tmp *= i;
}
p[sz++] = tmp;
}
}
if (m != ) p[sz++] = m;
m = y / x;
int ans = ;
for (int i = ; i < ( << sz); i++) {
int a = , b;
for (int j = ; j < sz; j++) {
if (i & ( << j)) a *= p[j];
}
b = m / a;
if (l <= a * x && a * x <= r && l <= b * x && b * x <= r) ans++;
}
cout << ans << endl;
return ;
}

Nastya and a Wardrobe

每次x2-0.5

#pragma comment(linker, "/STACK:102400000,102400000")
#ifndef ONLINE_JUDGE
#include "stdafx.h"
#else
#include<bits/stdc++.h>
#endif
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} inline lint pow(lint a, lint b, lint p) {
lint rtn = ;
while (b) {
if (b & ) rtn = rtn * a % p;
a = a * a % p;
b >>= ;
}
return rtn;
} int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
lint x, k;
cin >> x >> k;
lint p = ;
if (k == ) {
cout << (x * % p) << endl;
return ;
}
if (x == ) {
cout << << endl;
return ;
}
lint a = pow(, k, p) * (x % p) % p, b = pow(, k - , p);
//a-b+0.5->2a-2b+1
cout << (( * a - * b + ) % p + p) % p << endl;
return ;
}
/*
2(2x - 0.5) - 0.5
2x - 0.5 1
4x - 1.5 2
8x - 3.5 4
16x - 7.5 8
32x - 15.5 16
*/

Nastya and a Game

枚举左端点然后依次往右找,对于一段连续的1可以根据范围直接判断。因为S的最大值不超过2*10^18,所以向右找的次数不超过120次。

#pragma comment(linker, "/STACK:102400000,102400000")
#ifndef ONLINE_JUDGE
#include "stdafx.h"
#else
#include<bits/stdc++.h>
#endif
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} lint a[], s[];
int r[];
double ln[];
double inf = log(1LL << ); int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, k;
cin >> n >> k;
s[] = ;
for (int i = ; i <= n; i++) {
cin >> a[i];
s[i] = s[i - ] + a[i];
ln[i] = log(a[i]);
}
for (int i = n; i >= ; i--) {
if (a[i] == ) {
if (a[i + ] == ) r[i] = r[i + ];
else r[i] = i;
}
}
lint ans = ;
if (k == ) ans = n;
for (int i = ; i <= n; i++) {
int j = i + ;
lint m = a[i];
double p = ln[i];
while (j <= n) {
if (a[j] == ) {
if (m % k == ) {
lint tmp = m / k;
if (s[j] - s[i - ] <= tmp && tmp <= s[r[j]] - s[i - ]) ans++;
}
j = r[j] + ;
} else {
if (p + ln[j] >= inf) break;
p += ln[j];
m *= a[j];
if (m == (s[j] - s[i - ])*k) ans++;
j++;
}
}
}
cout << ans << endl;
return ;
}

Nastya and King-Shamans

两个线段树,一个维护sum,一个维护max。对每个询问,如果a[1]=0直接输出1,否则令m=1,显然此时[1,m]里没有萨满王。对于m之后的,想成为萨满王其能力值至少为sum(1,m),找到第一个满足条件的萨满p,判断p是不是萨满王,如果不是则令m=p,直到找到或确定没有。

#pragma comment(linker, "/STACK:102400000,102400000")
#ifndef ONLINE_JUDGE
#include "stdafx.h"
#else
#include<bits/stdc++.h>
#endif
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} lint a[];
int n, q;
template <typename T> class SegmentTree {
/*
type:
0:sum
1:max
*/
private:
T *data, *lazy;
int type;
void pushup(int rt) {
if (type == )
data[rt] = data[rt << ] + data[rt << | ];
else if (type == )
data[rt] = max(data[rt << ], data[rt << | ]);
}
void pushdown(int rt, T m) {
if (lazy[rt] == ) return;
lazy[rt << ] += lazy[rt];
lazy[rt << | ] += lazy[rt];
if (type == ) {
data[rt << ] += (m - (m >> )) * lazy[rt];
data[rt << | ] += (m >> ) * lazy[rt];
} else if (type == ) {
data[rt << ] += lazy[rt];
data[rt << | ] += lazy[rt];
}
lazy[rt] = ;
}
public:
SegmentTree(int n, int t) : data((T *)malloc((n << ) * sizeof(T))), lazy((T *)malloc((n << ) * sizeof(T))), type(t) {}
void Build(T * base, int l, int r, int rt) {
lazy[rt] = ;
if (l == r) data[rt] = base[l];
else {
int mid = (l + r) >> ;
Build(base, l, mid, rt << );
Build(base, mid + , r, rt << | );
pushup(rt);
}
}
void Modify(int l, int r, int rt, int L, int R, T v) {
if (L <= l && R >= r) {
lazy[rt] += v;
if (type == )
data[rt] += v * (r - l + );
else if (type == )
data[rt] += v;
return;
}
pushdown(rt, r - l + );
int mid = (l + r) >> ;
if (L <= mid)
Modify(l, mid, rt << , L, R, v);
if (R > mid)
Modify(mid + , r, rt << | , L, R, v);
pushup(rt);
}
T QueryPoint(int l, int r, int rt, int val) {
if (l == r) return data[rt];
pushdown(rt, r - l + );
int mid = (l + r) >> ;
T ret = ;
if (val <= mid) ret = QueryPoint(l, mid, rt << , val);
else ret = QueryPoint(mid + , r, rt << | , val);
pushup(rt);
return ret;
}
T QuerySegment(int l, int r, int rt, int L, int R) {
pushdown(rt, r - l + );
if (L == l && R == r) return data[rt];
int mid = (l + r) >> ;
if (R <= mid) return QuerySegment(l, mid, rt << , L, R);
if (mid < L) return QuerySegment(mid + , r, rt << | , L, R);
if (type == )
return QuerySegment(l, mid, rt << , L, mid) + QuerySegment(mid + , r, rt << | , mid + , R);
else if (type == )
return max(QuerySegment(l, mid, rt << , L, mid), QuerySegment(mid + , r, rt << | , mid + , R));
}
int find(int l, int r, int rt, int p, T x) {
if (r <= p) return -;
if (data[rt] < x) return -;
if (l == r) return l;
int mid = (l + r) >> ;
if (l > p) {
if (data[rt << ] >= x) return find(l, mid, rt << , p, x);
else return find(mid + , r, rt << | , p, x);
}
int rtn = find(l, mid, rt << , p, x);
if (rtn != -) return rtn;
return find(mid + , r, rt << | , p, x);
}
};
SegmentTree<lint> Tsum(, ), Tmax(, ); int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
cin >> n >> q;
for (int i = ; i <= n; i++) cin >> a[i];
Tsum.Build(a, , n, );
Tmax.Build(a, , n, );
while (q--) {
int p;
lint x;
cin >> p >> x;
Tsum.Modify(, n, , p, p, x - a[p]);
Tmax.Modify(, n, , p, p, x - a[p]);
a[p] = x;
if (a[] == ) {
cout << << endl;
continue;
}
int m = ;
while () {
lint sum = Tsum.QuerySegment(, n, , , m);
int nex = Tmax.find(,n,,m,sum);
if (nex == -) {
cout << - << endl;
break;
}
if (a[nex] == Tsum.QuerySegment(, n, , , nex - )) {
cout << nex << endl;
break;
}
m = nex;
}
}
return ;
}

[Codeforces]Codeforces Round #489 (Div. 2)的更多相关文章

  1. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  2. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  3. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  4. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  5. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  7. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  8. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

  9. Codeforces Beta Round #72 (Div. 2 Only)

    Codeforces Beta Round #72 (Div. 2 Only) http://codeforces.com/contest/84 A #include<bits/stdc++.h ...

  10. Codeforces Beta Round #70 (Div. 2)

    Codeforces Beta Round #70 (Div. 2) http://codeforces.com/contest/78 A #include<bits/stdc++.h> ...

随机推荐

  1. 【Codeforces 675D】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...

  2. HDU 1085 多重背包转化为0-1背包问题

    题目大意: 给定一堆1,2,5价值的硬币,给定三个数表示3种价值硬币的数量,任意取,找到一个最小的数无法取到 总价值为M = v[i]*w[i](0<=i<3) 那么在最坏情况下M个数都能 ...

  3. Codeforces Round #226 (Div. 2) C题

    数论好题 题目要求:求给定序列的素因子如果在给定区间内该数字个数加1; 思路:打表时求出包含给素数因子的数的个数,详见代码 1 #include<cstring> +;      scan ...

  4. Centos7 上安装mysql遇上的问题:mysql无法正常启动

    第一次在Centos上安装mysql遇到的一些问题. 第一步就遇到问题,安装mysql-server报错没有可用包.  [解决方法] 先要安装mysql # wget http://repo.mysq ...

  5. android 使用post 提交

    1.使用post 方式提交时不要把须要传递的參数写在URL 中,一定要使用 BasicNameValuePair 这个类来完毕 创建我想发送一个类似Get 方式的一个URL ---------- ht ...

  6. 1159--Palindrome(dp:回文串变形2)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53431   Accepted: 18454 Desc ...

  7. 【C语言】不使用大小于号,求出两数最大值

    //不使用大小于号,求出两数最大值 #include <stdio.h> #include <math.h> double Max(double a, double b) { ...

  8. 继承自TWinControl的控件不能在设计期间接受子控件,用代码设置子控件却可以(它的自绘是直接改写PaintWindow虚函数,而不是覆盖Paint函数——对TWinControl.WMPaint又有新解了)

    这个控件直接继承自TWinControl,因此不是改写Paint;函数,而是直接改写PaintWindow虚函数,它在VCL框架里被直接调用,直接就把自己画好了(不用走给控件Perform(WM_Pa ...

  9. changing permissions of Read-only file system in linux

      up vote 2 down vote favorite 1 i use this command to make a bootable flash disk of linux mint sudo ...

  10. framework/base子目录

    framework/base下各子目录 ~/src/aosp_master/frameworks $ tree base/ -L 1 base/ ├── Android.bp ├── Android. ...