2018icpc沈阳站G题

本文探讨了如何在平面中动态处理点的插入、删除、权重更新及查询问题,特别是针对那些距离固定点的欧几里得距离为特定值的情况。通过预处理所有可能的坐标组合,使用set进行去重,确保了高效且准确地处理各种操作。


传送门:Best ACMer Solves the Hardest Problem


Best ACMer Solves the Hardest Problem
Time limit12000 ms Memory limit1048576 kB

One day an excellent ACMer will leave the field to face new challenges, just like what the predecessors are doing. Some of them have taken over their family businesses, and some of them are struggling with the edges of unemployment. Some of them have the courage to display themselves and become a professional Ingress player, and some of them are still pushing themselves to limits and try to solve all problems in Project Euler.

But all these destinations are so shallow for Benecol de Cecco, the former king. What he does now is to be the best respondents in StackOverflow. StackOverflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers.

Today, he notices a question which is posted by Kevin Li, saying: Recently, I implemented an experiment where I need to find all the data records whose Euclidean distances to the query point q are the same value r. I tried to use the k-d tree to improve the search efficiency. However, I found that the k-d tree needs to traverse all leaf nodes to return the result, that is, it still needs to compare all dataset to obtain the result.

This question can be formalized to build a database with real-time queries and modifications. In the beginning, suppose we have n different points in the plane. The i-th point is located at (xi,yi) and has a weight wi. Then we consider several queries and modifications dynamically given by

1 x y w, to insert a new point at (x,y) of weight w, where we guarantee that before the operation no point was located in the place;
2 x y, to delete the point located at (x,y), where we guarantee that the point existed before the operation;
3 x y k w, for each point whose Euclidean distance to (x,y) is √k, to increase its weight by w;
4 x y k, to query the sum of weights for all points whose Euclidean distances to (x,y) are √k.
Benecol de Cecco says this question is pretty easy and he asked me to share the problem with you all. By the way, the Euclidean distance between two points (x0,y0) and (x1,y1) is equal to sqrt((x0−x1)2+(y0−y1)2).

Input
The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 1000.

For each test case, the first line contains two integers n and m indicating the initial number of points in the plane and the number of operations respectively, where 1≤n,m≤105.

Each of the following n lines contains three integers x,y and w satisfying 1≤x,y,w≤6000, which describe a point at (x,y) of weight w in the beginning.

Each of the following m lines contains an operation which can be a query or a modification. These operations are given in the forms described above. To make all x and y in operations dynamic, we use lastans to denote the answer to the most recent query and its initial value is zero. For each operation with the values x and y in input, their real values should be (((x+lastans)mod6000)+1) and (((y+lastans)mod6000)+1) respectively. All coefficients in operations are integers and satisfy 0≤k≤107 and 1≤x,y,w≤6000.

We guarantee that the sum of n and the sum of m in all test cases are no larger than 106 individually.

Output
For each test case, output a line containing “Case #x:” (without quotes) at first, where x is the test case number starting from 1.

Then for each query, output an integer in a line indicating the answer.

Example
Input
1
3 6
2999 3000 1
3001 3000 1
3000 2999 1
1 2999 3000 1
4 2999 2999 1
2 2995 2996
3 2995 2995 1 1
4 2995 2995 1
4 3000 3000 1
Output
Case #1:
4
6
0
Note
In the sample case, if we ignore the special input format for dynamic x and y in operations, here we can show these modifications and queries directly in an offline format as follows:

1 3000 3001 1;
4 3000 3000 1;
2 3000 3001;
3 3000 3000 1 1;
4 3000 3000 1;
4 3007 3007 1.



打出1e7里每个数字可以有哪些(x,y),x2+y2等于这个数,然后每个点找距离根号k的点的时候,就四个方向找一下,用set存来去重(某个点某维坐标变成0的时候,会有重复的)

wa到吐,不注意的话很容易就超时和超内存


AC代码:


#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e7+10;
const int mod=6000;
int t,n,m;
int x,y,w,op,k;
int dir[4][2]={{1,1},{1,-1},{-1,1},{-1,-1}};
typedef pair<int,int> p1;
vector<p1 > g[N];
vector<p1 > cc;
set<pair<int,int> > st;
//set<pair<int,int> >::iterator its;
int mp[6100][6100];
//map<pair<int,int>,int> mp;

void init()//预处理
{
  for(int a=0;a<=6000;a++)
  {
    for(int b=0;b<=6000;b++)
    {
      int k=a*a+b*b;
      if(k>1e7) break;
      g[k].push_back(make_pair(a,b));
    }
  }
}

int main()
{
  init();
  scanf("%d",&t);
  int ca=0;
  while(t--)
  {
    cc.clear();
    scanf("%d%d",&n,&m);
    printf("Case #%d:\n",++ca);
    for(int i=0;i<n;i++)
    {
      scanf("%d%d%d",&x,&y,&w);
      mp[x][y]=w;
      cc.push_back(make_pair(x,y));
    }
    ll lastans=0;
    while(m--)
    {
      scanf("%d%d%d",&op,&x,&y);
      x=(x+lastans)%mod+1;
      y=(y+lastans)%mod+1;
      if(op==1)
      {
        scanf("%d",&w);
        mp[x][y]=w;
        cc.push_back(make_pair(x,y));
      }
      else if(op==2)
      {
        mp[x][y]=0;
      }
      else if(op==3)
      {
        st.clear();
        scanf("%d%d",&k,&w);
        int len=g[k].size();
        for(int i=0;i<len;i++)
        {
          int dx=g[k][i].first,dy=g[k][i].second;
          for(int j=0;j<4;j++)
          {
            int xx=x+dx*dir[j][0],yy=y+dy*dir[j][1];
            if(xx<1||xx>6000||yy<1||yy>6000)
              continue;
            if(mp[xx][yy]!=0)
            {
              st.insert(make_pair(xx,yy));
            }
          }
        }
        set<pair<int,int> >::iterator its;
        for(its=st.begin();its!=st.end();its++)
        {
          int xx=its->first,yy=its->second;
          mp[xx][yy]+=w;
        }
      }
      else if(op==4)
      {
        st.clear();
        scanf("%d",&k);
        int len=g[k].size();
        ll sum=0;
        for(int i=0;i<len;i++)
        {
          int dx=g[k][i].first,dy=g[k][i].second;
          for(int j=0;j<4;j++)
          {
            int xx=x+dx*dir[j][0],yy=y+dy*dir[j][1];
            if(xx<1||xx>6000||yy<1||yy>6000)
              continue;
            if(mp[xx][yy]!=0)
            {
              st.insert(make_pair(xx,yy));
            }
          }

        }
        set<pair<int,int> >::iterator its;
        for(its=st.begin();its!=st.end();its++)
        {
          int xx=its->first,yy=its->second;
          sum+=mp[xx][yy];
        }
        lastans=sum;
        printf("%lld\n",sum);
      }
    }
    for(int i=0;i<cc.size();i++)
    {
      mp[cc[i].first][cc[i].second]=0;
    }
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值