A. Infinite Sequence time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the elements are numerated from one).
Find the number on the n-th position of the sequence.
Input
The only line contains integer n (1 ≤ n ≤ 1014) — the position of the number to find.Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Output
Print the element in the n-th position of the sequence (the elements are numerated from one).
Sample test(s)
input
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1
题意:求1 12 123 1234 12345 123456……数列中第n个数是什么
先二分找到第n个数之前最靠近的区间(设a,即区间元素个数),如123 -》3,12345-》5,12345678-》8
(1+X)*X/2《=N
值有可能正好是在最后,那么直接可以输出a,否则就为n-a
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll Max=1e8;
int main()
{
ll n;
ll l,r,mid;
cin>>n;
l=0;
r=Max;
while(r-l>1){
mid=(l+r)/2;
if((1+mid)*mid/2<=n){
l=mid;
}else
r=mid;
}
if((l+1)*l/2==n){
cout<else{
ll s=(1+l)*l/2;
s=n-s;
cout<
C. Not Equal on a Segment time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output
You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.
Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106) — the parameters of the i-th query.
Output
Print m lines. On the i-th line print integer pi — the position of any number not equal to xi in segment [li, ri] or the value - 1 if there is no such number.
Sample test(s)
input
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
output
2
6
-1
4
题意:给一数列,每次查询[l,r]区间不等于x的任意一个数。
首先,记录0~i-1与ai不同的最近的数的位置c[i],然后再从查询r之前的,位置在l之后的,就输出,否则,不存在,为-1
#include
#include
#include
#include
#include
using namespace std;
const int N=200005;
const int M=1000005;
int a[N];
int c[N];
int main()
{
int n,m;
int l,r,x;
scanf("%d%d",&n,&m);
int cc=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]==a[i-1])
c[i]=c[i-1];
else
c[i]=i-1;
}
for(int i=0;iscanf("%d%d%d",&l,&r,&x);
if(a[r]==x){
if(a[c[r]]==0||c[r]printf("-1\n");
}else{
printf("%d\n",c[r]);
}
}else
printf("%d\n",r);
}
}