快读,快写
inline int read(){
int x=0;bool f=0;char c=getchar();
while (c<'0'||c>'9'){if (c=='-')f=1;c=getchar();}
while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f?-x:x;
}
inline void print(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) print(x/10);
putchar(x%10+'0');
}
超级快读测试代码(数据量极大,比关闭同步流快)
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct fio
{
inline char gc()
{
static const int IN_LEN = 1 << 18 | 1;
static char buf[IN_LEN], *s, *t;
static streambuf *sb = cin.rdbuf();
return (s == t) && (t = (s = buf) + sb->sgetn(buf, IN_LEN)), s == t ? -1 : *s++;
}
inline fio &operator>>(string &s)
{
static char ch;
ch = gc();
s.clear();
for (; isspace(ch); ch = gc())
if (ch == -1)
return *this;
for (; !isspace(ch) && ch != -1; ch = gc())
s += ch;
return *this;
}
inline fio &operator>>(char *s)
{
static char ch;
ch = gc();
for (; isspace(ch); ch = gc())
if (ch == -1)
return *this;
for (; !isspace(ch) && ch != -1; ch = gc())
*(s++) = ch;
return *this;
}
inline void gtline(string &s)
{
static char ch;
ch = gc();
s.clear();
for (; ch != '\n' && ch != -1; ch = gc())
s += ch;
}
template <typename T>
inline fio &operator>>(vector<T> &n)
{
for (auto &it : n)
{
*this >> it;
}
return *this;
}
template <typename T>
inline fio &operator>>(T &n)
{
static char ch, sgn;
ch = gc(), sgn = 0;
for (; !isdigit(ch); ch = gc())
{
if (ch == -1)
return *this;
sgn |= ch == '-';
}
for (n = 0; isdigit(ch); ch = gc())
n = n * 10 + (ch ^ '0');
if (ch == '.')
{
T tmp = 0;
ch = gc();
for (; isdigit(ch); ch = gc())
tmp = tmp * 10 + (ch ^ '0');
while (tmp > 1)
tmp /= 10;
n += tmp;
}
sgn && (n = -n);
return *this;
}
} io;
signed main()
{
int T;
io>>T;
while(T--)
{
int n;
io>>n;
cout<<n<<endl;
}
}
版本2
struct I {
char fin[_], *p1 = fin, *p2 = fin;
inline char gc() {
return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
}
inline I& operator>>(int& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
inline I& operator>>(double& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c - 48);
while ((c = gc()) > 47) x = x * 10 + (c - 48);
if (c == '.') {
double d = 1.0;
while ((c = gc()) > 47) d = d * 0.1, x = x + (d * (c - 48));
}
x = sign ? x : -x;
return *this;
}
inline I& operator>>(char& x) {
do
x = gc();
while (isspace(x));
return *this;
}
inline I& operator>>(string& s) {
s = "";
char c = gc();
while (isspace(c)) c = gc();
while (!isspace(c) && c != EOF) s += c, c = gc();
return *this;
}
} in;
struct O {
char st[100], fout[_];
signed stk = 0, top = 0;
inline void flush() {
fwrite(fout, 1, top, stdout), fflush(stdout), top = 0;
}
inline O& operator<<(int x) {
if (top > (1 << 20)) flush();
if (x < 0) fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(char x) {
fout[top++] = x;
return *this;
}
inline O& operator<<(string s) {
if (top > (1 << 20)) flush();
for (char x : s) fout[top++] = x;
return *this;
}
} out;
1
[3, 1, 4, 1, 5, 9, 2, 6]
2 4
int t; cin>>t; getchar();
while(t--){
string s;
getline(cin,s);
int x,y; cin>>x>>y;
getchar();
}
二分:最小值最大,最大值最小,单调
求区间合并后的长度(没验证)
n是区间个数,m是所有区间总的范围
int CountSize(){
int flg[...], cnt = 0, ret = 0;
for (int i = 0; i < n; i++){
flg[ left[i] ]++;
flg[ right[i]+1 ]--;
}
for (int i = 0; i < m; i++){
cnt += flg[i];
if (cnt > 0){
ret++;
}
}
return ret;
}
归并排序求正序数
int n; int ans; int a[N]; int tmp[N];
void merge(int q[], int l, int r){
if (l >= r) return;
int mid = l + r >> 1;
merge(q, l, mid);
merge(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r){
if (q[i] <= q[j]){
tmp[k ++ ] = q[i ++ ];
ans=(ans+r-j+1)%mod;
}
else tmp[k ++ ] = q[j ++ ];
}
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}
主函数:
merge(a,1,n);
归并排序求逆序对数
void merge(int q[], int l, int r){
if (l >= r) return;
int mid = l + r >> 1;
merge(q, l, mid);
merge(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r){
if (q[i] <= q[j]){
tmp[k ++ ] = q[i ++ ];
}
else{
tmp[k ++ ] = q[j ++ ];
ans=ans+mid-i+1;
}
}
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}
进制转换
输入格式:共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用大写字母A−F表示数码10−15,并且该n进制数对应的十进制的值不超过1000000000,第三行也是一个正整数,表示转换之后的数的进制m(2≤m≤16)
输出格式:一个正整数,表示转换之后的m进制数
输入:16 FF 2 输出:11111111
int b,m; char s[40],t[40];
void upper(char c[]){ for(int i=0;i<40;i++) if(c[i]>='a'&&c[i]<='z') c[i]+='A'-'a';}
signed main(){ cin>>b>>s>>m; itoa(strtol(s,NULL,b),t,m); upper(t); cout<<t; }